Why doesnt this work

This is a discussion on Why doesnt this work within the C++ Programming forums, part of the General Programming Boards category; Hey, im back again. I got a little time to program and so i decided to write a rock paper ...

  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,317
    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
    #include<xErath.h> xErath's Avatar
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    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
    Jan 2005
    Posts
    7,317
    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,317
    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
    181
    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, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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