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 . . .

Dave Sinkula commented: Post *your attempt*, not simply your homework assignment's requirements. -2
tux4life commented: Double poster! -1
Kole12 commented: UGH! +0

i have just finished done the code.
using while loop, setprecison, if statement and others

but i hope with some effort from you,
i will give you the code. but it is wise to try first...
i (and all the other people) will be pleased to help you!

commented: No, do NOT GIVE your code to him. Ever! Help him code it. -2

hi

i am trying this for last 4-5 days i have made the guessing section with the help of rand() command which generates random number between 1 to 10 ,but i dont know how to do the other part like deduction amount from the bet amount and i am getting lot of errors in that,if u can help than it will be great .

Thanks for giving time to my post......

@ salem ( Sorry if i did something wrong)

This is not my homework problem ,i am preparing for one exam in for the position of junior software engineer in one of the company ,so i am trying to learn programmimg as much possible,iam just a beginer

include <iostream>
#include <cstdlib>
#include <ctime>
 
using namespace std;

int main()
{
	srand(time(0));
	int randomNumber = rand() % 10 + 1; 	int guess = 0;
	
	cout << "\tThe Number Guessing Game\n\n"; 
do
	{
		cout << "Enter your guess (#1-10): ";
		cin >> guess; 
 
		if (guess < randomNumber) // If guess is < than the random number, display
			cout << "Your guess was too low\n\n"; 
 
		if (guess > randomNumber) // If guess is > than the random number, display
			cout << "Your guess was too high\n\n";
 
	} while (guess != randomNumber); // While guess is not equal to the random number

this is what i did so far ,but i dont know how to incorporate amount and this for this part i have no idea ((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!) )).

Please use the code tags

if (guess < randomNumber) // If guess is < than the random number, display
           cout << "Your guess was too low\n\n";

if (guess > randomNumber) // If guess is > than the random number, display
           cout << "Your guess was too high\n\n";

You did not read the specification too well. It says they want the output to match what they have and there is nothing about higher/lower there.

The do/while is a step in the right direction. Except you should read the conditions they lay out for you about what happens when the balance is zero or the user enters a zero bet.

The rest is a bunch of if statements. You need to account for a user trying to bet more than they have and trying to bet a negative number. Use two double (or float if you prefer) variables balance and bet. Adjust balance from bet according to the specification they gave you.

I think you need to sit down with it and spend some time. Break it down into pieces. You've already got a lot of the I/O down. Give it a genuine go and post back with any problems.

but i hope with some effort from you,

i am encouraging our friend here, coolfriend to post and try himself....
and yes, i know that if i give my code directly to him, he will never learn. i will not simply just give him the code just like that.

> This is not my homework problem ,i am preparing for one exam in for the position of junior software engineer in one of the company ,
> so i am trying to learn programmimg as much possible,iam just a beginer
Unless this is an absolutely lowest rung of the ladder position, for a student or apprentice, then you're wasting everyone's time.
At the moment (and for many months to come), you're going to need a lot of hand-holding. Which is fine if that is what the company is expecting of you.

But if you're trying to blag some more senior position "on the fly", then you're going to become unstuck in a hurry.

Fact is, these kind of questions are dead easy for people who are capable of doing the job. The problems you will get in the job will be vastly harder and more complex to solve, and it is there that you will realise just how far out of your depth you'll be.

i am encouraging our friend here, coolfriend to post and try himself....
and yes, i know that if i give my code directly to him, he will never learn. i will not simply just give him the code just like that.

Thanks for replying-:
Well..its not like that i m not trying ,i tried the part of guessing random numbers ,but i really dont know how to do the other part of betting and deduction from betting and how to incorporate in that ,so if u can give code ,than i can learn that and if i come across any such problems in future ,i will be absolutely fine in doing that..

Let's make sure this is clear: no one is going to give you the code. We've given you a few suggestions as to how you should proceed with the betting part of the code. Salem is right, this is a toy problem designed for early undergraduate students. Make an attempt at writing the betting portion and then we might be willing to talk about it.

This is what i made till now ..but i am not getting desired result desired result

please correct this

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

using namespace std; 

int main() 
{
	cout<<" Guessing Game:"<<endl;
	cout<<"Written by :"<<endl;
	srand(time(0));	
	int randomNumber = rand() % 10 + 1; 	
	int guess = 0;
	int balance=0;


	int bet=100;

	cout<<"Guess a number between 1 and 10:"<<endl;
    cin>>guess;
	cout<<"Please enter your bet (up to $100.00):"<<endl;
	cin >>bet;

	do
	{
		cout << "Guess a number between 1 and 10: ";		
		cin >> guess; 
		if (guess==randomNumber)
		{
			cout << "You win:"<<endl;
			balance=balance+bet;
		}
		else
		{
			cout<<"You lose:"<<endl;
			balance=balance-bet;
		}
	}while (guess != randomNumber);
	cout << "Wrong! The computer chose: " <<randomNumber << " You Lose " << " guesses.";
          
          
    return 0;

}

OK. I corrected the CODE tags. Now what?

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.