Post: C++ Base App
07-12-2013, 03:36 AM #1
(adsbygoogle = window.adsbygoogle || []).push({}); Well, I got bored so I wrote a C++ base app. When writing an app the beginning code is the same mostly (if it's console apps lol), anyways go through this and see what I mean. I wrote all of this so I give full permission to use and release your app. All I ask is to not tell people you made the app without this thread if you use. Anyways, here it is lol.
Also, I'm sure there is a easier way to code this, but I was bored so I was figuring some stuff out by the way I coded this :p

Built in Visual C++ 2010

It looks like this:
You must login or register to view this content.

    
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <string>

// Define colors
void setColor(int e) {
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), e);
}

// Define pause
void pause() {
_getch();
}

// Define clear
void clear() {
system("CLS");
}

// Define spacer (\n)
void _space(std::string i = "\n") {
using namespace std;
using std::string;

cout << i;
}

// Define spacer2 (\n\n)
void _space2(std::string l = "\n") {
using namespace std;
using std::string;

cout << l << endl;
}

// Define text
void printText(std::string e) {
using namespace std;
using std::string;

cout << e;
}

// Define directions
void dir_init() {
setColor(14);
printText("Press A to say hello world!"); _space();
printText("Press B to say Hello World!!"); _space();
printText("Press C to say Hellow world!!!"); _space();
}

// Define header
void _header() {
setColor(12);
printText("========================= ::: "); printText("Name of Application"); printText(" ::: ========================="); _space();
printText("Build <number>"); _space();
printText("Created By: <name>"); _space();
printText("================================================================================");
_space2();
dir_init();
}

// Button handling
void but_init() {
char ch;
while(true) {
ch = _getch();
if (ch == 'a' || ch == 'A'Winky Winky {
printText("hello world!"); _space();
}
else if(ch == 'b' || ch == 'B'Winky Winky {
printText("Hello World!!"); _space();
}
else if(ch == 'c' || ch == 'C'Winky Winky {
printText("Hello world!!!"); _space();
}
else {
clear();
printText("Please press the correct options from the menu!"); _space2(); _space2();
_header();
}
}
pause();
}

int main() {
using namespace std;

SetConsoleTitle(L"Title of the console ie: app name");
_header();
but_init();
return 0;
}


Last edited by TheUnexpected ; 07-13-2013 at 03:16 AM.
07-12-2013, 08:41 PM #2
Complete Speed
Do a barrel roll!
Originally posted by TheUnexpected View Post
Well, I got bored so I wrote a C++ base app. When writing an app the beginning code is the same mostly (if it's console apps lol), anyways go through this and see what I mean. I wrote all of this so I give full permission to use and release your app. All I ask is to not tell people you made the app without this thread if you use. Anyways, here it is lol.
Also, I'm sure there is a easier way to code this, but I was bored so I was figuring some stuff out by the way I coded this :p

It looks like this:
You must login or register to view this content.

    
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <string>

// Define colors
void setColor(int e) {
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), e);
}

// Define pause
void pause() {
_getch();
}

// Define clear
void clear() {
system("CLS");
}

// Define spacer (\n)
void _space(std::string i = "\n") {
using namespace std;
using std::string;

cout << i;
}

// Define spacer2 (\n\n)
void _space2(std::string l = "\n") {
using namespace std;
using std::string;

cout << l << endl;
}

// Define text
void printText(std::string e) {
using namespace std;
using std::string;

cout << e;
}

// Define directions
void dir_init() {
setColor(14);
printText("Press A to say hello world!"); _space();
printText("Press B to say Hello World!!"); _space();
printText("Press C to say Hellow world!!!"); _space();
}

// Define header
void _header() {
setColor(12);
printText("========================= ::: "); printText("Name of Application"); printText(" ::: ========================="); _space();
printText("Build <number>"); _space();
printText("Created By: <name>"); _space();
printText("================================================================================");
_space2();
dir_init();
}

// Button handling
void but_init() {
char ch;
while(true) {
ch = _getch();
if (ch == 'a' || ch == 'A'Winky Winky {
printText("hello world!"); _space();
}
else if(ch == 'b' || ch == 'B'Winky Winky {
printText("Hello World!!"); _space();
}
else if(ch == 'c' || ch == 'C'Winky Winky {
printText("Hello world!!!"); _space();
}
else {
clear();
printText("Please press the correct options from the menu!"); _space2(); _space2();
_header();
}
}
pause();
}

int main() {
using namespace std;

SetConsoleTitle(L"Title of the console ie: app name");
_header();
but_init();
return 0;
}




this is really good and all, but it can be shorter and you used a lot of unnecessary includes.
i would have used. just this.
    
#include <iostream>
using namespace std;
void opt1();
void opt2();
void opt3();
void opt4();
void opt5();
void (*options[5])()=
{
opt1,opt2,opt3,opt4,opt5
};
int main()
{
int ans;
cout << "Welcome to the menu system\npress 1 -- say hello world!\npress 2 -- say hello word!\npress 3 -- say hello word!\npress 4 -- say hello word!\npress 2 -- say hello word!\nEnter Choice: ";
cin>>ans;
switch(ans)
{
case 1: options[0]();
system("pause>nul");
break;
case 2: options[1]();
system("pause>nul");
break;
case 3: options[2]();
system("pause>nul");
break;
case 4: options[3]();
system("pause>nul");
break;
case 5: options[4]();
system("pause>nul");
break;
return 0;
}
}
void opt1()
{
cout<<"Hello, World!"<<endl;
}
void opt2()
{
cout<<"Hello, World!"<<endl;
}
void opt3()
{
cout<<"Hello, World!"<<endl;
}
void opt4()
{
cout<<"Hello, World!"<<endl;
}
void opt5()
{
cout<<"Hello, World!"<<endl;
}


then to further shorten it we could just make the functions in a .h file and include that.
07-13-2013, 03:15 AM #3
Originally posted by Complete
this is really good and all, but it can be shorter and you used a lot of unnecessary includes.
i would have used. just this.
    
#include <iostream>
using namespace std;
void opt1();
void opt2();
void opt3();
void opt4();
void opt5();
void (*options[5])()=
{
opt1,opt2,opt3,opt4,opt5
};
int main()
{
int ans;
cout << "Welcome to the menu system\npress 1 -- say hello world!\npress 2 -- say hello word!\npress 3 -- say hello word!\npress 4 -- say hello word!\npress 2 -- say hello word!\nEnter Choice: ";
cin>>ans;
switch(ans)
{
case 1: options[0]();
system("pause>nul");
break;
case 2: options[1]();
system("pause>nul");
break;
case 3: options[2]();
system("pause>nul");
break;
case 4: options[3]();
system("pause>nul");
break;
case 5: options[4]();
system("pause>nul");
break;
return 0;
}
}
void opt1()
{
cout<<"Hello, World!"<<endl;
}
void opt2()
{
cout<<"Hello, World!"<<endl;
}
void opt3()
{
cout<<"Hello, World!"<<endl;
}
void opt4()
{
cout<<"Hello, World!"<<endl;
}
void opt5()
{
cout<<"Hello, World!"<<endl;
}


then to further shorten it we could just make the functions in a .h file and include that.


As I stated above "Also, I'm sure there is a easier way to code this, but I was bored so I was figuring some stuff out by the way I coded this :p"
07-20-2013, 07:19 PM #4
Originally posted by Complete
this is really good and all, but it can be shorter and you used a lot of unnecessary includes.
i would have used. just this.
    
#include <iostream>
using namespace std;
void opt1();
void opt2();
void opt3();
void opt4();
void opt5();
void (*options[5])()=
{
opt1,opt2,opt3,opt4,opt5
};
int main()
{
int ans;
cout << "Welcome to the menu system\npress 1 -- say hello world!\npress 2 -- say hello word!\npress 3 -- say hello word!\npress 4 -- say hello word!\npress 2 -- say hello word!\nEnter Choice: ";
cin>>ans;
switch(ans)
{
case 1: options[0]();
system("pause>nul");
break;
case 2: options[1]();
system("pause>nul");
break;
case 3: options[2]();
system("pause>nul");
break;
case 4: options[3]();
system("pause>nul");
break;
case 5: options[4]();
system("pause>nul");
break;
return 0;
}
}
void opt1()
{
cout<<"Hello, World!"<<endl;
}
void opt2()
{
cout<<"Hello, World!"<<endl;
}
void opt3()
{
cout<<"Hello, World!"<<endl;
}
void opt4()
{
cout<<"Hello, World!"<<endl;
}
void opt5()
{
cout<<"Hello, World!"<<endl;
}


then to further shorten it we could just make the functions in a .h file and include that.



using unmanaged c/c++ would be an even better approach.

printf( "Option 1: %s\nOption 2: %s\nOption 3: %s", "Hello World", "HW", "Hellow World" );
char c = getchar(); //upon enter
switch( c )
{
case '1': printf( "\n\nHello World" ); break;
case '2': printf( "\n\nHW" ); break;
... etc
default: break;
}
07-21-2013, 06:40 AM #5
Complete Speed
Do a barrel roll!
Originally posted by Jake625 View Post
using unmanaged c/c++ would be an even better approach.

printf( "Option 1: %s\nOption 2: %s\nOption 3: %s", "Hello World", "HW", "Hellow World" );
char c = getchar(); //upon enter
switch( c )
{
case '1': printf( "\n\nHello World" ); break;
case '2': printf( "\n\nHW" ); break;
... etc
default: break;
}

well my way was using array of function pointers which will help in functionality overall. allowing someone to do something like.

if (choice == 1){
functions[0]();
}
else if(choice == 2){
functions[1]();
}

where the given functions could be login, and edit profile or something along those lines.
07-21-2013, 08:11 AM #6
Originally posted by Complete
well my way was using array of function pointers which will help in functionality overall. allowing someone to do something like.

if (choice == 1){
functions[0]();
}
else if(choice == 2){
functions[1]();
}

where the given functions could be login, and edit profile or something along those lines.


Correct, but in terms of this project he doesn't need anything that advanced. Which is why I feel my answered suited best. Your answer would be most efficient for more extensive/difficult tasks.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo