"Beginning C++ game programming" code problem

Started by
5 comments, last by crashdmj 16 years, 3 months ago
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
Advertisement
1. Use [source] tags when posting code.

2. Be sure to post the actual errors that you're getting.

3. Take another look at the line before 'return 0'.
Sorry about that! I will put Source and my errors in next time. Thanks very much
what jyk said. Take a look at the line before return 0;
You accidentally entered >> instead of <<
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.
[TheUnbeliever]
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?
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!

This topic is closed to new replies.

Advertisement