Jump to content
  • Advertisement
Sign in to follow this  
Lorraine_B

Please help- looping?undeclared identifier?

This topic is 3309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

im suposed to make a game and it basicaly another version of the game petals around a rose only you call it polar bears around a hole... it needs to: make THREE dice pop up on the screen randomly on each roll and then you count them and put in your answer and if your right you move on if your wrong the game is over. and at the end it needs to ask if you want to play again or not and if yes it needs to start all over if no it shud close the program. anyways i get this error :error C2065: 'PLAYAGAIN' : undeclared identifier can you PLASE help me, please please please.... im realllly new to C++ programing so try to explain things really well so i understand. heres my code... (anything else u see wrong please let me know) #include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; int Diceroll(); int Introscreen(); void Tryagain(); void Win(); int Totalbears; int Dicenumber; int Useranswer; int Totalcorrect; string PLAYAGAIN; string NAME; bool PLAY = true; int main() { srand((unsigned)time(NULL)); string PLAYAGAIN = "yes"; while (PLAYAGAIN == "yes") Introscreen(); while(PLAY) { Totalcorrect = 0; for (int iCOUNT=1; iCOUNT<11; iCOUNT++) { if (Useranswer==3) { Win(); } Diceroll(); cout<<"How many polar bears do you see around the ice holes?"<<endl; cin>>Useranswer; if (Useranswer == Totalbears) { cout<<"Correct! =D"<<endl; Totalcorrect++; } else { cout<<"Incorrect! The right answer was: "<<Totalbears<<endl; } system("pause"); Tryagain(); } } } void LOSE (string NAME) { //the losing screen cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl; cout<<"! !"<<endl; cout<<"! Sorry, !"<<endl; cout<<"! You Lose! !"<<endl; cout<<"! !"<<endl; cout<<"! !"<<endl; cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl; cout<<endl; cout<<"SORRY "<<NAME<< " YOU LOST"<<endl<<endl; cout<<"BETTER LUCK NEXT TIME"<<endl; } //the intoduction screen int Introscreen() { cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<< endl; cout<<"! !"<<endl; cout<<"! !"<<endl; cout<<"! Ice and Dice !"<<endl; cout<<"! !"<<endl; cout<<"! !"<<endl; cout<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"<<endl<<endl; cout<<endl; cout<<"Game details:"<<endl; cout<<"The name is in the game, and the game is in the name."<<endl; cout<<"and the name of the game is Polar Bears Around An Ice Hole."<<endl; cout<<"Some people call this Petals Around A Rose."<<endl<<endl; cout<<endl; cout<<"Wish to play? Please enter your name here:"<<endl; cin>>NAME; } int Diceroll() { Totalbears = 0; for (int numRoll = 1; numRoll<4; numRoll++) { Dicenumber=1+rand()%6; switch(Dicenumber) { case 1: { cout<<"*******"<<endl; cout<<"* *"<<endl; cout<<"* 0 *"<<endl; cout<<"* *"<<endl; cout<<"*******"<<endl; Totalbears = Totalbears+0; break; } case 2: { cout<<"********"<<endl; cout<<"* 0 *"<<endl; cout<<"* *"<<endl; cout<<"* 0 *"<<endl; cout<<"********"<<endl; Totalbears = Totalbears+0; break; } case 3: { cout<<"*******"<<endl; cout<<"* 0 *"<<endl; cout<<"* 0 *"<<endl; cout<<"* O *"<<endl; cout<<"*******"<<endl; Totalbears = Totalbears+2; break; } case 4: { cout<<"*********"<<endl; cout<<"* 0 0 *"<<endl; cout<<"* *"<<endl; cout<<"* 0 0 *"<<endl; cout<<"*********"<<endl; Totalbears = Totalbears+0; break; } case 5: { cout<<"*********"<<endl; cout<<"* O O *"<<endl; cout<<"* O *"<<endl; cout<<"* O O *"<<endl; cout<<"*********"<<endl; Totalbears = Totalbears+4; break; } case 6: { cout<<"********"<<endl; cout<<"* O O *"<<endl; cout<<"* O O *"<<endl; cout<<"* O O *"<<endl; cout<<"********"<<endl; Totalbears = Totalbears+0; break; } default: { break; } } } return 0; } void Win () { cout<<"Congratulations, "<<NAME<<", You have won! =D"<<endl; } void Retry() { cout<<"Want to play again? (yes or no)"<<endl; cin>>PLAYAGAIN; if (PLAYAGAIN == "no") { PLAY = false; cout<<"Bye"<<endl; } if (PLAYAGAIN == "yes") { PLAY = true; cout<<"Okay!"<<endl; } [Edited by - Lorraine_B on May 1, 2009 7:42:04 PM]

Share this post


Link to post
Share on other sites
Advertisement
Ok, first thing:
Please use source and
/source tags for your code. It makes it musch easier to read.
Second: You never defined 'PLAYAGAIN' as and interger or boolean.

Share this post


Link to post
Share on other sites
i thought it was being used for like, at the end if they say "yes" that they want to play again it was using bool play

and i updated my code above so u can look at it again

Share this post


Link to post
Share on other sites
Let us look at this carefully. First, lets make sure we understand what the compiler means. What is an "undeclared identifier". Well, C++ has many rules, one of which is that you must tell C++ about something before you use it.

Its like if I said: "Yeah, so I was hanging out with Jenny yesterday and...". If I never mentioned "Jenny" before, you would probably interrupt me there and then and say "Who is Jenny"?

So the compiler is asking: "what is PLAYAGAIN", or more precisely, "what type is PLAYAGAIN".

Let us look at the code. Here is everything before main:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;


int DICEROLL();
int INTROSCREEN();
void RETRY();
void WIN();
int BEARTOTAL;
int DICENUMBER;
int USERANSWER;
int TOTALCORRECT;
string NAME;
bool PLAY = true;


No mention of PLAYAGAIN yet.

Now the first few lines of main (indentation added)

void main ()
{
srand((unsigned)time(NULL));

while (PLAYAGAIN == "yes")

INTROSCREEN ();

// ...
}


Ok, so the compiler sees you trying to use something called PLAYAGAIN, without you giving it any clues about it.

Two lines later you do tell it, but its too late, the compiler has already rejected your program. Conversations with people are a bit more lenient [smile]

The solution is to move the declaration of PLAYAGAIN to before the loop:

void main ()
{
srand((unsigned)time(NULL));

string PLAYAGAIN = "yes";

while (PLAYAGAIN == "yes")

INTROSCREEN ();

// ...
}


There are a number of other things about your program:

  • The function "main" should return an int, it is not a void function.

  • The outer loop in main() lacks curly brackets. If you write a loop without using curly brackets, only the first line is considered part of the loop. So you essentially wrote: "while playagain equals yes, introscreen()", which probably isn't what you wanted.

  • You can avoid writing function declarations by moving some of your functions before main()

  • Global variables (ones declared outside a function) are generally not a good idea. Instead, see if you can pass information to functions in their parameter lists and you can use the return keyword to send information back from a function.

    As an example, consider a "retry" function:

    // This function returns "true" if the player wants to retry.
    // Other wise it returns "false".
    // It will try again if it gets input it doesn't understand
    //
    bool retry()
    {
    // keep looping until they give us an answer!
    while(true)
    {
    cout << "Want to play again? (yes or no)" << endl;
    string answer;
    cin >> answer;

    // use "else" when two conditions are mutually exclusive.
    if (answer == "no")
    {
    return false;
    }
    else if(answer == "yes")
    {
    return true;
    }
    else
    {
    cout << "I'm sorry, I didn't understand your answer: " << answer << endl;
    }
    }
    }



    Other code can use the information like this:

    // keep playing while the user says they want to retry:
    do
    {
    playGame();
    }
    while( retry() );


  • The old C headers are called cwhatever, not whatever.h. So instead of <stdlib.h>, we use <cstdlib> and for <time.h> we use <ctime>.
  • You have a switch statement where a number of the cases are identical. See if you can use the "default" case instead to handle these.

  • You can see that when I posted code, I got a box with some nice colouring, at the same time the indentation of the code was preserved. You can do this too, by using these tags when writing your posts. You can go back and edit your original post to include them, though you will need to copy and paste your code back in to fix the indentation.

  • Caps are awful hard to read. Try replace them ASAP [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!