CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: a few questions

  1. #1
    Join Date
    Apr 2010
    Posts
    60

    a few questions

    i am creating a game of Rock, paper, Scissors,

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string a;
    int b;
    int main()
    {
        cout << "Welcome to Rock, Paper, Scissors\n";
        cout << "To play, Simply type R,P or S\n";
        cout << "\n";
        system("PAUSE");
        system("CLS");
     //Game Begins
     
       cout << "Your turn, R,P or S?\n";
       cin >> a;
       
       if (a == "R")
          {cout << "You chose Rock..\n";}
       else if (a == "P")
          {cout << "You chose Paper..\n";}
       else if (a == "S")
          {cout << "You chose Scissors\n";}
       else
          {cout << "Please enter R,P or C only\n";}
       system("PAUSE");
       
          
        
        
        
    }
    First off, How can i get the code to Start again after "Please enter R,P or C only" so it dosent have to restart?,


    And, How can i make a random number generator, that randomly picks one number, from 3, so 1,2,or 3,?

  2. #2
    Join Date
    Sep 2010
    Posts
    9

    Re: a few questions

    For the program to repeat itself, you should put the whole thing inside of a while loop which runs until a variable (I prefer using the word "end" as the variable name) changes to 1, like so:

    Code:
    int main ()
    {
         int end;
         int everything_else;
         while ( end == 0 )
         {
              blah blah blah
              if ( a == "R" || a == "P" || a == "S" )
                   end = 1;
         }
         return 0;
    }
    This will have the program continually loop until the input is either R, P, or S. Oh, and its better to have the input looked for be lower case, just because nobody's going to want to hold shift and most will try the program without putting it in caps.

    For the random number, your going to need something like srand() (http://www.cplusplus.com/reference/c...cstdlib/srand/), though thats not completely random (they refer to it as pseudo-random the whole time), and actually tends towards the lower part of the numbers your trying to get as output and has predictable output.

    Hope this helped! :P

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: a few questions

    Quote Originally Posted by momrocker View Post
    For the program to repeat itself, you should put the whole thing inside of a while loop which runs until a variable (I prefer using the word "end" as the variable name) changes to 1, like so:

    Code:
    int main ()
    {
         int end;
         int everything_else;
         while ( end == 0 )
         {
              blah blah blah
              if ( a == "R" || a == "P" || a == "S" )
                   end = 1;
         }
         return 0;
    }
    This will have the program continually loop until the input is either R, P, or S. Oh, and its better to have the input looked for be lower case, just because nobody's going to want to hold shift and most will try the program without putting it in caps.
    You're right, but stylistically, I'd use a bool. Don't forget to initialize it, and watch the and/or logic there.


    Code:
    int main ()
    {
         bool end = false;
         int everything_else;
         while (!end )
         {
              blah blah blah
              if ( a != "R" && a != "P" && a != "S" )
                   end = true;
         }
         return 0;
    }

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: a few questions

    Quote Originally Posted by GCDEF View Post
    You're right, but stylistically, I'd use a bool. Don't forget to initialize it, and watch the and/or logic there. ...
    I nomally don't use a variable in the while condition but use an infinite loop and break the loop when the error condition occurs:

    Code:
    int main()
    {
    
        while (true)   
        {
             ...
             if (input == 'q')   // break the loop when the user types q for quit.
                  break;
        }
        return 0;
    }
    Though the above seems to be more dangerous it actually is less error-prone cause the break would work immediately while the check on 'end' variable works at begin of next loop cycle.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: a few questions

    It's undisputable that rand() isn't well-suited for cryptographic applications, but this didn't let me sleep well:

    Quote Originally Posted by momrocker View Post
    [...] and actually tends towards the lower part of the numbers your trying to get as output [...].
    I don't think that it actually tends anywhere, as illustrated by the attatched image, sampled from the VC++ 2010 implementation of rand().
    Attached Images Attached Images
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured