Validate Input, Fix Increment, and Bool Issue

Hi, so I'm in my beginner C++ class and we are supposed to write a program that is a high-low guessing game. It asks the user to guess a random number between 1-100 and the user makes a bet from their initial $1000 and has 6 guesses before they lose. We are supposed to be using the multiple functions in this assignment and we were told to use this specific formula to create the random number. I have two main questions...
1.In the GetBet function, I was unsure of how to validate input. When you type in a bet between 0 and the amount of money you have, it returns the input properly. If you type in a single character, it asks the user to reenter a valid bet. However, if you enter a string of characters or a large negative value (abcd or -568745), it accepts it as a valid bet. So is there a better way to check for incorrect input?
2.At the end of the program, it asks the user if they want to play another round(restart main). I tried to apply a do-while loop, but it didn't make the loop restart. Any help with this problem?
Thanks!
EDIT: I was able to implement the do-while loop correctly.
But now, when the loop restarts, GuessNumber is still incremented(so it starts off at 6 instead of 1) and I guess won is still set to true because it pops up after the first(wrong) guess.
So now, I just need to figure out how to validate the information that is input in the GetBet function. And figure out how to fix the above...thanks!

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>		//for standard I/O 
#include <ctime>		//needed to access the computer's clock
#include <cstdlib>		//necessary for rand() and RAND_MAX

using namespace std;

void PrintHeading(int, int, int);	//prototype for printing initial heading
int GetBet(int);			//prototype for getting bet(input) from user
int DrawNum (int);			//prototype for function that generates random number
int GetGuess(int, int);			//prototype for getting guesses from user

int main()
{
	const int START_BALANCE = 1000,	//user initially receives $1000
		  MIN = 1,		//minimum number that can be guessed
		  MAX = 100;		//maximum number that can be guessed
	int betAmount,			//amount that the user wants to bet
	    numDrawn,			//the random number from the computer
	    guess,			//the guess from the user
	    winnings,			//the amount that the user won after the round
	    money = START_BALANCE,	//the user's money balance(initialized with $1000)
	    guessNumber = 0,		//number of the guess(1-6)
	    gamesWon = 0,		//how many games the user has won
	    totalGames = 0;		//total games the user has played
	unsigned int seed;		//seeds rand()
	bool won = false;		//initializes won as false
	double percentageWon;		//calculates the percentage of games won out of total played
	char playAgain;			//user inputs Y/N about whether they want to play another round
	
	//call function that prints overall heading
	PrintHeading(money, MIN, MAX);	

	/*assign the return of the GetBet function to betAmount
	and changes user's money balance to reflect their bet*/
	betAmount = GetBet(money);				
	money = money - betAmount;				
	
	/*obtain value for the time from the computer's clock and with 
	it call srand to initialize "seed" for the rand() function*/
	seed = static_cast<unsigned>(time (NULL));
	srand (seed);
	
	//get number drawn by computer
	numDrawn = DrawNum(MAX);

	do //do loop
	{
	guessNumber++;									//increment the number of guesses
	cout << endl << "Please enter guess #" << guessNumber << ": ";	//prints the prompt to gain user's guess
	guess = GetGuess(MIN, MAX);										//assigns return from GetGuess function to guess
		if(guess < numDrawn)										//if the user's guess is less than the computer's number
		cout << "Try again, that guess is too low!" << endl << endl;
		else if(guess > numDrawn)									//if the user's guess is more than the computer's number
		cout << "Try again, that guess is too high!" << endl << endl;
		else if(guess == numDrawn)									//if user guesses the computer's number		
		{
			won = true;										//reset won to true
			cout << "That's the correct number!" << endl << endl;	//print winning statement
		}
		else														//changes any invalid guesses to "too low"
			cout << "Try again, that guess is too low!" << endl << endl;
	} //do loop
	while (won != true && guessNumber < 6);					//while user's guess is incorrect and guessnumber is under 6

	if(won) //if won is true
	{
		gamesWon++;	//increment games won
		totalGames++;	//increment total games
		winnings = betAmount / guessNumber;					//initialize winnings to specific formula
		money += winnings;						//add any winnings to user's money balance
		percentageWon = 100.0 * (gamesWon/totalGames);//initialize percentage won with formula
		cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl
			 << "You won this round!" << endl			//print closing message telling winning statistics
			 << "You have won $" << winnings << " in this round." << endl
			 << "You now have $" << money << " total." << endl
			 << "You have won " << percentageWon << "% of the games you have played." << endl
			 << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
	} //if won is true
	else	//else
	{
		totalGames++;									//increment total games played		
		percentageWon = 100.0 * (gamesWon/totalGames);	//percentage of games won out of total
		cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl	//print closing message telling losing statistics
			 << "Sorry! The correct number was " << numDrawn << "." << endl
			 << "You lost $" << betAmount << " in this round." << endl
			 << "You now have $" << money << " total." << endl
			 << "You have won " << percentageWon << "% of the games you have played." << endl
			 << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl << endl;
	}

	cout << "Would you like to play another round? Enter Y/N: ";	//ask the user if they want to play again
	cin >> playAgain;												//read input (Y/N) from user

	if(playAgain != 'Y' || playAgain != 'y')	//if playAgain is not yes
	{											//print final message
		cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl
			 << "Thanks for playing the High-Low Betting Game!" << endl
			 << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl << endl;
	
	return 0;	//return zero
}

void PrintHeading(int balance, int min, int max)	//function that prints the initial heading of game
{	//PrintHeading function
	cout << "===============================================" << endl
		 << "* Welcome to the High-Low Betting Game!\t      *" << endl
		 << "* You have $" << balance << " to begin the game.\t      *" << endl
		 << "* Valid guesses are numbers between " << min << " and " << max << ".*" << endl
		 << "===============================================" << endl;
	return;
}	//PrintHeading function

int GetBet(int balance)								//function that prompts and obtains bet from user
{	//GetBet function
	int bet;	

	cout << "Please enter the amount of money you want to bet: ";
	cin >> bet;						//bet is input from user
	if(bet > 0 && bet <= balance)	//if bet is valid(between zero and total money balance
		return bet;					//return the bet to main
	else							//if input is invalid
	{
		cout << "That is not a valid bet!" << endl
			 << "Please enter a valid bet: ";	//prompt for a new valid bet
		cin.clear();			
		cin.ignore(numeric_limits<streamsize>::max(),'\n');
		cin >> bet;
	}
}	//GetBet function

int DrawNum(int max)				//function that calculates random number
{	//DrawNum function
	double x = RAND_MAX + 1.0;		//x and y are both auxiliary 
	int y;							//variables used to do the 
									//calculation 

	y = static_cast<int> (1 + rand() * (max / x));
	return (y);						//return random number to main
}	//DrawNum function

int GetGuess(int min, int max)		//function that gets user's guess
{	//GetGuess function
	int userGuess;

	cin >> userGuess;				//user inputs guess
	
	if (userGuess >= min && userGuess <= max)
		return userGuess;			//if guess is between 1 and 100, return it to main
}	//GetGuess function 
Last edited on
Topic archived. No new replies allowed.