# C++ syntax errors

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

## 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 on other sites
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 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 on other sites
(I haven't programmed in c++ in a while but these lines seem wrong)

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 on other sites
Quote:
 Original post by PseudokaiYou 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 << "YOU'RE LYING!!!"

[wink]

##### Share on other sites
Quote:
 Lately, i have wanted to learn C++ to create video games

Welcome to the dark side :)

Quote:

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 nif((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 on other sites
hehe, a interesting small game!

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by CuddleBunniezzz12I 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 on other sites
Quote:
Quote:
 Original post by CuddleBunniezzz12I 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 on other sites
I'm having trouble with my "While" loop.

I get two errors in Dev C++ (I'm going to eventually pick up Visual C++ 2008 Express, but I'm too busy).

They are line 34, the two messages i get are  and (Each

I also added a new if statement for in case the user picks a number that is not 1 - 100.

// 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(){    srand(time(0)); // seed random number genrator    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;        if ((theNumber < 1) || (theNumber > 100))    {        cout << "Sorry, that number won't work, try one that is 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 = 'n';    while (answer == n)    {        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 << "YOU'RE LYING!!!" << 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;}

##### Share on other sites
Please put single quotes around the n in
while (answer == n)
and
if ((guess == theNumber) && (answer == n))

Also, the number to guess can be out of range. Test it by putting two numbers out of range in the program. It accepts the second number. To solve this use:
while ((theNumber < 1) || (theNumber > 100))
if ((theNumber < 1) || (theNumber > 100))`
. This new code keeps asking for a new value, if the previous value was out of range.

Emiel1

PS: Nice idea to change roles with the computer!