C++ question

Discussion in 'OT Technology' started by SuperMan91, Apr 17, 2008.

  1. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Im new with C++ so i just started out with the traditional "Hello World" program but everytime i run it, it only shows up for like a tenth of a second then goes away. Heres the code for u to see if i messed up on someting or not.


    //Hello world program

    #include <iostream>
    using namespace std;

    int main()
    {
    cout << "Hello World!/n";
    return 0;
    }
     
  2. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,490
    Likes Received:
    0
    Location:
    Southern Oregon
    run your command prompt, THEN run the program.
     
  3. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa


    Still does the same thing..... :wtc:
     
  4. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,490
    Likes Received:
    0
    Location:
    Southern Oregon
    um, you be dumb, son.
     
  5. bandit320

    bandit320 New Member

    Joined:
    Mar 26, 2007
    Messages:
    16
    Likes Received:
    0
    lol
    I think you need to go start->run

    type in cmd

    then in the window that pops navigate to the directory where your .exe is located.

    type the name of your exe and your program will run.
     
  6. kd.snake

    kd.snake New Member

    Joined:
    Apr 13, 2008
    Messages:
    1
    Likes Received:
    0
    Location:
    Russia, Moscow
    Newline character is "\n" :)
     
  7. CodeX

    CodeX Guest

    change it to this:

    #include <iostream>
    using namespace std;

    void main()
    {
    while(1) cout << "Hello World! << endl";
    }

    but no seriously its because your program does nothing and then exits immediately... what did you expect to happen? You see that return 0? that ends the program... so you display "hello world" in the command prompt and then immediately with the next instruction exit it.
     
  8. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,709
    Likes Received:
    0
    That wouldn't matter if he manually opened a DOS window first, so that it wouldn't automatically close as soon as the program is done running. It would stay open, displaying the output from the program until he closed the window himself.

    - - -

    Unrelatedly, it's considered bad form to use the "void" type for any function. You don't have to capture the output from a function with a return value, but in case you ever happen to need to return an error code to the user, it's good to have that capability pre-programmed by using the "int" type instead of "void".
     
  9. CodeX

    CodeX Guest

    It's generally a bad idea to question someones programming style when they make a living doing it... I know damn well I am not going to be returning a value to the system so I use void... and if in the future I wanted to it would be beyond trivial to make that change.

    I don't follow conventions for the sake of it being a convention. Unless you can give me a better reason I will continue to declare functions void if I know they will not be returning anything.

    Also, correct me if I am wrong but by declaring a function void you reduce stack usage (otherwise, space is reserved on the stack for the return value, since its an automatic variable). Big deal right? Well unless you work in an embedded environment like I do where we have had to increase stack size 3 times now in the history of this project due to stack overflow
     
  10. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa



    Thanks, it works now.
    You are Teh Man......... :bowdown:
     
  11. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,709
    Likes Received:
    0
    I agree that it's not necessary to declare functions with a return type if they don't return anything, but it doesn't make any practical difference. It's just as easy to type "int main" instead of "void main" and "return 0;" instead of "return;", and it leaves you open to return a meaningful value later on if you need to, without even having to think about it.

    In an embedded environment, I can see how there could be an advantage to reducing stack size by an extra 4 bytes per function, but then, why are you writing embedded code in C++ in the first place? C would be a much more compact choice.

    Anyway, that piece of my post was actually directed at the threadstarter, not you. And for the record, I write code for a living too, just not every single day.
     
  12. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa





    Whats the difference between "int main" and "void main".....? { and like i said im new to C++ so i pretty much sucks at it. }
     
  13. CodeX

    CodeX Guest

    void means the function does not return a value to the place it was called from. Any other data type specifier in place of void (int for example) reserves appropriate space on the stack for that function to return a value, example:

    int add(int a, int b)
    {
    return a+b;
    }


    This function returns an integer type, it could be used like this:

    int c = add(1,2);

    Where c would then equal 3.
     
  14. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,764
    Likes Received:
    3
    Location:
    Plantation, FL

    there's really no overhead difference between using C and using C++
     
  15. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    Errrr........ Dizamn i suck at C++ (and no im not givin up, just need more help..lol) Well im stuck on my 3rd prgram. .. pathetic isnt it..... :eek4:( i only got my 2nd program right so far..) I was editing a small program to test to make sure i was ready to move on to the next chapter... and i managed to mess sometin up...... ne wayz, heres the code.. all the errors happened with the "enum" and below.


    // Game stats
    // Demonstrates constants

    #include <iostream>
    using namespace std;

    int main()
    {
    const int ALIEN_POINTS = 150;
    int alienskilled = 10;
    int score = alienskilled * ALIEN_POINTS;
    cout << "score: " << score <<endl;

    enum Difficulty (Novice, Easy, Normal, Hard, Unbeatable);
    Difficulty myDifficulty = Easy;

    enum ship (Fighter = 25, Bomber, Cruiser = 50, Destroyer = 100);
    ship myShip = Bomber;
    cout << "/nTo upgrade will cost "
    << (Cruiser - myShip) << " Resource Points./n" <<endl;
    system("pause");
    return 0;
    }
     
  16. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,709
    Likes Received:
    0
    Maybe there's no overhead difference, but there is definitely a difference in the size of the compiled code. C code almost always compiles significantly smaller than C++ code, which means when the program gets loaded into RAM on an embedded system, there will be more RAM left over for overhead.
     
  17. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,764
    Likes Received:
    3
    Location:
    Plantation, FL
    that depends on alot of things
     
  18. SuperMan91

    SuperMan91 New Member

    Joined:
    Mar 2, 2008
    Messages:
    107
    Likes Received:
    0
    Location:
    Pa
    is someone gonna help me out alil?...lol
     
  19. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,709
    Likes Received:
    0
    Fuck off noob, we're talking about complicated big-people stuff now.

    :big grin:

    Seriously though, what's left that you don't understand? You got handed the answer you were looking for.
     
  20. CodeX

    CodeX Guest

    You are not defining the enumerated types properly. First of all, you need to set the initial value, it can be anything but is most often 0. Also, you need to use {}'s instead of ()'s... just a syntax detail. And one last thing, you are setting your ships values to values like 25, 50, 100... this does not belong in an enumerated type. Enumerated types are used for counts of things, often used to index an array of structs, therefore having a beginning of 0 and then a single digit increment (0,1,2,3,4... etc). If you intended the values you are assigning to your ships to represent health or something you should not use an enumerated type.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        enum Difficulty 
        {
            Novice = 0, 
            Easy, 
            Normal, 
            Hard, 
            Unbeatable,
            DIFFICULTY_COUNT
        };
    
        enum Ship 
        {
            Fighter = 0, 
            Bomber, 
            Cruiser, 
            Destroyer,
            SHIP_COUNT
        };
    
        const int ALIEN_POINTS = 150;
        const int health[SHIP_COUNT] = {25,25,50,100};
        int alienskilled = 10;
        int score = alienskilled * ALIEN_POINTS;
        Ship myShip = Bomber;
        Difficulty myDifficulty = Easy;
    
        cout << "score: " << score << endl << endl;
        cout << "To upgrade will cost "    << (health[Cruiser] - health[myShip]) << " Resource Points." << endl;
        system("pause");
        return 0;
    }
    Basically, I just setup an array to hold the health values (or resource points or whatever the hell they are supposed to be) of each of the ship types. You can then index that array with an element of the Ship enumeration (which starts at 0 with fighter and ends at 3 with cruiser).

    You should only use enumerated types when the value of the enumeration is arbitrary. They are basically used to assign a name to a counted value, you could just as easily use a bunch of #define directives. I believe internally enumerated types are treated as unsigned int's, though I could be wrong about that.
     
    Last edited by a moderator: Apr 18, 2008

Share This Page