"Beginning C++ game programming" code problem
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
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'.
2. Be sure to post the actual errors that you're getting.
3. Take another look at the line before 'return 0'.
what jyk said. Take a look at the line before return 0;
You accidentally entered >> instead of <<
You accidentally entered >> instead of <<
For both our sake and yours:
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.
#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.
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.
Isn't this better?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement