Need Help on Coin Toss Program

This is a discussion on Need Help on Coin Toss Program within the C++ Programming forums, part of the General Programming Boards category; need to use the random number generator I am using in the code. need it to display the results I ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Need Help on Coin Toss Program

    need to use the random number generator I am using in the code.
    need it to display the results I have listed under void for the result of the coin toss.
    and then need to make it ask if I want to flip again(which is not included yet).
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>
    using namespace std;
    
    char AskForInput();
    void DisplayHead();
    void DisplayTail();
    int result;
    
    
    
    int main()
    {
    	srand(static_cast<unsigned int>(time(0)));  
        int HeadOrTails = rand(); 
    	int result = (HeadOrTails % 2) + 1;
       
    	int Heads=1;
        int Tails=2;
    	char AskForInput;
    	 cout << "\n\nEnter your guess: ";
         cin>> AskForInput;
       cout<<"You guessed:"<<AskForInput;
    	
       cout<<"\nResult is:"<<result;
       if (result=1)
       {
    	   cout<<"\nThe Result is Heads";
       }
       if (result=2)
       {
    	   cout<<"\nThe Result is Tails";
       }
       
    
    void DisplayHeads();
    	 {
    		 cout<<"\n        _.-'~~`~~'-._         \n";
    	     cout<<"       .'`  B   E   R `'       \n";
    		 cout<<"      / I             T |;     \n";
    		 cout<<"    /`        .-'~''-.    ;    \n";
    		 cout<<"   ; L       / `-    \    Y ;   \n";
    		 cout<<"  ;         />  `.  -.|     ;  \n";
    		 cout<<"  |        /_     '-.__)    | \n";
    		 cout<<"  |         |-  _.' \ |       | \n";
    		 cout<<"  ;         `~~;   \\\\       ; \n";
    		 cout<<"  ;   INGODWE /    \\\\)P   ;  \n";
    		 cout<<"   (   TRUST '.___.-'`''   /   \n";
    		 cout<<"    `(                  /`    \n";
    		 cout<<"      '._   1 9 9 7  _.'     \n";
    		 cout<<"         `'-..,,,..-'`        \n";
    	 
    	 }
    void DisplayTails();
    	 {
          cout<<"\n        _.-'~~`~~'-._         \n";
    	  cout<<"       .'`A     R     `'     \n";
    	  cout<<"      / U       __    T |;     \n";
    	  cout<<"     /`        /'{>       ;    \n";
    	  cout<<"    ; Q    ____) (____   E ;   \n";
    	  cout<<"   ;     //'--;   ;--'))  R ; \n";
    	  cout<<"   |    ///////[_]))))))    | \n";
    	  cout<<"   |           m m          | \n";
    	  cout<<"   ;          TAILS         ; \n";
    	  cout<<"    ;D                      ;  \n";
    	  cout<<"     (                     R/   \n";
    	  cout<<"     '( O                A/`    \n";
    	  cout<<"       '._   L  L      _.'     \n";
    	  cout<<"          `'-..,,,..-'`        \n";
    	 }
    	
    
    return 0;
    }
    my Question is:
    How do I get it to take the result of the random number generated(the toss )
    and use it in the coding to let it display a message.
    "example"
    Code:
    if(result=AskForInput)
    {
      cout<<"You Guessed Correctly";
    and then how do I let it display the "DisplayHeads" / "DisplayTails" after it checks for the result?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Code:
       if (result=1)
       {
    	   cout<<"\nThe Result is Heads";
               if(AskForInput == 'h')
                 cout << "\nYou guessed correctly!";
       }
    I'm guessing you can figure out the rest from there.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    so I do have make case 'h' and a case 't'?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    if (result=1)

    I tried doing such but it would just always say correct without saying incorrect.
    It's like it counted 2 as result = 1 or something

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Well there's a difference between
    if ( result = 1 )
    and
    if ( result == 1 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    and then need to make it ask if I want to flip again(which is not included yet).

    what value do I return it to? or how would I loop it to make it repeat

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    Well you might consider making what you have so far in main into
    Code:
    void flipOneCoin ( void ) {
    }
    Then make your main into something else, which calls flipOneCoin() in a loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Code:
    if (result=1)
       {
    	   cout<<"\nThe Result is Heads";
               if(AskForInput == 'h')
                 cout << "\nYou guessed correctly!";
       }
    This was posted earlier I tried it and it will go through the code giving result the value of 1 and 2

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Well you might consider making what you have so far in main into

    Code:
    void flipOneCoin ( void ) {
    }
    Then make your main into something else, which calls flipOneCoin() in a loop.

    Can you also explain this more thoroughly?
    mainly atm I'm having problems with result equaling both 1 and 2 so I can't let it distinguish.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,742
    > I'm having problems with result equaling both 1 and 2 so I can't let it distinguish.
    Well I already told you about = vs. ==, yet you still carry on using =
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Quote Originally Posted by Blazefury5 View Post
    Code:
    if (result=1)
       {
    	   cout<<"\nThe Result is Heads";
               if(AskForInput == 'h')
                 cout << "\nYou guessed correctly!";
       }
    This was posted earlier I tried it and it will go through the code giving result the value of 1 and 2
    Heh. That's what I get for copying and pasting. It should obviously be result==1 instead.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Figured it out works perfectly
    needed (result "==" 2) etc.
    and needed a closing declaration to close int main()
    before the void functions
    and got the loop to work as you mentioned above, was having problems looping it because I figured the program had to end after the voids
    Ty for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help coin program.
    By Kinto in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2009, 09:00 PM
  2. coin toss
    By ejd81882 in forum C Programming
    Replies: 3
    Last Post: 10-14-2008, 01:53 PM
  3. Coin Toss
    By kazuakijp in forum C Programming
    Replies: 4
    Last Post: 08-31-2004, 10:02 PM
  4. coin toss program?
    By girliegti in forum C++ Programming
    Replies: 4
    Last Post: 09-17-2003, 11:09 AM
  5. coin change program??
    By kelly in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 08:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21