Guess the number

Just made a CMD guess the number game, would like some constructive criticism or evaluation.

Thanks in advance, Actionman1995
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
97
98
99
#include <cstdlib>
#include <iostream>

using namespace std;

int computerGuess(int high, int low)
{
    return ((high-low)/2)+low;
}

int generateNumber()
{
    srand(time(0));
    return rand()%100;
}

int displayMenu()
{
    int value;
    cout << " Welcome to Guess the Number\n"
         << " Please Choose a Game Type: ";
         cout << "\n\n";
         cout << "1) Human Guessing\n"
              << "2) Computer Guessing\n"
              << "3) Exit\n\n"
              << "Your choice: ";
         cin >> value;
    return value;
}

int main(int argc, char *argv[])
{
    
    int menu, guess, num, counter = 10;
    
        system("CLS");
        menu = displayMenu();
        system("CLS");
        
    if (menu == 1)
     {
        num = generateNumber();
        do
        {
            cout << "Your Guess is: ";
            cin >> guess;
                if (num>guess)
                {
                    cout << "The number is higher\n\n"
                         << "You have " << counter-- << " attempts left\n\n";
                }
                else if (num<guess)
                {                     
                    cout << "The number is lower\n"
                         << "You have " << counter-- << " attempts left\n\n";
                }
                else
                {
                    cout << "Well done, you got it right!\n\n";
                    break;
                }
        }
        while (counter>=0);
      }
      else if (menu==2)
      {
           int high, low;
           char ans;        
           low = 1;
           high = 100;
          do
          {
               cout << "If the computers guess is too high type in [L],\n"
                       "if the computer guess is too low type in [H]\n"
                       "if the answer is correct typr in [Y].\n\n"
                       "Computers Guess: ";
               num = computerGuess(high, low);
               cout << num << "\nLow: " << low << "\nHigh: " << high << "\n";
               cin >> ans;
               
               switch(ans)
               {
                  case 'l':
                  case 'L':
                     high=num;
                     break;
                  case 'h':
                  case 'H':
                     low=num;
                     break;
                  default:
                     cout << "Well done computer!\n";
               }
          }
          while (counter>=0);
      }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Don't use system("PAUSE");. cin.get(); is lighter and does not pose potential safety issues like the Systeem call
In order to expand upon spaggy's previous remark, you can check out the following thread:

http://cplusplus.com/forum/beginner/1988/
Avoid sing namespace std;, http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5

You should only call srand(time(0)); once, not every time you want a random number.

You might want to create a function for each menu option.

Some people would use a switch for the menu selection because its a finite int based decision.
Topic archived. No new replies allowed.