5 Replies - 1195 Views - Last Post: 26 November 2012 - 11:56 AM Rate Topic: -----

#1 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

coin flip

Posted 21 November 2012 - 11:09 AM

#include <iostream>  
#include <ctime>  

 using namespace std;  

 int main ()  
 {  

    int flip; 
    int guess;
	
	
		
		cout << "How many times do you think heads will appear in a row?" << endl << endl;
	 	cin >> guess;
	
	    cout << "How many times would you like to flip a coin? ";  
	    cin >> flip; 
	     
		srand(time(0));  

		for(int i=0; i<flip; i++)  
		{  

		   int coin = rand()%2;  

		   if(coin == 1)  
			cout<<"TAILS"<<endl;  
           else 
		    cout<<"HEADS"<<endl;  

        }  
}





In this game I have to have the player guess how many times they think heads will appear in a row. The coin needs to flip until I get my first HEADS and then stop flipping once a TAILS appears. The problem is that it keeps flipping even if I get a TAILS disregarding the userís guess.

Is This A Good Question/Topic? 0
  • +

Replies To: coin flip

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,737
  • Joined: 19-March 11

Re: coin flip

Posted 21 November 2012 - 11:21 AM

All this is going to do is get a number from the user and loop through the coin flip that many times.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: coin flip

Posted 21 November 2012 - 11:23 AM

Quote

The coin needs to flip until I get my first HEADS and then stop flipping once a TAILS appears. The problem is that it keeps flipping even if I get a TAILS disregarding the userís guess.

So where are you trying to stop when you get "Tails"?

I would also suggest you work on one project at a time. Finish one then move on to the others.


Jim
Was This Post Helpful? 1
  • +
  • -

#4 aresh  Icon User is online

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • Posts: 4,163
  • Joined: 08-January 12

Re: coin flip

Posted 21 November 2012 - 11:25 AM

If it needs to stop flipping after a tails, then you must put that condition in your program. Do you think the computer is smart enough to guess your intentions? No, it is not. You can either put that as a condition in the for loop or use break.

Quote

I would also suggest you work on one project at a time. Finish one then move on to the others.

This.

This post has been edited by aresh: 21 November 2012 - 11:27 AM

Was This Post Helpful? 0
  • +
  • -

#5 lateshapowell3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 17-October 12

Re: coin flip

Posted 26 November 2012 - 11:44 AM

View Postaresh, on 21 November 2012 - 11:25 AM, said:

If it needs to stop flipping after a tails, then you must put that condition in your program. Do you think the computer is smart enough to guess your intentions? No, it is not. You can either put that as a condition in the for loop or use break.

Quote

I would also suggest you work on one project at a time. Finish one then move on to the others.

This.


#include <iostream>  
#include <ctime>  
#include<string>

 using namespace std;  

 int main ()  
 {  

        int flip; 
	int guess;
	
	char x;
	
	
	double bet();//takes in a bet
	double wager;

		
		cout << "How many times do you think heads will appear in a row?" << endl << endl;
	 	cin >> guess;

		flip = (rand() % 6) + 1;
	
	   
	        srand(time(0));

		for(int i=0; i<flip; i++)  
		{  

		   int coin = rand()%2;  

		   if(coin == 1)  
		    cout<<"TAILS"<<endl;  
                   else 
		    cout<<"HEADS"<<endl;  

        }  

		cin >> x;

			
				return 0;  
 
}





Okay for the coin toss I would do something like

flip = (rand() % 6) + 1;

to have the coin flip randomly, but then I have to place in a line of code to stop when there is a TAILS only after we at least got one HEADS, should I call tails something or declare it as something so when a tails pops up it will stop flipping. What I am trying to say is can I use some type of variable to assign as TAILS and HEADS in order for it to recognize TAILS when it appears and when it should stop exactly
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,737
  • Joined: 19-March 11

Re: coin flip

Posted 26 November 2012 - 11:56 AM

Here's your program as a dialog. Try implementing this:

Quote

"Hey, I'll bet you a dollar this quarter will come up heads three times in a row"

"Okay: you haven't flipped it yet, so number of times we've seen heads is zero, and we haven't seen a tails yet either."

"That's right, now I'm going to flip it. And it came up heads"

"Okay, so now number of times we've seen heads is 1, which is less than 3 and we haven't seen a tails yet, so we keep flipping."

"And it came up heads again. "

"Okay, so now number of times we've seen heads is 2, which is less than 3 and we haven't seen a tails yet, so we keep flipping."

"Rats! This time it came up tails."

"Okay, so now number of times we've seen heads is 2, which is less than 3 but now we have seen a tails, so we stop flipping."

"And because we have seen a tails during that run, I owe you a dollar".


Hint: when there's two conditions, I usually find it easier to use a while loop instead of a for loop.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1