Thread: Rock Paper Scissor HW Lab Code

  1. #1
    Registered User
    Join Date
    Jun 2014
    Location
    California
    Posts
    9

    Unhappy Rock Paper Scissor HW Lab Code

    DESCRIPTION: Chapter 3 Programming Project #1 on page 170Write a program to score the rock-paper-scissor game. Each of two users types in either R,P, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or Nobody wins. Be sure to allow the user to use lowercase as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he is done.

    My Code is attached, the instructions are there too if someone may help please!

    My problem is basically that it works yes, but it gives me results that dont involve who won or lost, so i'm missing some form of input. (theres a '}' at the end, didnt come out cuz screen to small to capture that also) Sorry to be a bit unspecific, it gives me this:
    player1: Enter r for rock, p for paper, or s for scissors: r
    player2: Enter r for rock, p for paper, or s for scissors:p
    Program ended with exit code: 0
    Nothing else with the code

    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main ()
    { string player1;
    string player2;
    string r;
    string p;
    string s;
    Attached Images Attached Images Rock Paper Scissor HW Lab Code-screenshot-2014-06-29-10-23-45-png 
    Last edited by Jose Vega; 06-29-2014 at 11:38 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the code as text, between code tags. We can't compile a picture.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2014
    Location
    California
    Posts
    9

    Smile

    Code:
     #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    int main ()
    {
    
    
        // Declare the Variables //
        
        string player1;
        string player2;
        string r;
        string p;
        string s;
        
        // Input the rules of Paper, Scissor, Rock //
        
        cout << "player1: Enter r for rock, p for paper, or s for scissors:";
        cin >> player1;
        cout << "player2: Enter r for rock, p for paper, or s for scissors:";
        cin >> player2;
        
        
        if (player1 == player2)
        {
            cout <<"There is a tie" <<endl;
        }
        
        if ( player1 == r && player2 == p)
        {
            cout << "Paper wraps rock, Player1 win";
        }
        else if (player1 == r && player2 == s)
        {
            cout << "Rock smashes scissors, player1 win";
        }
        
        if (player1 == p && player2 == r)
        {
            cout <<"Paper wraps rock, player1 win";
        }
        else if ( player1 == p && player2 == s)
        {
            cout <<"Scissors cut paper, player2 win";
        }
        
        if ( player1 == r && player2 == p)
        {
            cout << "Paper wraps rock, Player1 win";
        }
        else if (player1 == r && player2 == s)
        {
            cout << "Rock smashes scissors, player1 win";
        }
        
        if (player1 == p && player2 == r)
        {
            cout <<"Paper wraps rock, player1 win";
        }
        else if ( player1 == p && player2 == s)
        {
            cout <<"Scissors cut paper, player2 win";
        }
        
        if ( player2 == s && player1 == r)
        {
            cout <<"Scissors cut paper, player1 win";
        }
        else if (player2 == s && player1 == p)
        {
            cout <<"Rock smashes scissors, player2 win ";
        } 
        
        return 0;
    }
    Sorry but here it is! sorry new to this place lol

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You seem to be using the names of variables like they were values. You really should be using a constant in your comparisons.

    Since you're using strings something like:

    Code:
      if ( player1[0] == 'r' && player2 == "p")
    Note the difference between player1 and player two. With player1 I'm comparing just the first letter to the character constant 'r'. With player2 I'm comparing the whole word to the string constant "p".

    Also don't forget that C/C++ is case sensitive and single quotes denote a char and double quotes denote a string, 's' is not the same as 'S' or "s" or "S".

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2014
    Location
    California
    Posts
    9

    Cool

    OH MY GOD! this works!!! Thank you So Much! it now displays the rules and everything!

  6. #6
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Why not char's instead? (Use std::cin.get()). Or getline() with the strings and that way you can clear whatever else was left behind in the input stream so it doesn't mess up subsequent calls to std::cin.get(). Then index [0] to get the first character.
    Last edited by cstryx; 07-02-2014 at 08:47 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    chars are bad since they leave the newlines in the input buffer.
    However, I would agree that std::getline would be better since it will read everything the user has entered so far, so you won't get surprises later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by Elysia View Post
    chars are bad since they leave the newlines in the input buffer.
    However, I would agree that std::getline would be better since it will read everything the user has entered so far, so you won't get surprises later.
    Yes, that's why I like getline(), but he could compare using chars instead of strings too either way.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    chars are bad since they leave the newlines in the input buffer.
    The extraction operator leaves newlines in the input buffer, though It doesn't really matter what you read - newlines count as white space, and the extraction operator skips white space by default.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For a program like this I would getline() to get the input, then use either toupper() or tolower() on the first character of the string and compare that character to a constant char of the proper case. I try to avoid actually comparing strings entered by the user unless absolutely necessary. Users can be very unpredictable, and simple spelling errors, and capitalization issues are very hard to predict. But "usually" they can at least get the first letter correct (if you eliminate capitalization issues). Even if you tell them to enter a single character many times they will enter more than that single character, for example typing out "paper" instead.

    and the extraction operator skips white space by default.
    Unless you're retrieving a char, which by default the extraction operator doesn't skip leading ws.

    Jim

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by jimblumberg View Post

    Unless you're retrieving a char, which by default the extraction operator doesn't skip leading ws.

    Jim
    Doesn't seem to be true:
    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::string s = "\nreally?";
        std::istringstream input(s);
        char newlineMaybe;
        input >> newlineMaybe;
        std::cout<<newlineMaybe<<" "<<std::hex<<int(newlineMaybe)<<std::endl;
    }
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    r 72

  12. #12
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    123
    Quote Originally Posted by whiteflags View Post
    Doesn't seem to be true:
    Code:
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ cat foo.cpp
    #include <iostream>
    #include <sstream>
    #include <string>
    
    int main()
    {
        std::string s = "\nreally?";
        std::istringstream input(s);
        char newlineMaybe;
        input >> newlineMaybe;
        std::cout<<newlineMaybe<<" "<<std::hex<<int(newlineMaybe)<<std::endl;
    }
    
    Josh2@Josh-PC /cygdrive/c/users/josh2/desktop
    $ ./foo
    r 72
    You are correct, the sentry object for the istream defines the parameter for noskipws by default as false, meaning it skips whitespace, unless:
    Code:
    input_stream >> std::noskipws >> newlineMaybe;
    The key here is that by default whitespace IS in fact skipped, which is the opposite of what jim had stated.

    Code:
    class sentry {
    public:
      explicit sentry (istream& is, bool noskipws = false);
      ~sentry();
      operator bool() const;
    private:
      sentry (const sentry&);             // not defined
      sentry& operator= (const sentry&);  // not defined
    };
    The copy constructor and assignment operator are essentially 'deleted' using the old C++03 style of getting rid of them by marking them as privately accessible instead of public.

    Cheers
    Last edited by cstryx; 07-02-2014 at 10:40 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cstryx
    Or getline() with the strings and that way you can clear whatever else was left behind in the input stream so it doesn't mess up subsequent calls to std::cin.get(). Then index [0] to get the first character.
    Note that getline could store an empty string, so you should not use operator[] to access the first character by index until you have checked that it exists.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Note that getline could store an empty string, so you should not use operator[] to access the first character by index until you have checked that it exists.
    O_o

    The value at the offset may exist yet still be invalid--for whatever definition.

    One also needs to check if `std::getline' was successful.

    Soma

    Code:
    if(getline(/* ... */)) //...
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 11-14-2011, 12:29 PM
  2. Rock, Scissors, paper
    By myyke_myyke in forum C Programming
    Replies: 6
    Last Post: 08-25-2011, 01:26 PM
  3. Rock, Paper, Scissors
    By Slynet in forum C Programming
    Replies: 2
    Last Post: 02-13-2009, 07:14 PM
  4. Another rock paper scissor question
    By mattflick in forum C Programming
    Replies: 11
    Last Post: 09-29-2005, 09:41 PM

Tags for this Thread