Thread: Simple Variable Problem

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    2

    Simple Variable Problem

    I'm very very new at programming, and I am attempting to make a simple (I thought) program that asks the user the current year, their age. It would then output the year in which they were born. +/- a year, depending on when the birthday falls... But I'll fix that later.

    The issue is, BirthYear *always* returns 23. I can't figure it out. I suspect I'm doing something wrong here:

    Code:
    int BirthYear = CurrentYear - Age;
    But here is the complete program in case that is not the problem.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int AskWhatYear ();
    int AskAge ();
    
    
    
    int main()
    {    
    
      int Age;
      int CurrentYear;
    
      AskAge();
      AskWhatYear();
    
      int BirthYear = CurrentYear - Age;
    
      cin.get();
    
      cout<<"\nYou were probably born in "<< BirthYear;
      cin.get();       
    }
    
    
    
    
    int AskAge() // Asks the users age.
    {
      int Age;
    
      cout<<"\nPlease enter your age: ";
      cin>> Age;
      cin.ignore();
      return Age;
    }
    
    int AskWhatYear() // Asks the user what year it is.
    {
      int CurrentYear;
    
      cout<<"\nPlease enter the current year: ";
      cin>> CurrentYear;
      cin.ignore();
      return CurrentYear;
    }
    Thanks in advance!

    I'm sure there are other noob mistakes in there that work, but aren't the ideal way to do it. I'm open to al suggestions

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    the only thing wrong with it is that you did not asign "Age" and "CurrentYear" in the main function a value.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int AskWhatYear ();
    int AskAge ();
    
    
    
    int main()
    {    
    
      int Age;
      int CurrentYear;
    
      Age = AskAge();
      CurrentYear = AskWhatYear();
    
      int BirthYear = CurrentYear - Age;
    
      cin.get();
    
      cout<<"\nYou were probably born in "<< BirthYear;
      cin.get();       
    }
    
    
    
    
    int AskAge() // Asks the users age.
    {
      int Age;
    
      cout<<"\nPlease enter your age: ";
      cin>> Age;
      cin.ignore();
      return Age;
    }
    
    int AskWhatYear() // Asks the user what year it is.
    {
      int CurrentYear;
    
      cout<<"\nPlease enter the current year: ";
      cin>> CurrentYear;
      cin.ignore();
      return CurrentYear;
    }
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    2
    Thank you very much. Works great now.

    I new it was going to be simple, but after going over it numerous times, I couldn't see what it might be.

    Thanks again

    /me bows

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple XOR program
    By spike_ in forum C++ Programming
    Replies: 8
    Last Post: 08-17-2005, 12:09 AM
  2. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  3. Simple memory game problem
    By Eamusuta in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2005, 07:59 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM