Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,086 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,550 people online right now. Registration is fast and FREE... Join Now!




guessing game

 
Reply to this topicStart new topic

guessing game, Im fairly new to c++

killakev
4 Sep, 2008 - 06:40 AM
Post #1

New D.I.C Head
*

Joined: 1 Sep, 2008
Posts: 20

I want to write a simple code using iterations with selections on a dice roll guessing game. I want to have the program ask the user to guess the dice roll. How is this possible?
User is offlineProfile CardPM
+Quote Post

killakev
RE: Guessing Game
4 Sep, 2008 - 07:04 AM
Post #2

New D.I.C Head
*

Joined: 1 Sep, 2008
Posts: 20

Write a program that asks the user to guess the next roll of a six sided die. Each guess costs $ 1. If they guess correctly, they get $ 100.00. The player will start out with a $10.00 bank. Each time he (or she) guesses incorrectly you will subtract $1.00 from their bank. Each time they guess correct, you add $100.00 to their bank. Print the total winnings or loss at the end of the game. [Each die toss is simulated using die = (int)(rand() % 6) + 1;]

Your program should look something like this:

Your bank is $10.00
Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:

6
Sorry, the dice rolled a 5.
continue?

y

Your bank is $9.00
Please enter your guess for the next roll. It only costs $1.00 to play, If you are correct I will pay you $100.00:

4
Winner! the dice rolled a 4.
continue?
n
Thanks for playing your bank is $109.00. Come back real soon


basically this is what I have to do... Taking this class online soo this stuff is kind of difficult to follow... Thanks for the help
User is offlineProfile CardPM
+Quote Post

joske
RE: Guessing Game
5 Sep, 2008 - 04:57 PM
Post #3

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
What did you try so far?...

you have to:
- ask user for input
- process the input (role dice, adjust amount of money)
- output results
- write a loop to repeat when user wants to continue

User is offlineProfile CardPM
+Quote Post

red_4900
RE: Guessing Game
5 Sep, 2008 - 10:13 PM
Post #4

Code Dreamers
****

Joined: 22 Feb, 2008
Posts: 813



Thanked: 11 times
My Contributions
just do a minor tweak for your last code.

notice that I put the rolling dice part into another function to make it easier. this should do the trick :
cpp
#include <iostream>
#include <ctime>
using namespace std;

char die(char,int *);

int main(void)
{
int money = 10;
char exit='Y';

srand(time(0));

while(exit=='Y' || exit=='y'){
cout<<endl<<"Your bank is $" <<money<<endl;
exit = die(exit, &money);
};

cout<<endl<<"Thanks for playing your bank is $"<<money<<". Come back real soon"<<endl;

cin.get ();
return 0;
}

char die(char exit, int *money){
*money = *money-1;
int num;
int die;
die=(int)(rand()%6)+1;

cout<<endl<<"Please enter your guess for the next roll. It only costs $1.00 to play. If you are correct I will pay you $100.00:" <<endl;
cin>>num;

if (die == num)
{
cout<<"Winner. The dice rolled a "<<die<<endl;
*money += 100;
}
else if(die != num)
{
cout<<"Sorry. The dice rolled a "<<die<<endl;
}

cout<<"Press 'Y' to continue. Continue?"<<endl;
cin>>exit;
return exit;
}


you can ask here again if there's anything that you don't understand.

hope this helps you smile.gif

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:11PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month