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.
#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;
}
elseif(x=='m' || x=='M'){
lvl_difficulty=50;
}
elseif(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;
}
elseif(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
#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 ;
elseif( 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 {
constint 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" ;
}
elseif( ( 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" ;
}