Like Tree1Likes
  • 1 Post By jimblumberg

do while loop to auto execute when there is a tie in a game ? completely stuck

This is a discussion on do while loop to auto execute when there is a tie in a game ? completely stuck within the C++ Programming forums, part of the General Programming Boards category; Hello all. I am stuck on something I know should be really simple to fix but I just can't figure ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    do while loop to auto execute when there is a tie in a game ? completely stuck

    Hello all. I am stuck on something I know should be really simple to fix but I just can't figure it out I've been Google-ing and gone through my text (Gaddis C++ Intro, Ch. 6) and tried a few things, but it keeps coming back to the variables not being defined/declared for use within the loop -- except I don't see how they aren't. I'm using VS 2010.


    Here's the code. I appreciate any help. I feel kinda stupid for not being able to fix this myself. This is my first programming class and I'm also taking visual basic at the same time. It's been interesting.


    The debug errors I'm getting are both C2065 "undeclared identifier" for both cpuChoice and userChoice.


    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    
    
    
    void outputChoice(int c)
    {
      switch(c)
      {
        case 1:
            cout << "Rock";
            break;
        case 2:
            cout << "Paper";
            break;
        case 3:
            cout << "Scissors";
            break;
        case 4:
            cout << "Lizard";
            break;
        case 5:
            cout << "Spock";
            break;
      }
    }
    
    
    //bool for determining if win or if draw -- loss will be elseif
    
    
    bool isWin(int userChoice, int cpuChoice)
    {
      bool result = 
        (    (userChoice == 1 && cpuChoice == 3) ||
            (userChoice == 1 && cpuChoice == 4) ||
            (userChoice == 2 && cpuChoice == 1) ||
            (userChoice == 2 && cpuChoice == 5) ||
            (userChoice == 3 && cpuChoice == 2) ||
            (userChoice == 3 && cpuChoice == 4));
      return result;
    }
    
    
    bool isDraw(int userChoice, int cpuChoice)
    {
      bool result = 
            ( (userChoice == cpuChoice));
      return result;
    }
    
    
    int main()
    {
        do{
    
    
            srand(time(NULL));
        
            cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
            cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
            cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
            cout << endl;
            
            {
                int userChoice;
        
                    cout << "Please choose your move.  Select 1-5: \n\n";
                    cout << "1) Rock" << endl;
                    cout << "2) Paper" << endl;
                    cout << "3) Scissors" << endl;
                    cout << "4) Lizard" << endl;
                    cout << "5) Spock\n\n" << endl;
        
                    cin >> userChoice;
        
                if (!(userChoice >= 1 && userChoice <= 5))
                {
                    cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
                }
        
                else
                {
                    int cpuChoice = rand() % 5 + 1;
                      cout << "You chose... ";
                    outputChoice(userChoice);
                    cout << endl;
            
                    cout << "The computer chose... ";
                    outputChoice(cpuChoice);
                    cout << endl;
                    cout << endl;
    
    
                    cout << "The result is..." << endl;
                }
    
    
                if (isWin(userChoice, cpuChoice))
                {
                    cout << "You chose wisely!  WINNER!!!!!" << endl;
                }
            
                else if (isDraw(userChoice, cpuChoice))
                {
                    cout << "You chose well, but so did I - TIE!" << endl;
                }
            
                else
                {
                    cout << "You chose poorly!  You loose!" << endl;
                }
            
        }
        while (userChoice == cpuChoice);    
    
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,897
    First you seem to have some mismatched braces in your code, you should verify that the opening and closing braces match.

    Next look at this snippet:

    Code:
    ...
               else
                {
                    int cpuChoice = rand() % 5 + 1;
    
    ...
                }
     
     
                if (isWin(userChoice, cpuChoice))
    You declared cpuChoice inside your else statement, which means that it is only a valid variable inside that else statement, not any statements following the else block. So in that last if statement cpuChoice is not defined. You may want to study up a little about variable scope.


    Also posted here.


    Jim
    Last edited by jimblumberg; 09-22-2013 at 05:28 PM.
    Salem likes this.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    2

    works great now - THANK YOU

    thank you, i knew it was simple as "duh, put your variables outside so everything can access them" ... i claim brain fry but thanks again, much appreciated

    sorry i didn't see it before i posted too


    Quote Originally Posted by jimblumberg View Post
    First you seem to have some mismatched braces in your code, you should verify that the opening and closing braces match.

    Next look at this snippet:

    Code:
    ...
               else
                {
                    int cpuChoice = rand() % 5 + 1;
    
    ...
                }
     
     
                if (isWin(userChoice, cpuChoice))
    You declared cpuChoice inside your else statement, which means that it is only a valid variable inside that else statement, not any statements following the else block. So in that last if statement cpuChoice is not defined. You may want to study up a little about variable scope.


    Also posted here.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using the while loop to execute sequences
    By jamesallen4u in forum C Programming
    Replies: 15
    Last Post: 10-03-2011, 05:42 AM
  2. completely stuck understanding how to use time.h
    By pastitprogram in forum C++ Programming
    Replies: 11
    Last Post: 04-04-2008, 11:11 AM
  3. Complete C noob...and completely stuck lol
    By JST212 in forum C Programming
    Replies: 6
    Last Post: 03-02-2008, 10:54 AM
  4. Payroll- I'm completely stuck
    By stno17 in forum C Programming
    Replies: 7
    Last Post: 11-29-2007, 03:17 AM
  5. I'm completely stuck!
    By tek in forum C++ Programming
    Replies: 0
    Last Post: 08-23-2003, 07:43 PM

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