If statements and Simple Pos. Int output?

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") ..
closed account (3hM2Nwbp)
1) Absolute Value - http://www.cplusplus.com/reference/clibrary/cmath/fabs/
2)
1
2
3
4
if(diff == 1)
    ...
else
    ...


*edit
typedef unsigned short int USHORT; sounds like we have a new windows programmer budding :P
Last edited on
Thanks Luc Lieber, for the prompt reply.

You help me solved the second question. I added the else statement then followed by the if statements for the plural verbage. seems to be working :)

However trying to figure out the absolute value suggestion for the first question.

-Steph'
closed account (3hM2Nwbp)
1
2
3
4
5
6
7
8
9
10
11
12
#include <cmath>
int main()
{
    if(abs(-3) != 3)
    {
        return 0xBADC0DE;
    }
    else
    {
        return 0x600DC0DE;
    }
}
Thanks!! Ill give this a try. Just curious is there any other alternative besides using "abs" function to obtain the same result ??
Not really, unless you count writing out what the function basically does:
((x < 0) ? -x : x)
Or zeroing the sign bit, or something.
closed account (3hM2Nwbp)
I had to come up with a reason to post on this topic again that wouldn't be considered spam, so if your instructor would allow it, I would suggest that you make a utilities header file that contains all of the useful functions that you've created thus far in your courses.

Now for the spam, I assume your user tag is referring to BMW's E36 Coupe. Not a bad ride ;)
Coupee36 - Never use system("pause") if you are serious about programming. Its non standard and a bad programming habbit.
Check out this link for the reason behind it and alternatives for this:
http://www.gidnetwork.com/b-61.html
Last edited on
Thank you all for the help.. and thanks, anonymouscoder for the suggestion i will read into that link tonight..

LUc - yes, you are correct.. OT real quick, i upgraded to E46 now though.. :)

once again, many thanks everyone.
anonymouscoder, the system("PAUSE"); i was only using since my IDE auto closes when i build/debug the program. I tried the alternative mention on your link.. cin.get() but that auto closes as well.. I understand now what system("PAUSE") does, thanks to the link and if all that is true then i agree thats a bad habbit.. but it seems unless i find something that works with VS2010 i'll have to be using system("PAUSE") at this time in order to debug.. unless of course there are other suggestions that would work with VisualStudio2010..

thanks.
Topic archived. No new replies allowed.