Thread: Please review my structure.

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    16

    Please review my structure.

    I am new to programming and bought Jumping into C++ by Alex Allain. I am working through the practice problems and was hoping someone could take a look at this and let me know if I am writing the code in an appropriate format.

    My program compiles and runs fine. I just do not want to get into any bad habits. Any reviews would be greatly appreciated.

    Thanks,

    Kranky

    Code:
    #include <iostream>
    
    using namespace std;
    
    // Chapter 4 practice problem 1
    // Ask the user for two users' ages and indicate who is older
    // Behave differently if both are over 100
    
    int main()
    {
        int age1;
        int age2;
    
        cout << "This program will compare two ages and let you know which is older\n";
        cout << "Enter age 1: ";
        cin >> age1;
    
        cout << "Enter age 2: ";
        cin >> age2;
    
        if ( age1 < age2 )
        {
            cout << "Age 1 is younger than Age 2\n";
        }
        else if ( age1 == age2 )
        {
            cout << "They are both the same age!\n";
        }
        else
        {
            cout << "Age 2 is younger than Age 1\n";
        }
    
        if ( age1 > 100 && age2 > 100 )
        {
            cout << "They are both very old!\n";
        }
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I can spot no problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your code is excellent thus far but if I were you I'd explicitely "return 0;" at the end( or "return EXIT_SUCCESS;" ).
    Devoted my life to programming...

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I find it a little weird that newbies are already using his book... but good for him.

    It looks fine for a beginner. I'm glad to see the formatting rubbing off.

    That said, if you want to get into good habits you can count on, you can start checking the return values of library functions.

    For example, you might check to make sure that input succeeded:

    Code:
    if(!(cin >> age1))
    {
        // The input request failed for some reason.
    }
    I haven't finished the book myself, but I assume more information about how the stream operators behave is coming.

    Soma

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    16
    Thank you for the responses. I have not made it to the part about return, so I am not sure where that goes.

    It feels weird putting spaces in statements like "age1 < age2" versus "age1<age2". I can that it is easier to read, it just feels harder to type.

    I put spaces on lines 13, 17, 20, and 33 because it feels like they need to be there and I put my comment lines and the beginning to let people know what I was doing the program for.

    Thanks,

    Kranky

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Kranky View Post
    I put spaces on lines 13, 17, 20, and 33 because it feels like they need to be there and I put my comment lines and the beginning to let people know what I was doing the program for.
    That's how a programmer should think!
    You've got a built-in sense of indentation and proper documentation, I wish I had it too.
    Devoted my life to programming...

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Just keep working at it and you'll get there soon enough.

    As for the formatting, you are free to invent your own, but the book sticks to a common form pretty consistently. Consistency is your friend, especially when you are a beginner. I strongly recommend you stick with the formatting the book presents for now.

    That said, "harder to type" isn't valid criticism. I've written a huge lot of code in my career. If you stick with programming long term "lines of code" will become such a meaningless metric you'll almost stop seeing "lines of code" and just reading what it does.

    Soma

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by GReaper View Post
    Your code is excellent thus far but if I were you I'd explicitely "return 0;" at the end( or "return EXIT_SUCCESS;" ).
    That is optional, though.
    return 0 is implicit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review please
    By Richardcavell in forum C Programming
    Replies: 3
    Last Post: 05-13-2011, 11:01 AM
  2. Program review
    By MikeyVeeDubb in forum C Programming
    Replies: 1
    Last Post: 09-24-2009, 01:29 PM
  3. c++ review
    By san in forum C++ Programming
    Replies: 3
    Last Post: 07-10-2002, 06:56 PM
  4. Can someone review?
    By ob1cnobe in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 09:48 PM