Simple stuff!

This is a discussion on Simple stuff! within the C++ Programming forums, part of the General Programming Boards category; I was thinking of making a simple program that tells how old you are. So here is what I come ...

  1. #1
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321

    Question Simple stuff!

    I was thinking of making a simple program that tells how old you are.

    So here is what I come to:


    #include <iostream.h>

    int main()

    {

    int age;

    cout<<"Enter date you were born (4 digit):";

    cin>>age;

    cout<<"You are " <<2002-age "years old

    }


    Would it work?

    Sorry, I don't got a compiler to tell you what I got.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Would it work?
    No.

    >Sorry, I don't got a compiler to tell you what I got.
    So why does it matter if it works or not? Try it when you get a compiler.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    close, but no cigar. Using slightly outdated syntax you could do something like this:

    #include <iostream.h>

    int main()

    {

    int age;
    int year = 2002;


    cout<<"Enter year you were born (4 digit):" << endl;

    cin>>age;

    cout<<"You are " << year - age << " years old " << endl;

    return 0;

    }

    There are ways to jazz this up a little, but it should be a functional program as is.

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Of course those programs only print an approximation of the persons age; to actually compute their age you would need to use the current month and day, and the birth month and day as well.

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    here's a possibility
    Code:
    #include <iostream.h>
    
    int main(){
    
    int birth_year,birth_month,birth_day;
    
    
    
    //current date info 
    // ( in reality this should not be hardcoded )
    int year = 2002;
    int month = 12;
    int day = 26;
    
    cout<<"Enter year you were born (4 digit):";
    cin>>birth_year;
    cout<<"Enter month you were born (2 digit):";
    cin>>birth_month;
    cout<<"Enter day you were born (2 digit):";
    cin>>birth_day;
    
    
    int age = year - birthyear;
    if(birth_month < month) age-- ;
    else if(birth_month == month && birth_day < day) age-- ; 
    
    cout<<"You are " << age << " years old " << endl;
    
    return 0;
    
    }
    I think that should work, but I haven't tested it so be warned that it may not.

  6. #6
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321
    I just want to be able to assume what the outcome will be without a compiler.

    Yeah, I know, - never assume too much -

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Dude, Liger86, why don't you have a complier? You can download one for free. You can't be a programmer without a compiler.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321
    I got one at school that I experiment with but I'm gettin VC++ .net

    So there
    From Ukraine with love!

    Internationally known widely respected

    - Digitally yourz -

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    I dont think iostream could handle that expression and it would be considerable if the expression is replaced with a variable wich receive the result

    Good luck~

  10. #10
    Registered User dead_cell's Avatar
    Join Date
    Jun 2002
    Posts
    44

    You might want to guard against tampering with the year and date options

    Code:
    #include <iostream>
    using namespace std;
    
    int main(){
    
    int birth_year,birth_month,birth_day;
    
    //current date info 
    // ( in reality this should not be hardcoded )
    int year = 2002;
    int month = 12;
    int day = 26; //reflect current day
    
    cout<<"Enter year you were born (4 digit):";
    cin>>birth_year;
    
    if(birth_year > 2002)
    {
    cout<<"Error:  You cannot be born after this year if you are using the program now.  See Einstein's clock theory for more details"<<endl;
    }
    if(birth_year <= 2002)
    {
    cout<<"Enter month you were born (2 digit):";
    cin>>birth_month;
    if((birth_month<0) || (birth_month>12))
    {
    cout<<"Error: In the standardized time format adopted some while ago, you cannot have a negative month or a month having a value more than 12 (unless you're referring to the Simpsons' \"Smarch\""<<endl;
    }
    if((birth_month>0) && (birth_month <= 12))
    {
    cout<<"Enter day you were born (2 digit):";
    cin>>birth_day;
    if(birth_day < 0 || birth_day > 31)
    {
    cout<<"Error: In the standardized time format adopted some while ago, you cannot have a negative day or a day having an integral value exceeding that of 31"<<endl;
    }
    else
    {
    int age = year - birth_year;
    if(birth_month < month) age-- ;
    else if(birth_month == month && birth_day < day) age-- ; 
    		
    cout<<"You are " << age << " years old " << endl;
    }
    }
    return 0;
    }
    }
    Just as a little revision to beege31337's code, You might want to guard against negative/exceeding years, negative/exceeding days, and negative/exceeding months. This was hacked together quite quickly so I don't know if it'll compile, but it should work.

  11. #11
    In your face... ha ha ha Liger86's Avatar
    Join Date
    Dec 2002
    Location
    Motorcity Capital
    Posts
    321
    What do

    Code:
    << endl;
    do?
    From Ukraine with love!

    Internationally known widely respected

    - Digitally yourz -

  12. #12
    Registered User dead_cell's Avatar
    Join Date
    Jun 2002
    Posts
    44

    *sigh*

    If I were in a bad mood, i'd say RTFM, but since you're new here, I'll explain.

    When you're outputting a string of text or variables in C++, you normally use iostream to handle the output and input by using cout<< and cin>>. However, in the cout<< function, you can jump down by a line by using the \n switch inside a logical text string. Ex:

    Code:
    cout<<"Hello, let me skip a line downwards...\nHello!";
    Because the \n switch doesn't flush the output buffer, it is commonly used in short, simple functions and programs. Since I was quickly hacking the code together, I didn't take that into consideration.

    <<endl is used to flush the output buffer and jump a line downward in the data output. Whether you use it for big or small functions, it doesn't matter. However, it is generally considered correct in both cases, as it makes for a smaller compilation size and a better compiled footprint (if you're working with complex algorithms).

    Ex:

    Code:
    cout<<"Hello, let me skip a line downwards..."<<endl<<"Hello!";
    Last edited by dead_cell; 12-27-2002 at 01:14 PM.
    Linux is great - It does infinite loops in five seconds!

    ~Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple snmp console application.
    By csteinsv in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2009, 06:03 PM
  2. Connecting to a SQL Server to do simple stuff
    By GDR92 in forum C Programming
    Replies: 4
    Last Post: 01-07-2009, 07:56 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Need help with simple data types
    By partnole in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 09:36 AM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 10:38 AM

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