Sign in to follow this  
Lorraine_B

Please help- looping?undeclared identifier?

Recommended Posts

Lorraine_B    122
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
X19OV1    118
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
Lorraine_B    122
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
rip-off    10976
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
Lorraine_B    122
okay changed the order of those as u sugested.

heres one of the erros im having

: error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'int (__cdecl *)(void)' (or there is no acceptable conversion)

(theres a long list of errors this is like the first one)

Share this post


Link to post
Share on other sites
Lorraine_B    122
kk ty i fixed alot of the caps, sorry about those i know its rather anoying.
i was just used to doing it because examples for class used caps for some reason

please stay with me for a lil im fixing everything i see u mention , im puting the c infront of those now , refresh ur page ina sec

Share this post


Link to post
Share on other sites
Lorraine_B    122
awsome that took away almost all of the errors! =)

okay this is the only error im having now

fatal error C1083: Cannot open include file: 'cstdlib.h': No such file or directory

O_O what now?

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by Lorraine_B
now im geting an error called
error C2446: '==' : no conversion from 'const char *' to 'int (__cdecl *)(void)'


This is a far more cryptic error, especially for a beginner. Essentially, it means "I cannot compare a string and a function". A string literal (something in quotes) is of type const char *, and a function have their own type, which is related to how they are declared.

Any function has this form:

<return-type> <name> ( <argument-list> ) ;

You can see that "int (__cdecl *)(void)" looks quite similar to that, "int" is the return type and "void" means that the argument list is empty. The second set of parentheses, with "__cdecl *" inside them, are beyond this discussion, but again they are more clues that the type is a function.

A correct declaration of your string is this:

string PLAYAGAIN;

Although you would have to initialise it to "yes" for your program to work. However, as I showed in my earlier post, this variable can be local to main (or eliminated entirely) by rewriting your RETRY function.

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by Lorraine_B
awsome that took away almost all of the errors! =)

okay this is the only error im having now

fatal error C1083: Cannot open include file: 'cstdlib.h': No such file or directory

O_O what now?


Read it again, a little more carefully:
Quote:

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>.

C++ standard library headers do not have a ".h" extension.

And don't forget to check the link I gave about forum tags. You can use [source] and [/source] around your code to put them in the scrolling, syntax highlighted box.

Share this post


Link to post
Share on other sites
Lorraine_B    122
ohhhh i see, ohay i fixed that and updated the code

now im getting this error

fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\owner\my documents\visual studio 2005\projects\ice and dice\ice and dice\icendice.cpp(198)' was matched

O.o


by the way i put that source and /source in so now it made the page smaller =)

Share this post


Link to post
Share on other sites
rip-off    10976
We can't fix your code for you, that is against the forum rules on homework.

The error message is pretty self explanatory. The reference to "198" in the error message is the line number. So go to line 198 and look for an opening curly brace. The error says that when the compiler was looking for the corresponding closing brace, it reached the end of the file.

Other than that, you may want to copy and paste your code from your IDE again as it has been mangled by the transformation into HTML.

Next time you get an error message, try googling the error code. This will tell you more about what the error means. Here, the error code is "C1075". If you can't figure out the error message means, try include something about what you think it might mean next time you post. Don't worry about being wrong.

Learning to decipher error messages is an important skill, so best to practise.

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by Lorraine_B
it doesnt say next to it what line it is

In the editor?

Assuming MSVC, go to "tools, options, text editor, all languages, general" and tick the "show line numbers" setting. I would have thought that line numbers would be enabled by default.

But you can just click on the error message and it will jump to the line involved.

Share this post


Link to post
Share on other sites
Lorraine_B    122
awsome thanks so much, i got the numbers to come up so i know where line 198 is now

thanks alot i really apreiciate it. i didnt ant to come here for someone to do my homework for me i just couldnt figure htis out on my own, you know? so thanks for explaining alot of things to me! =)

ill keep working at it and see if i can get it to work ^^

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by Lorraine_B
awsome thanks so much, i got the numbers to come up so i know where line 198 is now

No problem!

Quote:

thanks alot i really apreiciate it. i didnt ant to come here for someone to do my homework for me i just couldnt figure htis out on my own, you know? so thanks for explaining alot of things to me! =)

I understand. I have to say that compiler writers do seem to go out of their way to make the errors cryptic and difficult to understand. Even a C++ veteran can get a lost when they try compile some template wizardry and the compiler takes exception to it and chokes on a few hundred lines of dense error messages.

Just don't forget about some of the advice I gave in my first reply. Your latest edit doesn't appear to have taken it all in, some of it is extremely important.
Quote:

ill keep working at it and see if i can get it to work ^^

Good luck with getting your program to work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this