Thread: Age Check Program

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    2

    Question Age Check Program

    How are you guy's doing? My name is James, i began learning c++ last week. I have been learning about basic input,if statements,outputs, and integers. I am currently working on a program that is suppose to tell you your current age. I cannot seem to get my if statement to work. I would gladly appreciate it, if you guys could take the time to check out my code.


    Code:
    #include <iostream>
        using namespace::std;
        int a;
        int b;
        int c;
        
        
    
        
        int main()
        
    {
        
        
        cout<<" Age Check" <<endl;
        cout << "Enter the current year "<< "\n";
        cin>>a;
        cout << " Enter the year born" << "\n" ;
        cin>>b;
        cout <<" If born after January press 1"<<"\n";
        cin>>c;
        
        if  (c = 1);{
        int result2 = 2011 - b;
        cout <<" You are : " <<result2<<endl;
        }
        if (c = 0);{
        int result = a - b;
        cout << " You are : " <<result<<endl;
        cin>>result;
        }
        
        cout << " Press enter twice to exit" <<endl;
        getchar(), getchar(),getchar(),getchar();
        return 0;
        }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Remove the semi-colon between the closing bracket and the opening curly bracket. The semi-colon makes the if only apply to an empty statement block.

    Next, read up about what == does versus what = does.

    Lastly, learn about global variables versus local variables, and why you should learn to use local variables instead of globals wherever possible.

    That should have it all sorted out for you.
    Last edited by iMalc; 01-20-2012 at 02:32 AM.
    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"

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    2
    Thank you very much i tweaked it a bit. I looked up the == and =. That made a big difference.., the program is working fine now.
    Here is the tweaked program.
    Code:
    #include <iostream>
        using namespace::std;
        int a;
        int b;
        int c;
        
        int main()
        
    {
        cout<<" Age Check" <<endl;
        cout << "Enter the current year "<< "\n";
        cin>>a;
        cout << " Enter the year born" << "\n" ;
        cin>>b;
        cout <<" If born on January press 1 if born after press 0"<<"\n";
        cin>>c;
        
        if  (c == 1) 
        {
        int result2 (2011- a);
        cout <<" You are : " <<result2<<"\n";
        cin>>result2;
        getchar(),getchar(),getchar(),getchar();
    
        }
    
        else if (c == 0)
        {
        int result = ( a - b );
        cout << " You are : " <<result<<endl;
        cin>>result;
        getchar(),getchar(),getchar(),getchar();
        return 0;
        }
        
        
        
        }
    Last edited by Jlongoria89; 01-20-2012 at 03:22 AM.

  4. #4
    Novice
    Join Date
    Jul 2009
    Posts
    568
    A couple of suggestions.

    Declaring `a`, `b` and `c` outside of `main()`, outside of any functions, for that matter, makes them what is called global variables. This means that they are accessible everywhere, in any function. That might seem like a good thing at first, and can sometimes be useful, but is generally discouraged in programs of any significant complexity, except in special cases, like definition of constants and certain development patterns. Reason being that, if these variables can be changed from several functions it becomes hard to debug the program and it can make it hard to argue about the global state of the program. In this case, move them inside of `main()` and you'll be OK.

    Your `using` statement is incorrect. At least I think it is, or it's a form I've never seen before. It should be `using namespace std;`, otherwise you're declaring that you're using name `std` from namespace `namespace`.

    You can declare multiple variables of the same type on the same line, like `int a, b, c;` instead of one per line. This is OK for simple types, but can lead to subtle errors. Consider `int* a, b, c;`. This looks like declaring three `pointers-to-int` but is, in fact, a declaration of `pointer-to-int` `a` and `int`s `b` and `c` due to `*` decorator binding to the right.

    Chaining `getchar()` like that is likewise unnecessary, usually one is enough, and none are needed if you'll be executing from the console.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Not only are the multiple calls to getchar() unneeded, but they could potentially cause problems. You should not mix iostream and stdio functions. Use std::cin.get() instead.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indentation can be improved, as well.
    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. Can someone check my program?
    By funkl3 in forum C++ Programming
    Replies: 5
    Last Post: 12-11-2009, 10:59 AM
  2. check out my first program
    By CrKrJak in forum C++ Programming
    Replies: 17
    Last Post: 09-07-2009, 08:19 PM
  3. please help to check my program
    By alice in forum C Programming
    Replies: 5
    Last Post: 04-18-2004, 09:08 AM
  4. check out my program
    By Shadow in forum Windows Programming
    Replies: 5
    Last Post: 12-03-2002, 11:44 PM
  5. My First BIG program done!!! Check it out!!
    By biosninja in forum C++ Programming
    Replies: 8
    Last Post: 09-04-2002, 03:33 PM