Jump to content
  • Advertisement
Sign in to follow this  
CuddleBunniezzz12

C++ syntax errors

This topic is 3595 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

Hi there. Lately, i have wanted to learn C++ to create video games, and I got this book called "Begging C++ through game programming, second edition" by Michael Dawson. Anyways, in chapter 2, the book told me how to create a number guessing game where i guess the number the computer randomly generates. At the end of the chapters, we are given exercises to create modifications of the example programs used, and one was to create a mod of the number guessing game where the computer and human switch roles in where the computer guesses. In Dev-C++ 4.9.8 (I think that's the version) I keep on getting these errors: And this is the source for the program (ignore the dashes): ------- // Guess my Number 2.0 - Comp guesses // The classic number guessing game, with a twist #include <iostream> #include <cstdlib> #include <ctime> using namespace std; int main() { int theNumber; cout << "\tWlcome to Guess My Number 2.0\n\n"; cout << "Enter a number for the computer to guess (from 1 to 100): "; cin >> theNumber; // Let the user start when he wants cout << "Okay, were going to start soon.\n\n"; system("PAUSE"); cout >> "GO!!\n\n\n"; char answer; while (answer == n) { rand(time(0)); // seed random number genrator int guess = rand() % 100 + 1; // the computer guesses the number cout << "Computer: Is your number: " << guess << "? (y/n)\t"; cin >> answer; if (guess == (theNumber && (answer == n))) { cout << "YOUR LIEING!!!" << endl; cout << "I know the number is " << guess << "!" << endl; cout << "I can see it in your eyes!" << endl; cout << "Play fair next time." << endl; } } cout >> "\n\nSo " << theNumber << " was your number after all.\n" << endl; cout >> "Good game." system("PAUSE"); return 0; } -------- That IF statement in the WHILE statement was something I wanted to put in if the human entered no even if the computer was right. It was not said to be entered by the book, just have the human and computer swap roles from the original number guessing game. Any help is appreciated to why i get these errors. ~Thanks.

Share this post


Link to post
Share on other sites
Advertisement
It helps if you use source tags and show us what lines have errors.

Half the time you write "cout <<", the other half "cout >>"

You need to pay closer attention.

Share this post


Link to post
Share on other sites
The first issue is that you're using Dev-C++, which is considered by just about everyone to be an outdated and buggy tool. Download Visual C++ Express 2008 and you'll find the experience to be much better.

Second, I can notice from your code that you use rand(time(0)) to seed the random number generator, when you actually meant to type srand, which is the seed function.

Third, your if statement most likely won't do what you want it to do. It's going to evaluate the inner expression as a boolean and then compare the guess to true or false. I don't think that's what you intended. Drop all the extra parenthesis in there and things should work out correctly.

Share this post


Link to post
Share on other sites
(I haven't programmed in c++ in a while but these lines seem wrong)

char answer;
while (answer == n){...}

You declare answer and then immediately check it without initializing it(giving it a value), and it doesn't look like you give a value in any reachable code.
The next line is comparing it to "n" which I don't see declared anywhere. If you're trying to get the character n you need to use 'n' instead.


Share this post


Link to post
Share on other sites
Quote:
Original post by Pseudokai
You declare answer and then immediately check it without initializing it(giving it a value), and it doesn't look like you give a value in any reachable code.
The assignment is in the while loop. The OP wants to use a do..while loop instead to avoid the quoted problem.

The biggest mistake I see is:

cout << "YOUR LIEING!!!"

instead of:

cout << "YOU'RE LYING!!!"

[wink]

Share this post


Link to post
Share on other sites
Quote:

Lately, i have wanted to learn C++ to create video games


Welcome to the dark side :)

Quote:

char answer;
while (answer == n)


You should initialize answer to some value before using. Also, as Pseudokai pointed out, to see if a char variable is equal to some specific letter, put the letter inside single quotes.


char answer = 'n';
while(answer == 'n');




Quote:

rand(time(0)); // seed random number genrator


You only need to do this once at the start of the program rather than inside the loop.

Quote:

int guess = rand() % 100 + 1; // the computer guesses the number


This could potentially take a while before it hits your number. Although this is fine for now you may want to lookup binary search when you start feeling more comfortable with the language.

Quote:

if (guess == (theNumber && (answer == n)))


As with previous comment about the start of the loop, remember to compare answer to 'n'. Also the brackets are mixed up a bit here. What this does is compare answer to n ('n') which results in a true/false value. Then it does 'theNumber != 0' (the long version of putting just theNumber) which also results in a true/false value. Then it checks if both of those 2 values are true which results in a 3rd true/false value. It then compares that final true/false to the value of guess. That probably isn't what you want :)


// if guess is equal to theNumber and answer is equal to the letter n
if((guess == theNumber) && (answer == 'n'))




Also, immediately after the last cout statement in that block I would add a 'break;' to exit the loop there.

Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by CuddleBunniezzz12
I thank you all for your replies.

And that cin <<>> cout thing is a constant typo for me.

Anyone known a good pneumonic aid to help remember which one to use?


I think of it this way... They point to where you are sending data. So for cout, you are sending data to the Ostream called cout, so it is

cout << "blah blah" << endl;

But, if you are reading data in, you are sending it to a variable, so it is

cin >> x;

Just point the arrow whichever way you want the data to flow.

Thats how I think of it anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by Chadwell
Quote:
Original post by CuddleBunniezzz12
I thank you all for your replies.

And that cin <<>> cout thing is a constant typo for me.

Anyone known a good pneumonic aid to help remember which one to use?


I think of it this way... They point to where you are sending data. So for cout, you are sending data to the Ostream called cout, so it is

cout << "blah blah" << endl;

But, if you are reading data in, you are sending it to a variable, so it is

cin >> x;

Just point the arrow whichever way you want the data to flow.

Thats how I think of it anyway.


Yeah that's the way I see and think of it too I don't use any mnemonics, besides if you keep practicing your C++ you won't need any!

p.s. And ditch dev-c++ as has been suggested. I guess you are using the older 1st edition of book since I think I saw newer 2nd in bookstore and now it suggest you use visual express instead I think no?

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!