please correct this------the desired output is in the bottom

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

using namespace std; 

int main() 
{

		int randomNumber=0;
		
	 	
		int guess = 0;
		int balance=100;
		int amount=0;
		int bet=0;
		double balanceLeft;
		
		

		cout<<" Guessing Game:"<<endl;
		cout<<"Written by coolfriend:"<<endl;

do
		{

		
		

	cout<<"Please enter your bet$100.00):"<<endl;
		cin >>bet;

		srand((unsigned)time(0)); 
		for(int index=0; index<RAND_MAX; index++)
		{ 
		randomNumber = (rand()%10)+1; 
				
		} 


cout << "Guess a number between 1 and 10: "<<endl;		
		cin >> guess;

		if(guess!=randomNumber)
		{
			
cout<<"Wrong!The computer chose:"<< randomNumber<<endl;
	balanceLeft=(bet/5)*abs(randomNumber-guess);
	cout << "You Lose $:"<<balanceLeft<<endl;
	balance=balance-balanceLeft;
	cout<<"Your new balance is $"<<balance<<endl;
			
		else if(guess==randomNumber)

		{				 
					 cout<<"Correct!"<<endl;
balance=bet;
cout<<"You win $"<<balance<<endl;
balance=balance+bet;
cout<<"your new balance is $"<<balance;
		}
}
}while(guess==0)
return 0;

}

Plan and code a guessing and betting number game, as described by the text below and the sample output but do not write any functions (other than main).

An appropriate welcome message with your name in it should be shown at the start of the program. The user starts with 100 dollars, and is able to bet and guess until they quit or have no more money. In each turn, they are asked for a bet. If they enter 0, then the program should give a farewell message and quit. The bet must be positive and within their available balance, but do not assume they will enter correctly. If they enter incorrectly, the program should set the bet to their balance and tell them this.

Once a bet is entered, they must pick a number between 1 and 10 (inclusive) (you do not need to error-check or correct this). This guess is then compared to a number the computer randomly generates each time (also between 1 and 10).

If the guess is correct, the user wins the amount of their bet. If the guess is incorrect, the user will lose their bet divided by 5 and multiplied by the distance from the correct number - e.g. if the bet is 50, the computer’s number is 5 and the user guesses 7, then the user will lose 20 dollars. However, they should not lose more than their balance, so if their balance is 50, the bet is 40, the computer’s number is 1 and the user guesses 10, then they should lose 50. (Don’t think this game has to be fair!)

After each turn, the program should display that turn's winnings (or loss amount) and the new balance, and then repeat the game until the user either quits or has no money left.

Dollar values should be formatted in the standard way with two decimal places and a $ in front. Sample output from the program is below. You should make your program match this exactly

Sample Output (this forms part of the requirements):
Guessing Game
Written by Lindsay Ward

Please enter your bet (up to $100.00): $50

Guess a number between 1 and 10: 5

Correct!
You win $50.00

Your new balance is $150.00

Please enter your bet (up to $150.00): $200
Your bet is $150.00

Guess a number between 1 and 10: 6

Wrong! The computer chose: 4
You lose $60.00

Your new balance is $90.00

Please enter your bet (up to $90.00): $80

Guess a number between 1 and 10: 1

Wrong! The computer chose: 7
You lose $90.00

Thank you for playing!
Press any key to continue . . .

Another Sample:

Guessing Game
Written by Lindsay Ward

Please enter your bet (up to $100.00): $-20
Your bet is $100.00

Guess a number between 1 and 10: 5

Wrong! The computer chose: 2
You lose $60.00

Your new balance is $40.00

Please enter your bet (up to $40.00): $10.50

Guess a number between 1 and 10: 12

Wrong! The computer chose: 2
You lose $21.00

Your new balance is $19.00

Please enter your bet (up to $19.00): $0
Thank you for playing!
Press any key to continue . . .

Recommended Answers

All 5 Replies

Please use code tags. It keeps it neater and preserves your formatting so we can read the code.

Make balance and bet doubles also, as what if I lost 20.50 in the process or wanted to bet $4.50?

cout<<"Please enter your bet$100.00):"<<endl;
//hint this line should be updated for each pass through
[/code]
I don't think there's much point in doing this
[code]
for(int index=0; index<RAND_MAX; index++)
{
      randomNumber = (rand()%10)+1;
} 
//just remove the for loop
[/code]
[code]
balance=bet;
cout<<"You win $"<<balance<<endl;
balance=balance+bet;
//so if you've won $800 so far and your bet was $20.00, after you've //won you're balance would be $40.00??

I'm sure there might be other stuff but I can't have all the fun. The important question to ask is "does my output match up with the sample output exactly?"

give me some hint how can i code
1"that if the lost amount is greater than balance left than balance only becomes 0 and not go into negative values."?
2"after winning how i can increment my bet amount which was 100 in the starting"?

"that if the lost amount is greater than balance left than balance only becomes 0 and not go into negative values."?

Your lost amount is balanceLeft (which I think you should rename to loss or something because it doesn't reflect your balanceLeft at all)
So, if (your loss is greater than balance) make the balance 0 else balance equals balance minus loss.

The incorrect thing you did in the second one was setting balance equal to bet. There's no rhyme or reason for doing that. As I had said that's like saying if you've won 800 so far and bet 25 and win and I suddenly switch your 800 for 50 instead.

You need to check how many times your do/while is running and read again the criteria under which it is supposed to stop.

@jonsca

Thank you for replying....

this is code i modified after ur suggestions

still it is not giving desired result can u please what else in wrong in that

MY CODE

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

using namespace std; 

int main() 
{

		int randomNumber=0;
		
	 	
		int guess = 0;
		double balance=100;
		int amount=0;
		double bet=100;
		double loss;
		
		

		cout<<" Guessing Game:"<<endl;
		cout<<"Written by :"<<endl;

		do
		{

		
		
		
	cout<<"Please enter your bet (up to $"<<bet<<")"<<endl;
		cin >>bet;

		srand((unsigned)time(0)); 
		for(int index=0; index<RAND_MAX; index++)
		{ 
			randomNumber = (rand()%10)+1; 
				
		} 


	cout << "Guess a number between 1 and 10: "<<endl;		
		cin >> guess;

		if(guess!=randomNumber)
		{
			
cout<<"Wrong!The computer chose:"<< randomNumber<<endl;
	loss=(bet/5)*abs(randomNumber-guess);
	cout << "You Lose $"<<loss<<endl;
	balance=balance-loss;
	cout<<"Your new balance is $"<<balance<<endl;
				
			if(loss>balance)
			{ 
				balance=0;
			}
			else
			{
				balance=balance-loss;
			}
		}
		else
			
			{
		cout<<"correct!"<<endl;
		balance=balance+bet;
		cout<<"You win $"<<balance<<endl;

			
		
		}
				
			
			
			}while(balance!=0);
				
	
	
          
          
    return 0;

}

I'm not commenting on anything more until you put code tags on it. I showed you how, there's no excuse.

commented: You tell 'em! :) +18
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.