Sign in to follow this  

"Beginning C++ game programming" code problem

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

Hello, Im new to C++ and I am using the aforementioned book. Instead of using the compiler that came with it I am using MSVC++ 2008 EE. But for some reason this code wont compile without errors: // Game Stats // Demonstrates declaring and initializing variables #include <iostream> using namespace std; int main() { int score; double distance; char playAgain; bool shieldsUp; short lives, aliensKilled; score = 0; distance = 1200.76; playAgain = `y'; shieldsUp = true; lives = 3; aliensKilled = 10; double engineTemp = 6572.89; cout << "\nscore: " << score << endl; cout << "distance: " << distance << endl; cout << "playAgain: " << playAgain << endl; //skipping shieldsUp since you don't generally print Boolean values cout << "lives: " << lives << endl; cout << "aliensKilled: "<< aliensKilled << endl; cout << "engineTemp: " << engineTemp << endl; int fuel; cout << "\nHow much fuel? "; cin >> fuel; cout << "fuel: " << fuel << endl; typedef unsigned short int ushort; ushort bonus = 10; cout >> "\nbonus: " << bonus << endl; return 0; } Keep in mind I am a newbie! Thanks

Share this post


Link to post
Share on other sites
For both our sake and yours:

#include <iostream>
using namespace std;

int main()
{
int score;
double distance;
char playAgain;
bool shieldsUp;

short lives, aliensKilled;

score = 0;
distance = 1200.76;
playAgain = `y'; // !!! Error 1: this should be 'y' not `y'
shieldsUp = true;
lives = 3;

aliensKilled = 10;

double engineTemp = 6572.89;

cout << "\nscore: " << score << endl;
cout << "distance: " << distance << endl;
cout << "playAgain: " << playAgain << endl;
//skipping shieldsUp since you don't generally print Boolean values
cout << "lives: " << lives << endl;
cout << "aliensKilled: "<< aliensKilled << endl;
cout << "engineTemp: " << engineTemp << endl;

int fuel;
cout << "\nHow much fuel? ";
cin >> fuel;
cout << "fuel: " << fuel << endl;

typedef unsigned short int ushort;
ushort bonus = 10;
cout >> "\nbonus: " << bonus << endl; // !!! Error two, this should be "cout <<" not "cout >>"

return 0;
}



The two errors are noted, along with their fixes, in the source. One is that you've put `y' instead of 'y', the other is that you've accidentally used cout >> instead of cout <<.

EDIT: I see sheep19 already caught the second one -- good catch.

Share this post


Link to post
Share on other sites
Wow TheUnbeliever, how did you see that!!! ;-)

crashdmj, it's a better idea to initialize your variables when you declare them.
If you don't, you might accidentally try to send them to cout, which will give you a garbage value (value that has been left in the computer memory).

Another mistake you might do, is: score++; This increases "score" by one, but "score" is uninitialized, so it will produce an error.



int score = 0;
double distance = 1200.76;
char playAgain = 'y';
bool shieldsUp = true;



Isn't this better?

Share this post


Link to post
Share on other sites
I totally agree. I was just going literally by the book. I have a feeling this book has quite a few more source errors but I will continue on because I like the style in which it is written. This is only day 2 of C++ but thank you all very much for the help!

Share this post


Link to post
Share on other sites

This topic is 3635 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.

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