-
question
why wont this work?
Code:
#include <iostream>
using namespace std;
int main()
{
int score;
float distance, EngineTemp;
short lives, alienskilled;
char playagain;
score = 0;
distance = 1200.76;
lives = 3;
alienskilled = 10;
EngineTemp = 6572.89;
playagain = 'y';
cout << "score: " << score <</n;
cout << "distance: " << distance <</n;
cout << "play again: " <<playagain <</n;
cout << "lives: " <<lives <</n;
cout << "ailenskilled: " <<ailenskilled: <</n;
cout << "Engine Tempature is: " << EngineTemp <</n;
int fuel;
cout << "/nhow much fuel is left: ";
cin >> fuel;
cout >> "fuel: " << fuel <<endl;
typedef unsigned short int bonus;
bonus = 10;
cout <<"/nbonus: " << bonus << endl;
return 0;
}
-
<</n;
the '\n' should be in single quotes. (also note that it is a backslash, not forward)
Also, next time try to explain what the problem is rather than "why wont this work"
ie. compiler errors, unexpected behaviour, etc...
-
Code:
typedef unsigned short int bonus;
bonus = 10;
cout <<"/nbonus: " << bonus << endl;
I think either you made a silly mistake there or you misunderstand the concept of typedef. This is what the compiler will see:
Code:
unsigned short int =10;
cout <<"/nbonus: " << unsigned short int << endl;
You might want something like:
Code:
typedef unsigned short int bonus;
bonus b=10;
cout<<"\nbonus: "<<b<<endl;
-
i did what you guys said and it worked!
but i just want to know what adding the b did?
-
The b is a variable. The code
is equivalent to
Code:
bonus b; /* defines b as a variable, of type bonus */
b = 10; /* assigns the value 10 into b */
The way you had it before, you defined bonus as a type, not as a variable.
Code:
typedef unsigned short int bonus;
This code means that the string of letters "bonus" is to be interpreted as "unsigned short int".
So when you write
on the next line, you are really writing "unsigned short int = 10;"
That's like writing "int = 10;" or "char = 'A';" or "double = 3.14;". These are not allowed, because "int", "char", and "double" are not variables that you can assign things to. They are descriptions of variables, that tell what kind of things that the variables can hold.
Think of types as adjectives, and variables as nouns.