question

This is a discussion on question within the C++ Programming forums, part of the General Programming Boards category; why wont this work? Code: #include <iostream> using namespace std; int main() { int score; float distance, EngineTemp; short lives, ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    28

    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;
    }

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    <</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...

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    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;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    28
    i did what you guys said and it worked!

    but i just want to know what adding the b did?
    Last edited by NiVaG; 10-04-2004 at 10:49 PM.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    26
    The b is a variable. The code

    Code:
    bonus b=10;
    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
    Code:
    bonus = 10;
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21