Thread: Best practice for super simple program

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128

    Best practice for super simple program

    Hi everyone,

    Been learning c++ for a couple of weeks and using Alexs book as a reference. I'm very much enjoying it but almost embarrassed to post this after seeing some of the other 'newbie' threads (is there a term lower than newbie I can use for myself?!). I'm hoping someone will remember that they were once in this position themselves and take pity.

    Anyway, maybe someone could help me out with best practice for programming something like in my examples.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        int firstAge;
        int secondAge;
        
        cout << "Please enter first persons age: ";
        cin >> firstAge;
        cout << "Please enter second persons age: ";
        cin >> secondAge;
        
        if (firstAge && secondAge > 100)
        {
            cout << "Wow, both over 100!!" << "\n";
        }
        
        else if (firstAge > secondAge)
        {
            cout << "Person one is older than person two" << "\n";
        }
        
        else
        {
            cout << "Person two is older than person one" << "\n";
        }
    }
    and

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        int firstAge;
        int secondAge;
        
        cout << "Please enter first persons age: ";
        cin >> firstAge;
        cout << "Please enter second persons age: ";
        cin >> secondAge;
        
        if (firstAge && secondAge < 100)
        {        
            if (firstAge > secondAge)
            {
                cout << "Person one is older than person two" << "\n";
            }
        
            else
            {
                cout << "Person two is older than person one" << "\n";
            }
        }
        else
        {
            cout << "Wow, both over 100!!" << "\n";
        }
        
    }

    To me, they both do the same thing, the only difference I can see is one is marginally shorter. I imagine these differences would grow over a larger program so I want to get into good habits right from the get go. All comments are very much appreciated or improvements I could make etc.

    Thanks,

    Sam.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    [Edit]
    Actually your condition statements are a bit wrong. Read that chapter again.
    Code:
    (firstAge && secondAge > 100)
    should be
    Code:
    (firstAge >100 && secondAge > 100)
    [/Edit]
    As long as the logic is sound, the ordering and nesting of the clauses does not make any difference (and I don't think any one is a better practice than the other ). The compiler will probably generate the same code, ultimately.

    That said, write whichever you find easier to logically comprehend.
    (Following the logic correctly is what most beginners fail to do, so if you understand the logic behind the above code... you're already better than 50%. )
    Last edited by manasij7479; 06-02-2012 at 09:24 AM.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by manasij7479 View Post
    That said, write whichever you find easier to logically comprehend.
    (Following the logic correctly is what most beginners fail to do, so if you understand the logic behind the above code... you're already better than 50%. )
    Thanks. I find the second one makes more sense to me logically. I think there is a 'main' check to see whether they are over 100 or not (if they are, then the sub check is not required anyway). Then a sub check of if they are under 100, then who is older.

    'Else if' has me confused.

    **I noticed the edit thanks** yes - I used similar logic allowing 2 different passwords like this:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string username;
        string password;
        
        cout << "Please enter your username: " << "\n";
        getline (cin, username, '\n');
        cout << "Please enter your password:" << "\n";
        getline (cin, password, '\n');
        if ((username == "root") && (password == "pass" || password == "pass2"))
        {
            cout << "Access granted" << "\n";
        }
        else
        {
            cout << "Either password or username is incorrect. Please try again" << "\n";
            return 0;
        }
    }
    I have written about 10 small programs already today as I find that's the only way I learn!

    Sam.
    Last edited by samwillc; 06-02-2012 at 09:35 AM.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'd say you're doing pretty well.
    One thing with your last post though is that the return 0; line should actually be outside of the else statement so that it is also executed when the condition is true.
    You may be getting a compiler warning about that.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Thanks.

    Using geany editor on ubuntu 12.04. no warning though.

    I did another one a minute ago for setting a variable and then using it:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    
    {
        string setUsername;
        string setPassword;
        string username;
        string password;
        
        
        cout << "Please set a username: " << "\n";
        getline (cin, setUsername, '\n');
        cout << "Please set a password: " << "\n";
        getline (cin, setPassword, '\n');
        
        cout << "Please login with your username and password" << "\n" //read a second ago in another post that subsequent cout commands can be combined rather than one per line
        << "Please enter your username: " << "\n";
        getline (cin, username, '\n');
        cout << "Please enter your password: " << "\n";
        getline (cin, password, '\n');
        if ((username == setUsername) && (password == setPassword))
        {
            cout << "Thank you. Access permitted" << "\n";
        }
        else
        {
            cout << "Sorry, wrong username or password. Try again." << "\n";
        }
        return 0;
    }
    That's the last example, I promise!

    Does this line need to be:

    Code:
    if ((username == setUsername) && (password == setPassword))
    or
    Code:
    if (username == setUsername && password == setPassword)
    Or does it not matter?

    Sam.
    Last edited by samwillc; 06-02-2012 at 10:08 AM.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by samwillc View Post
    Using geany editor on ubuntu 12.04. no warning though.
    Use the "-Wall" option on the compiler (g++, I presume)...which shows all warnings.
    Or does it not matter?
    It doesn't ... considering this. ( C++ Operator Precedence - cppreference.com )
    The two == s always get the first shot at execution...(without considering short circuiting).

  7. #7
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Thanks for the link, bookmarked it. I always wondered what :: was. Scope resolution, actually still don't know what it is, but at least now it has a name!

    Quote Originally Posted by manasij7479 View Post
    Use the "-Wall" option on the compiler (g++, I presume)...which shows all warnings.
    Under Build > 'Set build commands' in geany I have these settings:

    Compile: g++ -Wall -c "%f"
    Build: g++ -Wall -o "%e" "%f"

    So it looks like it's already using -Wall. I'm not 100% sure though if that's what you meant.

    Thanks.

    Sam.

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by samwillc View Post
    Under Build > 'Set build commands' in geany I have these settings:

    Compile: g++ -Wall -c "%f"
    Build: g++ -Wall -o "%e" "%f"

    So it looks like it's already using -Wall. I'm not 100% sure though if that's what you meant.
    Yes, you already have that enabled.
    It just happens that gcc does not think that the code warrants a warning.
    (Probably because it knows to assume a "return 0;" at the end of main by default.)

  9. #9
    Registered User
    Join Date
    Jun 2012
    Location
    Morden, UK
    Posts
    128
    Quote Originally Posted by manasij7479 View Post
    Yes, you already have that enabled.
    It just happens that gcc does not think that the code warrants a warning.
    (Probably because it knows to assume a "return 0;" at the end of main by default.)
    Ok thanks for the help

    I know it's early days but I'm like a little kid in a sweet shop each time I run one of my programs, however lame it may be. Writing the calculator one now.

    Sam.

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by samwillc View Post
    I know it's early days but I'm like a little kid in a sweet shop each time I run one of my programs, however lame it may be. Writing the calculator one now.
    I'm pretty new too... compared to many of the veterans here.
    Just wait until you write something not so lame... you'll be over the moon using it again and again just to see it work.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That's coming along well. Only one thing I can think of to point out and that's that you can include the newline as part of the string with the text, and I guess there's no point in having a space before the newline as you wouldn't see it:
    Code:
        cout << "Please set a username:\n";
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a program for a super market!
    By danishzaidi in forum C Programming
    Replies: 5
    Last Post: 11-09-2011, 12:51 PM
  2. Boat loader my practice program
    By jimtuv in forum C Programming
    Replies: 10
    Last Post: 05-29-2010, 09:43 PM
  3. simple char** practice
    By nadroj in forum C Programming
    Replies: 5
    Last Post: 04-13-2007, 05:55 AM
  4. Super Easy C++ program help
    By ftblstr2319 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 05:07 PM
  5. help on this very small practice program
    By incognito in forum C++ Programming
    Replies: 4
    Last Post: 01-01-2002, 10:28 AM