Code review please.

My program is a guessing game where the user chooses a difficulty level and then is given a random number and must guess if the next number is higher or lower than the first. It must also keep track of how many times the user plays as well as how many correct guess they get. Not sure where to place a while or for loop.
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
100
101
102
103
104
105
106
107
108
109
110
  

#include <iostream>
#include <cstdlib> //directives
#include <ctime>
int main() {
    std::cout << "In this game you will guess a number after being shown a number." << std::endl;
    std::cout << "You will guess if the second number is higher or lower than the first number." << std::endl;

    std::cout << "Do you want to play?" << std::endl;


   std::cout << "Input e or E for an easy game, m or M for a medium difficulty, or h or H for the most difficult" <<std::endl;
   //take in lvl of difficulty



   char lvl_difficulty = x;
    std::cin >> lvl_difficulty;

    if(x=='e' || x=='E'){
     lvl_difficulty=10;
     }
    else if(x=='m' || x=='M'){
     lvl_difficulty=50;
     }
    else if(x=='h' || x=='H'){
     lvl_difficulty=100;
     }

 std::srand (time(NULL));
   //lvl_difficulty= will store e m or h
   //if the user input matches e or E x=10
  //else if if the user input matches m or M x=50

   //else if user input matches h or H x=100

   int randomNumberA = rand() % lvl_difficulty + 1;
   int randomNumberB = rand() % lvl_difficulty + 1;



   std::cout << "You must input y if you are ready." << std::endl;
        //take in yes or no  //every time they want to play tally the rounds using user_rounds=0, i++
     char y;
     std::cin >> y;

   if(y=='y' || y=='Y'){

     std::cout << "The first randomly selected number is " << randomNumberA << std::endl;
   }
   else if(y=='n' || y=='N') {
   } //if they input n or N don't want anything to happen
   else{
     std::cout << "Your choice is not understood. Please select again." <<std::endl;
   }

    std::cout << "Is the second number higher or lower than the first" << std::endl;



    char user_guess;//will hold if they guess high or lower
    int user_correct = 0;
    int user_attempt = 0;
    std::cin >> user_guess;
    //nest if statements

    if (user_guess=='h' || user_guess=='H'){
    if (randomNumberA  < randomNumberB) {
     user_correct++;
     user_attempt++;
   }
  }
    std::cout << "You guessed correctly! You win!" << std::endl;

    //within nested statements tally how many times there right user_correct=0, and their attempts
   if (user_guess=='h' || user_guess=='H'){
   if (randomNumberA > randomNumberB) {
    user_attempt++;
    }
   }
   std::cout << "I'm sorry, that is not correct. You lose." << std::endl;

if (user_guess=='l' || user_guess=='L'){
   if (randomNumberA > randomNumberB) {
     user_correct++;
     user_attempt++;
     }
    }
  std::cout << "You guessed correctly! You win!" << std::endl;



  if (user_guess=='l' || user_guess=='L'){
  if (randomNumberA < randomNumberB){
    user_attempt++;
    }
   }
   std::cout << "I'm sorry, that is not correct. You lose." << std::endl;



   std::cout << "You played " << user_attempt << "and won " << user_correct << "of those rounds" << std::endl;

 return 0;
 }
 //cout user_correct and user_attempt


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
#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {

    std::cout << "In this game you will guess a number after being shown a number.\n\n"
                 "You will guess if the second number is higher or lower than the first number.\n"
                 "Do you want to play (y/n)? " ;

    char yn = 'n' ;
    std::cin >> yn ;
    if( yn != 'y' && yn != 'Y' ) return 1 ;

    std::cout << "Input e or E for an easy game,\n"
                 "      m or M for a medium difficulty,\n"
                 "   or h or H for the most difficult: " ;
    char level ;
    std::cin >> level ;

    int ubound = 50 ; // default is medium
    if( level == 'e' || level == 'E' ) ubound = 10 ;
    else if( level == 'h' || level == 'H' ) ubound = 100 ;
    // else ubound remains as 50

    std::srand( std::time(nullptr) ) ;

    int num_guesses = 0 ;
    int num_correct = 0 ;

    do {

        const int first = std::rand() % ubound + 1;
        int second = std::rand() % ubound + 1;
        // make sure that first is not equal to second
        while( second == first ) second = std::rand() % ubound + 1;

        ++num_guesses ;

        std::cout << "\n\nThe first number is " << first
                  << "\nIs the second number higher or lower than the first (H or L)? " ;
        char hl = 'l' ;
        std::cin >> hl ;

        if( ( hl == 'h' || hl == 'H' ) &&  second > first )
        {
            ++num_correct ;
            std::cout << "you guessed correctly! the second number is higher.\n" ;
        }

        else if( ( hl == 'l' || hl == 'L' ) &&  second < first )
        {
            ++num_correct ;
            std::cout << "you guessed correctly! the second number is lower.\n" ;
        }

        else std::cout << "that is not correct.\n" ;

        std::cout << "\nthe second number was " << second << '\n' ;

        std::cout << "once again (y/n)? " ;
        std::cin >> yn ;

    } while( yn == 'y' || yn == 'Y' ) ;

    std::cout << "\nYou played " << num_guesses << " times, and won "
                               << num_correct << " of those\n" ;
}
Topic archived. No new replies allowed.