Thread: Why doesnt this work

  1. #1
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91

    Why doesnt this work

    Hey, im back again. I got a little time to program and so i decided to write a rock paper scissors program.

    I am not sure what is wrong with it but it closes after the user inputs somthing. I think i needs it to return a value

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int rock();
    int paper();
    int scissors();
    
    int main()
    {
        int select;
        
        cout<< "Welcome to Rock, Paper, Scissiors Version 1.0";
        cout<< "\nInstructions:\n";
        cout<< "\nType ( 1 ) to cast rock\n";
        cout<< "Type ( 2 ) to cast paper\n";
        cout<< "Type ( 3 ) to cast paper\n";
        cout<< "\nYour choice :  ";
        cin>> select;
        switch (select)
        {
              case 1:
                   rock();
                   break;
              case 2:
                   cout<<"asdf";//paper();
                   break;
              case 3:
                   cout<<"ahrt";//scissors();
                   break;
              default:
                      cout<< "\nThat was not a selection!\n";
                      break;
                      }
        cin.get();
      }
      
    int rock()
    {
          int compchoicerock;
          
          compchoicerock = rand() % 3 + 1;
          
          if ( compchoicerock == 1 ){
               cout<< "\nThe computer also chooses rock.\n";
               cout<< "The round ends in a draw.";
               }
         
          else if( compchoicerock == 2 ){
               cout<< "\nThe computer chooses paper, and paper covers rock so you lose!\n";
               }
         
          else if( compchoicerock == 3 ){
               cout<< "\nThe computer chooses scissors, rock smashes scissors so you win!";
               }
    
    }
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Two possible problems. First, you are using cin.get() to keep the window open, but since you've added user input with operator>> the cin.get() is getting the leftover newline from the user input. The simple answer is to add a cin.ignore(); before the cin.get(). The better answer is to search the forums, FAQs, or internet for the better answer.

    The second problem might be that you don't flush the output. This may or may not be causing a problem, but to be safe, consider changing cout << "\n"; to cout << endl; which adds the '\n' to the output and flushes the output to the console.

  3. #3
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    rather than use cin.get() or even cin << to keep your program open, you could implement system("PAUSE");.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Jaken Veina
    rather than use cin.get() or even cin << to keep your program open, you could implement system("PAUSE");.
    ... bad...
    system calls a command line interpreter - big overhead
    pause can be a malicious program
    tons of better alternatives...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Quote Originally Posted by Salem
    That particular FUA and the one I (barely) saw it link to don't actually help fix the problem. If there is one that tells how to ignore the newline in the stream that would be better.

  7. #7
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Code:
    switch (select)
        {
              case 1:
                   rock();
                   break;
              case 2:
                   cout<<"asdf";//paper();
                   break;
              case 3:
                   cout<<"ahrt";//scissors();
                   break;
              default:
                      cout<< "\nThat was not a selection!\n";
                      break;
                      }
        cin.ignore();
        cin.get();
      }
      
    int rock()
    You need to insert a cin.ignore(); before your cin.get(); like I have shown. Then when the user inputs their guess, the window stays open.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  8. #8
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Quote Originally Posted by xErath
    ... bad...
    system calls a command line interpreter - big overhead
    pause can be a malicious program
    tons of better alternatives...
    Ahh.....I apologize. I'll keep that in mind. Thank you for pointing it out.

  9. #9
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    ok that sloved the problem of the window closing, but now i am having trouble with the rand part
    Code:
    void rock()
    {
          int compchoicerock;
          
          compchoicerock = rand() % 3+1 ;
          
          if ( compchoicerock == 1 ){
               cout<< "\nThe computer also chooses rock.\n";
               cout<< "The round ends in a draw.";
               }
         
          else if( compchoicerock == 2 ){
               cout<< "\nThe computer chooses paper, and paper covers rock so you lose!\n";
               }
         
          else if( compchoicerock == 3 ){
               cout<< "\nThe computer chooses scissors, rock smashes scissors so you win!\a\a\a";
               }
    
    }
    if i leave the rand as it is i always win, but if i take away the +1 I always lose.
    i havent looked at the FAQ yet, but i read rod's tutorial on rand but i didnt understand it

    can someone explain please and thank you
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Seed the random number generator with a call to srand(). The easiest way is to include <ctime> and call srand(time(0)); once at the very beginning of your main function. This will give it a different seed each time, otherwise the random numbers generated will always be the same.

  11. #11
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    wait explain a little bit more. I changed rand so srand and add #include <ctime> but now it gives me errors and the compiler opens up stdlib.h every time i run the debugger
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    try out this code

    Code:
    int rock()
    {
          int compchoicerock;
          
          srand(time(0));
          compchoicerock = rand() % 3;
          if ( compchoicerock == 0 ){
               cout<< "\nThe computer also chooses rock.\n";
               cout<< "The round ends in a draw.";
               }
         
          else if( compchoicerock == 1 ){
               cout<< "\nThe computer chooses paper, and paper covers rock so you lose!\n";
               }
         
          else if( compchoicerock == 2 ){
               cout<< "\nThe computer chooses scissors, rock smashes scissors so you win!";
               }
    
    }
    i hope this will work fine for you

    Code:
    rand() % 3
    when u you this rand number starts from 0 to 2 - not more than 3 -. so when 'if conditions' are between these number the program works fine.

    s.s.harish

  13. #13
    Pokemon Master digdug4life's Avatar
    Join Date
    Jan 2005
    Location
    Mystic Island, NJ
    Posts
    91
    thank you all very much
    Verbal Irony >>

    "I love english homework!" When really nobody like english homework.
    -Mrs. Jennifer Lenz (English Teacher)

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Quote Originally Posted by xErath
    ... bad...
    system calls a command line interpreter - big overhead
    pause can be a malicious program
    tons of better alternatives...
    Guidelines for safely using system(). Not super convenient, but it works.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM