Thread: Age Generating Console Program

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    Age Generating Console Program

    The reasoning behind posting this is to gather constructive criticism on my form, and see what you experts have to say about how I went about what I was trying to accomplish.

    This program is designed to request your date of birth information (seperately, I didn't know how to do it in one single MM/DD/YYYY input..)

    Then, request the current date info (again, didn't know how to gather 3 values in one input)
    and then calculate the age and verify no flaws in the calculation.

    In some questions, please do forgive my lack of better wording...

    For the sake of syntax highlighting and ease of reading, I took the code and posted it on codepad (similar to pastebin but for coding)

    C++ code - 61 lines - codepad

    If you wish for me to paste the 61 line code here, I will do so, but I forewarn that it's pretty hard to read with the little allotted space.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    First thing i noticed stdio.h should be cstdio for it to be standard c++.

    I prefer to pre-calculate the variables used in if-conditions because it simply looks better. Especially if you reuse them many times in either nested ifs or in else if clauses, this makes it easier to read. In this case this is a moot point since you can replace the else if with a simple else, either the first if is true or the second else if must be true. You can not have a case where the first if is false and the second else if is false.

    To extend it more, you probably want to make sure the user has typed either yes or no to the last question, as it is now the user can type abc and it will print out the you tricked me message. Validate your input to make sure it is valid.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    @Stdio: I tried as you suggested, cstio instead of stdio.

    Compiling: /home/joshua/Desktop/Extra Programming/Tutorial2.cpp
    /home/joshua/Desktop/Extra Programming/Tutorial2.cpp:3:20: error: cstdio.h: No such file or directory
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings


    @Suggestions: I changed my if statements into variable testing rather than defining what to test in the () itself [if you understand what I'm saying; if not look at the if statements in the code]

    C++ code by JGranger95 - 80 lines - codepad

    Now, in this, I've copied and pasted into the else if statement the initial question, so if their answer isn't yes or no it asks the question again.

    How can I make it loop until an answer is valid, rather than just asking one more time?

    Initially; I tried putting a return: and goto return; but what this did was if the month/day has yet to pass, it would decrease age by 1 each time the loop was repeated, making the age display 1 year younger per input error.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Additional bits:
    • Not only is it supposed to be <cstdio>, but why is it there at all?
    • You need <string> for string variables.
    • You need <cctype> for tolower (assuming you're not trying to use the fancy one that uses locales).
    • Your if statement does not accurately determine whether one date is after another. (For example, your if statement believes February 1 is before January 31.)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by User-_-Name View Post
    @Stdio: I tried as you suggested, cstio instead of stdio.

    Compiling: /home/joshua/Desktop/Extra Programming/Tutorial2.cpp
    /home/joshua/Desktop/Extra Programming/Tutorial2.cpp:3:20: error: cstdio.h: No such file or directory
    Process terminated with status 1 (0 minutes, 0 seconds)
    1 errors, 0 warnings
    Nobody said anything about cstdio.h. Which is good since, as you noticed, it doesn't exist.

    Quote Originally Posted by User-_-Name View Post
    @Suggestions: I changed my if statements into variable testing rather than defining what to test in the () itself [if you understand what I'm saying; if not look at the if statements in the code]

    C++ code by JGranger95 - 80 lines - codepad

    Now, in this, I've copied and pasted into the else if statement the initial question, so if their answer isn't yes or no it asks the question again.

    How can I make it loop until an answer is valid, rather than just asking one more time?
    Well ... you use a loop. while is a loop, do-while is a loop. if is not a loop.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    em, don't use goto. Now I am yet to read why it is so bad..but just to let you know either way.

    I don't know what your variables mean..
    ttl? time to live?
    Anyways, why not use some while or for loop? That's what you do, when you need a loop

    Code:
     if((ttl >= 0) && (xxl >= 0))
            {
                 cout << "You are currently " << age << " years old... correct? (yes/no)\n";
            }
        else if((ttl < 0) || (xxl < 0 ))
            {
                 cout << "You are currently " << --age << " years old... correct? (yes/no)\n";
            }
    
        //asks user if the calculations are correct and outputs accordingly
        cin >> answ;
    I would have thought answ would have been in either if statement, and call a function to execute appropirate code..
    Hopefully some experienced programmer would answer you quickly!
    You ended that sentence with a preposition...Bastard!

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    @Tabstop: I'm not really particularly familiar with absolutely everything in each file to #include, I just included it... don't remember why. Why do you NEED <string> ? Everything I've used thus far doesn't require it, strings still function as strings.

    cctype for tolower? algorithm also allows tolower.. any insight on what the difference is?


    @Eman: ttl and xxl are not particularly meant to represent any acronyms, I was just proposing an alternative as Shatki had suggested.

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by User-_-Name View Post
    @Eman: ttl and xxl are not particularly meant to represent any acronyms, I was just proposing an alternative as Shatki had suggested.
    oh well, wouldn't it be cool if your variables had names in relation to what they were supposed to do?
    Ah, anyways I will leave it to the experts.
    Good luck man.
    You ended that sentence with a preposition...Bastard!

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Thanks for your support It's always great to have agreeable people around! Most of my variables do, I was just in a hurry.

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Oh boy.. I just noticed a pretty big flaw.

    I was thinking about what Tabstop said: the program thinks 02/01 is "younger" than 01/31....

    Code:
        if(((cmonth - mborn) >= 0) && ((cday - dborn) >= 0))
            {
                 cout << "You are currently " << age << " years old... correct? (yes/no)\n";
            }
        else if(((cmonth - mborn) < 0) || ((cday - dborn) < 0 ))
            {
                 cout << "You are currently " << --age << " years old.... correct? (yes/no)\n";
            }
    I added an extra period after old... on the else if statement so I could see which it was outputting after testing the two dates.

    Turns out there isn't technically a flaw with the date differences, but I misused the first if statement:

    if(((2010 - 1995) >= 0) AND ((31 - 1) >= 0))

    31-1 is a negative number, therefore it is not greater than or equal to 0, making the program think that day has yet to pass and therefore not completing the True | True | True table required for the && operator...

    What are my possible solutions? Theoretically this will happen any time the current day is less than the day you were born, I just haven't noticed because I was using the current date (12/30/2010)

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by User-_-Name View Post
    @Tabstop: I'm not really particularly familiar with absolutely everything in each file to #include, I just included it... don't remember why. Why do you NEED <string> ? Everything I've used thus far doesn't require it, strings still function as strings.

    cctype for tolower? algorithm also allows tolower.. any insight on what the difference is?


    @Eman: ttl and xxl are not particularly meant to represent any acronyms, I was just proposing an alternative as Shatki had suggested.
    If you dont #include <string>, there is no such thing as a string object. (Some <iostream> headers include string, but you shouldn't rely on being so lucky.) If you don't #include <cctype> (or <clocale>), you don't have a tolower function. There is no tolower function declared in the <algorithm> header. The only reason you're getting away with it at the moment is that you've done ::tolower, which is ... well, I won't call it cheating, but there is no valid reason to ever use ::<standard library name>, ever. Basically you're saying "I didn't include the right header, so you won't find it in the std:: namespace where it belongs, but if you pretend it's a plain C un-prototyped function everything will be all right." (Granted, in this case it's true, since tolower takes an int and returns an int.)

    As to the other, you should only check the day if the months are the same. If the months are different checking the day only leads to heartache.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Okay. I've included string, cctype, and clocale;

    Now: My program cannot do without algorithm due to the fact transform() is in algorithm.
    What is the syntax (if any) to use tolower to move a string to lowercase without the transform()?

    Also, I've fixed it up to only check day if months are the same. Should be good to go now

    Thus; I've fixed every bug announced so far, and I have included (however useless) all files required, possibly one or two extras.

    And further input? [I'm surprised I've been able to understand and cope with criticism thus far, I haven't been learning but a few days tops. I appreciate you guys giving simple and easy-to-understand reasoning (for the most part)]

    http://codepad.org/pWCQcrX8

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    transform is in <algorithm>. tolower is in <cctype>. If you want to use both, you need to include both.

  14. #14
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    I don't particularly want to use them specifically; I've just yet to be introduced to a more simple method of converting strings to lowercase.

    Interesting idea; how difficult would it be to create a library/header file (or whatever is required) with a new command to convert a string to lowercase?

    something along the lines of strlower(stringnamehere);

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by User-_-Name View Post
    I don't particularly want to use them specifically; I've just yet to be introduced to a more simple method of converting strings to lowercase.

    Interesting idea; how difficult would it be to create a library/header file (or whatever is required) with a new command to convert a string to lowercase?

    something along the lines of strlower(stringnamehere);
    Given that it's one line of code in C++, I would say "very easy". (And of course, the C++ version already has the locale stuff built-in, so it will work with different character sets/alphabets/etc as if by magic.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Help with console program
    By feso4 in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2006, 11:53 AM
  3. my first windows console program
    By Syneris in forum Windows Programming
    Replies: 3
    Last Post: 04-08-2002, 03:18 PM
  4. DOS program versus DOS console program
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 01:42 PM
  5. semaphores using C++ in console program
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2001, 12:46 PM