How to add a menu to Number Guessing Game

OS: Windows.

1) Best way to add a menu in my program's format. Loops or Switch statements? How to proceed?
2) Why doesn't #include <cwindows> work?
3) Using the for loop to clear the screen seems more roundabout than system ("CLS") but decided to forgo system codes. Is there an easier solution than using for loops?
4) If using for loop to clear the screen how to make cursor on the top?

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
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <ctime>

using namespace std;

void set_color (unsigned short color);

int main ()
{
    char yes_no = 'y';
    srand (time(0));

    while (yes_no == 'y' || yes_no == 'Y')
    {
       int random_number = rand () % 10 + 1;
       int user_guess = 0;
       int total_guesses = 0;
       int guesses_left = 3;

       cout << " ----------  Number Guessing Game  ----------- " << endl << endl << endl;

       set_color (11);
       cout << "Try to guess my secret number between 1 - 10." << endl << endl;
       cout << "You will have 3 guesses." << endl << endl;

       set_color (6);
       cout << endl << "Begin..." << endl << endl;
       set_color (7);

       while (user_guess != random_number)
       {
           total_guesses ++;
           guesses_left --;

           cin >> user_guess;
           cout << endl;

           if (user_guess < random_number)
            {
               cout << "Guess higher..." << endl << endl;
               cout << "You have " << guesses_left << " guess(es) left." << endl;
               cout << "--------------------------------------" << endl << endl << endl;
            }

            else if (user_guess > random_number)
            {
               cout << "Guess lower..." << endl << endl;
               cout << "You have " << guesses_left << " guess(es) left." << endl;
               cout << "--------------------------------------" << endl << endl << endl;
            }

            else
            {
                set_color (12);
                cout << endl << "Great job! You guessed my number in " << total_guesses << " guess(es)." << endl << endl;
                set_color (7);
                break;
            }

            if (guesses_left == 0)
            {
                set_color (5);
                cout << "Sorry, you lose. The correct number was " << random_number << "." << endl << endl;
                set_color (7);
                break;
            }
       }

          cout << "Play again? (Y or N): ";
          cin >> yes_no;

          for (int i = 0; i < 50; i ++)
          {
              cout << endl;
          }
    }

     set_color (10);
     cout << endl << "Goodbye for now." << endl << endl;
     set_color (7);

     for (int i = 0; i < 18; i ++)
     {
        cout << endl;
     }

    return 0;
}

void set_color (unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute (hcon, color);
}
Last edited on
Hi,

1) Best way to add a menu in my program's format. Loops or Switch statements? How to proceed?


umm... your choice. Although I would prefer a switch inside do-while loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
{
cout<<"GAME MENU:";
//random game choice 1
//random game choice 2
//.. and so on..

switch(choice)
{
//switch statements
}

while(choice!=0)//0 for exit from menu

)
}


2) Why doesn't #include <cwindows> work?


I think its windows

It depends on your IDE or some other stuff

further reading: http://www.cplusplus.com/forum/general/107306/
https://en.wikipedia.org/wiki/Windows.h

3) Using the for loop to clear the screen seems more roundabout than system ("CLS") but decided to forgo system codes. Is there an easier solution than using for loops?


well system ("CLS") is not universal but the for loop is not the ideal solution. Stick system ("CLS")

further reading: http://www.cplusplus.com/articles/4z18T05o/

4) If using for loop to clear the screen how to make cursor on the top?


Again no universal way to do so, but if you use system("cls") you won't be needing that...
But still if you want you could use gotoxy (for old IDE's) or SetConsoleCursorPosition (may need API)
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686025(v=vs.85).aspx

HOPE IT HELPS

PS: welcome to cplusplus.com

PPS: could you tell us what IDE are you using?
Last edited on
I'm using Code::Blocks.

Thanks for all of your help. I'll practice switches before implementing that into my program. Since everybody experienced says using system <anything> is bad I'll try to avoid it, even though it makes the code look cleaner. I've already read up on SetConsoleCursorPosition; I'll try that when I feel I'm ready. I tried stuff like -endl but nothing simple worked. Thanks again.
welcome :)
Topic archived. No new replies allowed.