Thread: The char, string thing

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    The char, string thing

    Im trying to familiarize myself with the string thing, char, comparing strings, ect. by writing a program that allows a user to play rock paper scissors against a computer who obviously is going to choose randomly...so my original goal was to code this game in the fewest lines of code possible and use the string compare to compare the computer and user choices and decide who wins, and then little things like play 3 rounds and allow the user to play again if he wants, but I can pretty much take care of that. However, as with most projects, I figured out the beginning and just when i get to the most important part I get stuck.

    This is what I've gotten so far

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string.h>
    #include <cstdlib>
    
    using namespace std;     
    
    void main()
    {
        //initialize variables for intro screen
        char yesOrno[10] = "";
        int checkLoop = 0;
        char roundnum[10]="";
        int validChoice=0;
        char playerPut[10]= "";
        int computerPut=0;
    
    //while loop is set to different values to identify whether to repeat the intro
    screen again or not
        while(checkLoop == 0){                   
        
    //intro screen - ***** Just ignore all the cout stuff below this, I had it all set up and lined up to show the directions but when I pasted the code under here it messed up the spacing.
    
            cout << "                                                                            
    " << endl;
            cout << "            Rock          Paper         Scissors                            
    " << endl;
            cout << "
    __________________________________________________________________________  " <<
    endl;
            cout << "| This is a game of rock, paper, scissors. You will play three rounds     
    | " << endl;
            cout << "|  against the computer. To pick either rock, paper, or scissors
    simply    | " << endl;
            cout << "|  type in your choice and hit enter. The computer will randomly pick
    its  | " << endl;
            cout << "|  choice. At the end of the game type y to play again & n to end the
    game.| " << endl;
            cout << "|               Do you understand?  please enter y or n                   
    | " << endl;
            cout <<
    "|__________________________________________________________________________| "
    << endl; 
            
    //The end of that messy screen
    
    
    //user input
    cin.getline(yesOrno, 10);
        
    if((strcmp(yesOrno,"n")==0)||(strcmp(yesOrno,"y")==0))            //if the user input is
    a y or n go into this other loop
        {    
            if(strcmp(yesOrno,"y")==0)
                {
                    checkLoop=1;                                    
                    system("CLS");                        
                    cout<< "Get ready to start the game"<<endl;
                    }
                else                                                //if the user input is no repeat the directions
                
                {
                    system("CLS");
                    cout<< "Read the directions again";
                    checkLoop=0;                                    //makes this loop repeat until y or n entered
                }    
                    }     
    else
        { 
             system("CLS");            
             cout<< " Enter a y or n "<<endl;                        //if the user enters something other
    than a y or n print this statement                                        
             cout<<endl;
             cout<<endl;    
             }}
    
    system("CLS");
    
    cout<<"Enter your choice"<<endl;
    cin.getline(playerPut,20);
    
    system("CLS");
        
    
    while(validChoice==0)
    {
    
    if((strcmp(playerPut,"paper")==0)||(strcmp(playerPut,"rock")==0)||(strcmp(playerPut,"scissors")==0))
        { 
            validChoice=1;
            cout<< "Your choice was:"<<endl;
            cout<< playerPut<< endl;
            
            computerPut=rand()4%;
            
    //and this right here is pretty much where I got stuck, but i attempted to slowly drudge on    
            
    if(strcmp(playerPut,"paper")==3)
    
                cout<< "works"<< endl;
                         
            }        
        }
        else
        {
            system("CLS");
            cout<<"Not a valid choice."<< endl;
            validChoice=0;
            }
         
    }
    
    
        
    }

    Any suggestions that would help me fix this up with be greatly appreciated..

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by RenderedAwake
    Code:
            
    //and this right here is pretty much where I got stuck, but i attempted to slowly drudge on    
            
    if(strcmp(playerPut,"paper")==3)
    
                cout<< "works"<< endl;
                         
    ........
    If you're checking to see if the user entered "paper", then you should check to see if 0 is returned... strcmp() would return a number > 0 only if the original string was longer than the one being compared to it.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Krak
    If you're checking to see if the user entered "paper", then you should check to see if 0 is returned... strcmp() would return a number > 0 only if the original string was longer than the one being compared to it.

    The only things guaranteed about strcmp() are:

    1. It returns 0 if the two are equal.

    2. If the first is less than the second (byte-by-byte char comparison) it will return a negative number.

    3. If the first is greater than the second it will return a positive number.

    For example
    Code:
    #include <iostream>
    #include <cstring>
    
    using std::cout;
    using std::endl;
    int main()
    {
      char *s = "scissors";
      char *p = "paper";
      cout << "strcmp(p, s) = " << strcmp(p, s) << endl;
      cout << "strcmp(s, p) = " << strcmp(s, p) << endl;
      return 0;
    }
    With my Microsoft compiler the output is
    strcmp(p, s) = -1
    strcmp(s, p) = 1
    With Borland and GNU g++ on my system the output is
    strcmp(p, s) = -3
    strcmp(s, p) = 3
    But since this is C++, why not just use std::string for the variables (instead of char[] or char *) and then compare the strings directly (instead of using strcmp()). Both methods will work, of course, if applied correctly.

    Regards,

    Dave

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    first of all, thanks for your help so far i think ive gotten ive figured this out for the most part, I just need help with two things. (ill paste my code below)

    Number one, Ive got the intro screen to this paper rock scissors game to work and i think the mechanics of the game (best of three rounds) are worked out, but I cant seem to figure out where to put the CLS system statement, so that only the most recent user input and computer choice are shown. I tried putting it all over and seems whenever i put it ends up cutting out something..im looking to also add a counter that shows the computer and players score.

    Number two, I want to have an option that allows the user to play again..do i just do the whole game in a huge loop equal to 0 then when the user inputs yes or no have it switched to 1?...

    .any other suggestions on how to improve the game or shorten the code.??

    Code:
    #include <iostream.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define ROCK (0)
    #define PAPER (1)
    #define SCISSORS (2)
    
    int convert_choice(const char *choice)
    {
    if (!strcasecmp(choice,"rock")) return ROCK;
    if (!strcasecmp(choice,"paper")) return PAPER;
    if (!strcasecmp(choice,"scissors")) return SCISSORS;
    return -1;
    }
    
    const char *print_choice(int choice)
    
    {
    //gives word value to the random numbers
    static char rock[] = "rock", paper[] = "paper", scissors[] = "scissors", unknown[] = "unknown";
    switch (choice){
    case 0: return rock;
    case 1: return paper;
    case 2: return scissors;
    default: return unknown;
    }
    }
    
    int computerchoice(void)
    {
    return rand()%3; // gives value of either 0,1 or 2
    }
    
    void who_wins(int compchoice, int playerchoice)
    //Evaluates all the cases
    {
    switch (compchoice){
    case ROCK:
    switch (playerchoice){
    cout<< "                         "<<endl;
    case SCISSORS:
    cout<<endl<<"The computer wins"<<endl;
    break;
    case PAPER:
    break;
    cout<< "                         "<<endl;
    case ROCK:
    break;
    }
    break;
    cout<< "                         "<<endl;
    case PAPER:
    switch (playerchoice){
    case ROCK:
    break;
    cout<< "                         "<<endl;
    case SCISSORS: 
    break;
    cout<< "                         "<<endl;
    case PAPER:
    break;
    }
    break;
    case SCISSORS:
    switch (playerchoice){ 
    case ROCK:
    break;
    cout<< "                         "<<endl;
    case PAPER:
    break;
    cout<< "                         "<<endl;
    case SCISSORS:
    break;
    }
    break;
    }
    }
    
    int main()
    {
    char playerchoice[20];
    int compchoice;
    
    
    
    {
        //initialize variables for intro screen
        char yesOrno[10] = "";
        int checkLoop = 0;
        char roundnum[10]="";
        int validChoice=0;
        char playerPut[10]= "";
        char computerPut[10]=" ";
    
    //while loop is set to different values to identify whether to repeat the intro screen again or not
        while(checkLoop == 0){                   
        
            //intro screen
            cout << "                                                                             " << endl;
            cout << "            Rock          Paper         Scissors                             " << endl;
            cout << " ____________________________________________________________________________" << endl;
            cout << "| This is a game of rock, paper, scissors. You will the best of three rounds |" << endl;
            cout << "|  against the computer. To pick either rock, paper, or scissors simply      | " << endl;
            cout << "|  type in your choice and hit enter. The computer will randomly pick its    | " << endl;
            cout << "|  choice. At the end of the game type y to play again & n to end the game.  | " << endl;
            cout << "|               Do you understand?  please enter y or n                      | " << endl;
            cout << "|____________________________________________________________________________| " << endl; 
            
            //user input
            cin.getline(yesOrno, 10);
        
    if((strcmp(yesOrno,"n")==0)||(strcmp(yesOrno,"y")==0))            //if the user input is a y or n go into this other loop
        {    
            if(strcmp(yesOrno,"y")==0)
                {
                    checkLoop=1;                                    
                    system("CLS");                        
                    cout<< "Get ready to start the game"<<endl;
                    }
                else                                                //if the user input is no repeat the directions
                
                {
                    system("CLS");
                    cout<< "Read the directions again";
                    checkLoop=0;                                    //makes this loop repeat until y or n entered
                }    
                    }     
    else
        { 
             ;            
             cout<< " Enter a y or n "<<endl;                        //if the user enters something other than a y or n print this statement                                        
             cout<<endl;
             cout<<endl;    
             }}
    
    cout<<"Please type in rock, paper, or scissors"<<endl<<"and then press enter"<<endl;
    for(int x=0;x<500;x++){
    
    cin.getline(playerchoice, 9, '\n');
    
    if (convert_choice(playerchoice) != -1){
    compchoice = computerchoice();
    cout<<"The computer chose "<< print_choice(compchoice)<<endl;
    who_wins(compchoice,convert_choice(playerchoice));
     
    } else {
    cout << "Key in a valid choice" << endl;
    }
    } 
    return 0;
    }
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>allows the user to play again..do i just do the whole game in a huge loop equal to 0 then when the user inputs yes or no have it switched to 1?

    Almost. I'd use a "flag". Like this:
    Code:
    bool doItAgain = true;
    char ch;
    while(doItAgain)
    {
       //game code
       cout << "Game over.  Do it again? Enter 1 for yes, anything else for no" << endl;
      cin >> ch;
      if(ch != '1')
    	doItAgain = false;
    }
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM