User color change

Hey I am trying to get a submenu where the user is able to simply input a character or two and change the console color. The problem right now is that it ignores the mainmenu and and the cin at the color menu and simply starts changing the color itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
int main()
{
system("color 02");


system("cls");

char mainMenuChoice, choicePage1, choicePage2, choicePage3, choicePage4, choicePage5;
char quit_exe, apps, shutoff, colors;

mainmenu:
system("cls");
cout << " TAT Main Menu                       TAT 1.5.3" << endl;
cout << " _______________________________________________" << endl;
cout << " + Type 'menu' at any time if you wish to      +" << endl;
cout << " +  return to this menu.                       +" << endl;
cout << " +                                             +" << endl;
cout << " + Type 'quit_exe' at any time to exit.            +" << endl;
cout << " +_____________________________________________+" << endl;
cout << " + Type 'apps' to view batch applications.     +" << endl;
cout << " +_____________________________________________+" << endl;
cout << " + Type 'shutoff' to shut down your PC.        +" << endl;
cout << " +_____________________________________________+" << endl;
cout << " + Type 'colors' to view alternate colors.     +" << endl;
cout << " +_____________________________________________+" << endl << endl;
    time_t tim;  //create variable of time_t
    time(&tim); //pass variable tim to time function
    cout << ctime(&tim); // this translates what was returned from time() into a readable format
cout << " _________________________________" << endl << endl;
cin >> mainMenuChoice;
if (mainMenuChoice = quit_exe)
goto exit;
//if (mainMenuChoice = apps)
//goto apps;
if (mainMenuChoice = shutoff)
goto sd;
if (mainMenuChoice = colors)
goto color;
else
cout << " Invalid selection, try again." << endl;
system("pause");
goto mainmenu;

sd:
char shut_down_confirm, y, n;
system("cls");
cout << " Really shut down your computer?  y/n" << endl << endl;
cin >> shut_down_confirm;
if (shut_down_confirm == y)
cout << "TAT recieved a request to turn off this PC." << endl;
system("shutdown -s -f -t 10 -c");
if (shut_down_confirm == n)
goto mainmenu;
else
cout << " Invalid selection." << endl;
system("pause");
goto sd;

color:
char color, A, B, C, D, E, F;
char front, back;
cout << " Choose from the following text colors:" << endl << endl;
cout << " 1=blue            9=light blue" << endl << endl;
cout << " 2=green           A=light green" << endl << endl;
cout << " 3=aqua            B=light aqua" << endl << endl;
cout << " 4=red             C=light red" << endl << endl;
cout << " 5=violet          D=light violet" << endl << endl;
cout << " 6=yellow          E=light yellow" << endl << endl;
cout << " 7=white           F=brigth white (best for contrast)" << endl << endl;
cout << " 8=gray" << endl << endl;
cin >> color;
SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ) , color );
system("pause");
system("cls");
goto mainmenu;

exit:
system("pause");
return 0;
}

Ugh...there are some many problems with that code...

1.) You are using goto's in the worst way possible...you are trying to use them like they are functions when they aren't...

2.) system()...don't use it...it is evil

3.) Some beginner errors, you are using '=' in your if() when you need '=='

'=' is the assignment operator
'==' is the comparison operator
I do not understand the goto and system() part but as for the = vs == I could not get it to work with ==. So what is wrong with goto and system() and what are the alternatives?
You are not initializing any of the variables you are checking.

char quit_exe, apps, shutoff, colors;

You need to define these to something, i.e. quit_exe = 'q' ...

For goto's, you can simply use loops/functions, read on this here:
http://www.cplusplus.com/doc/tutorial/control/
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/

For system, look here:
http://www.cplusplus.com/forum/articles/7312/
http://www.cplusplus.com/forum/articles/10515/
Well now I'm even more confused. But I did switch from dev c++ to code::blocks. I read those articles and I still don't see what is wrong with goto and system commands. I'm sorry I am causing so much trouble. They just seem so much simpler and cleaner and just all around better.

Also I do not understand what you mean by defining them "quit_exe ='q'" I thought that was the point of char. So if I set quit_exe to q then how do I use it, have the user input q instead I am just really confused now.
Last edited on
Ahhhhhhhhhh! I am so confused now. I don't have a clue even what to do now because of what you said I though I had it figured out and now nothing makes sense!
Don't give up. It is confusing at first.

Everything following a label should be a function. For example, you have a mainmenu: label. Turn it into a function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// I like enumerations because they keep code clean
enum menuitem_t
  {
  mi_menu,
  mi_quit,
  mi_apps,
  mi_shutoff,
  mi_colors
  };

// This function displays the main menu, gets valid input from the user, and returns
// the enum value of the selected menu item.
menuitem_t mainmenu()
  {
  // Please avoid the use of system(). See below for where I got replacements for CLS and PAUSE.
  ClearScreen();

  cout << " TAT Main Menu                       TAT 1.5.3" << endl;
  cout << " _______________________________________________" << endl;
  cout << " + Type 'menu' at any time if you wish to      +" << endl;
  cout << " +  return to this menu.                       +" << endl;
  cout << " +                                             +" << endl;
  cout << " + Type 'quit_exe' at any time to exit.        +" << endl;
  cout << " +_____________________________________________+" << endl;
  cout << " + Type 'apps' to view batch applications.     +" << endl;
  cout << " +_____________________________________________+" << endl;
  cout << " + Type 'shutoff' to shut down your PC.        +" << endl;
  cout << " +_____________________________________________+" << endl;
  cout << " + Type 'colors' to view alternate colors.     +" << endl;
  cout << " +_____________________________________________+" << endl << endl;

  time_t tim;  //create variable of time_t
  time(&tim); //pass variable tim to time function
  cout << ctime(&tim); // this translates what was returned from time() into a readable format

  cout << " _________________________________" << endl << endl;

  // I personally like to give the user an input prompt, to let him know that he is expected
  // to type something and press the enter key.
  //
  // The loop is to make sure he enters something valid.
  //
  while (true)
    {
    cout << "menu> " << flush;

    string s;
    getline( cin, s );

    if (s == "menu")    return mi_menu;
    if (s == "quit")    return mi_quit;
    if (s == "apps")    return mi_apps;
    if (s == "shutoff") return mi_shutoff;
    if (s == "colors")  return mi_colors;

    cout << "Invalid response. Try again (type 'menu' for help).\n";
    }
  }

To replace system("CLS") see here: http://www.cplusplus.com/forum/articles/10515/
To replace system("PAUSE") see here: http://www.cplusplus.com/forum/articles/7312/
To replace system("COLOR X") use:
1
2
3
4
5
6
7
void Color( int color )
  {
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    color
    );
  }

You should, however, try to write your programs to avoid using any of these functions.

Do the same kind of thing for all the functions of your program.
Now, you can just call the appropriate function from main():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
  {
  Color( 0x02 );
  ClearScreen();

  while (true)
    switch (mainmenu())
      {
      case mi_menu:    /* do nothing */         break;
      case mi_quit:    PressEnterToContinue();  return 0;
      case mi_apps:    apps();                  break;
      case mi_shutoff: shutoff();               break;
      case mi_colors:  colors();
      }
  }

I think I touched everything I needed to. Hope this helps.
Last edited on
Topic archived. No new replies allowed.