First off, would like to say I am a fresh noob at this. Class started last friday, and already I am eager to keep learning. Anyhow I was bored and started to work on a class lab work 3 weeks ahead. Before anyone judges, I am not trying to get the answer but rather help and knowledge to solve my problem. Anyhow here is the program so far (sorry if its not clean or all over the place) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <string>
using namespace std;
int main()
{
//typedef defined
typedef unsigned short int USHORT;
//decleration section
USHORT FirstPersonAge,SecondPersonAge;
int diff;
string fnamestr,snamestr;
//main code starts :
cout << "This program calculates the age difference between two people.\n\n";
cout << "What is the name of the first person?" << endl;
cin >> fnamestr;
cout << "How old is " << fnamestr << "?" << endl;
cin >> FirstPersonAge;
cout << "What is the name of the second person?" << endl;
cin >> snamestr;
cout << "How old is " << snamestr << "?" << endl;
cin >> SecondPersonAge;
diff = FirstPersonAge - SecondPersonAge; //simple arithematic to calculate age difference
//if statement for result prompt display
if ( FirstPersonAge > SecondPersonAge )
cout << "Result: " << fnamestr << " is " << diff << " years older than " << snamestr << ".";
if ( FirstPersonAge < SecondPersonAge )
cout << "Result: " << fnamestr << " is " << diff << " years younger than " << snamestr << ".";
if ( FirstPersonAge == SecondPersonAge )
cout << "Result: " << fnamestr << " and " << snamestr << " are the same age.";
cout << endl;
system("PAUSE");
return 0;
}
|
So far when i Run it in debug it works great and calculation works. But i have two problems.
1. If for instance the first AGE i enter is 26 and the second age I enter is 27, the end Results will display
"...-1 years younger than <name>.." . The calculation is correct, but is there a way to have it display a positive integer rather than a negative just to make it a clean result?
2. The lab assignment, calls for the program to also; If the age difference between two people is exactly one year, the Result message should use the word "year" instead of "years" (singular instead of plural). I am thinking this could be done through the use of the IF statement, but i am stumped as to where to begin?
Many thanks in advance.
-Steph'
-PS. I am using VS 2010,, hence the system("PAUSE") ..