Alright I know you dont help on homework, but I have don't so much I just need a little guidance on the ending. I just need to make my number guessing game ask the user if they would like to play again and keep there bank.

The assignment:
http://www.glennstevenson.com/c++online/syllabus/midtermfall2008/midterm.html

Heres my code so far:

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
char play;
int guess;
bool done;
int noOfGuesses = 0;
int random;
int c;
double bank, winnings, total;
int check;

void printPayout()
{
				cout << "1 guess, win 2.00" << endl;
				cout << "2 guesses, win 1.75" << endl;
				cout << "3 guesses, win 1.5" << endl;
				cout << "4 guesses, win 1.25" << endl;
				cout << "5 guesses, win 1.00" << endl;
				cout << "6 guesses, .75" << endl;
				cout << "7 guesses, win .5" << endl;
				cout << "8 guesses, win .25" << endl << endl;
}

int checkGuess(int guess)
{
	if (guess > random) 
		{
			check = 1;
		} 
	else if (guess < random) 
		{
			check = -1;
		}
	else 
		{
			check = 0;
		}

    return (check);
}

int genRandom()
{
	int r;
	srand(time(NULL));
	r = rand() % 100 + 1;

return r;
}


int game()
{
	cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
	cout << "Do you want to play (y / n)" << endl;
	cin >> play;
		if (play=='y')
		{
		bank = 100.00;
		winnings = 2.00;
		cout << "Great!, your payout will be as follows:" << endl << endl;
		printPayout();
		cout << "Now guess a number between 1 and 100" << endl;
		random = genRandom();
		while ((noOfGuesses < 10))
		{
		cin >> guess;
		noOfGuesses++;
		c = checkGuess (guess);
		if (c == 0)
		{
	total = bank + winnings;
cout << "Correct, you win " << winnings << ", your bank is now "<< total<< endl;
		}
		else if (c == -1)
		{
		cout << "Sorry too low try higher" << endl;
				winnings = winnings - .25;
		}
		else if (c == 1){
		cout << "Sorry too high try lower" << endl;
		winnings = winnings - .25;
		}
		}
	}
return 0;
}


int main() 
{

	game();

	return 0;
}

I think your game should run until the user runs out of money!!

So You should be having 2 while loops.

while((bank!=0)&&(play=='y'))
{
noofGuesses=0;
random = genRandom();
		while ((noOfGuesses < 10))
		{
		cin >> guess;
		noOfGuesses++;
		c = checkGuess (guess);
		if (c == 0)
		{
	total = bank + winnings;
cout << "Correct, you win " << winnings << ", your bank is now "<< total<< endl<<"Do You Wanna Play Again??(Y/N)";
cin>>play;
break;
		}
		else if (c == -1)
		{
		cout << "Sorry too low try higher" << endl;
				winnings = winnings - .25;
		}
		else if (c == 1){
		cout << "Sorry too high try lower" << endl;
		winnings = winnings - .25;
		}
}cout<<"Do You want to play again?(Y/N)"<<endl;
cin>>play;
}

I have made 2 corrections to the code.

Firstly The game should run until the player wants to exit or money runs out. So the game should continue HENCE the first while loop makes sure of that.

For that we have to ask the user whether he wants to continue. When he wins or when he runs out of chances.
The Do you want to play again statements and the cin statements make sure of that.

And another adjustment is the inclusion of noofplays to zero everytime the new game is called upon. By doing all these you will have a game that runs fine .

Thanks alot :)

hmm there still seems to be a little problem. Heres how it plays out.

Welcome to the guess-o-matic. It only costs a dollar to play. You could double y
our bet.

Do you want to play (y / n)
y
Great!, your payout will be as follows:

1 guess, win 2.00
2 guesses, win 1.75
3 guesses, win 1.5
4 guesses, win 1.25
5 guesses, win 1.00
6 guesses, .75
7 guesses, win .5
8 guesses, win .25

Now guess a number between 1 and 100
34
Sorry too high try lower
24
Sorry too high try lower
12
Sorry too low try higher
18
Sorry too high try lower
16
Sorry too high try lower
14
Correct, you win 0.75, your bank is now 100.75
Do You Wanna Play Again[y/n)
y
Sorry, you lose0.75, your bank is now 100.75
Do You Wanna Play Again[y/n)
y
34
Sorry too low try higher
34
Sorry too low try higher

Right here idk why but it is repeats itself when you type y. I have been trying to fix it, but I am still stuck.

Correct, you win 0.75, your bank is now 100.75
Do You Wanna Play Again[y/n)
y
Sorry, you lose0.75, your bank is now 100.75
Do You Wanna Play Again[y/n)

Also I realized that I need it to show this code again. I moved the get random number up and tried to change it, but I still cant seem to get it right. Maybe someone can help again

cout << "Great!, your payout will be as follows:" << endl << endl;
				printPayout();
				cout << "Now guess a number between 1 and 100" << endl;
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
char play;
int guess;
bool done;
int noOfGuesses = 0;
int random;
int c;
double bank, winnings, total;
int check;

void printPayout()
{
				cout << "1 guess, win 2.00" << endl;
				cout << "2 guesses, win 1.75" << endl;
				cout << "3 guesses, win 1.5" << endl;
				cout << "4 guesses, win 1.25" << endl;
				cout << "5 guesses, win 1.00" << endl;
				cout << "6 guesses, .75" << endl;
				cout << "7 guesses, win .5" << endl;
				cout << "8 guesses, win .25" << endl << endl;
}

int checkGuess(int guess)
{
	if (guess > random)
		{
			check = 1;
		}
	else if (guess < random)
		{
			check = -1;
		}
	else
		{
			check = 0;
		}

    return (check);
}

int genRandom()
{
	int r;
	srand(time(NULL));
	r = rand() % 100 + 1;

return r;
}


int game()
{
	cout << "Welcome to the guess-o-matic. It only costs a dollar to play. You could double your bet." << endl << endl;
	cout << "Do you want to play (y / n)" << endl;
			bank = 100.00;
	cin >> play;
		if (play=='y')
		{
		winnings = 2.00;
		cout << "Great!, your payout will be as follows:" << endl << endl;
		printPayout();

		random = genRandom();
		while((bank!=0)&&(play=='y'))
{
noOfGuesses=0;
random = genRandom();
winnings = 2.00;

		while ((noOfGuesses < 10))
		{
cout << "Now guess a number between 1 and 100" << endl;
		cin >> guess;
		noOfGuesses++;
		c = checkGuess (guess);
		if (c == 0)
		{
	bank = bank + winnings;
cout << "Correct, you win " << winnings << ", your bank is now "<< bank<< endl<<"Do You Wanna Play Again??(Y/N)";
cin>>play;
break;
		}
		else if (c == -1)
		{
		cout << "Sorry too low try higher" << endl;
				winnings = winnings - .25;
		}
		else if (c == 1){
		cout << "Sorry too high try lower" << endl;
		winnings = winnings - .25;
		}
}
}
	}
	cout<<"Thank You For Playing, you are left with a total of " <<bank<<" $\n Wishing you will be back to play once more.";
return 0;
}


int main()
{

	game();

	return 0;
}

Sorry there were a few mistakes with my code. But i sorted them out and now its doing fine.

The next time you program, You should Try drawing a flow chart to get how the process should work out.

They were some basic mistakes that we needed to come back to.

YOU SHOULD EXAMINE YOUR PREVIOUS CODE AND THE CURRENT CODE . Understand what was missing and the method by which it was solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.