• 08-07-2003
RealityFusion
Hello I am new here and just thought it nice to introduce myself.

I have been messing around with C++ for quite some time, off and on. I pretty much know the basics, depending on what YOU think the basics are. I am writing this program for the fun of it and to learn more about this language. I am making very good progress but I have come across a little problem.

1.The program is built to get 2 different ages from different people.

2. Calculate the difference between the two people.

3. Calculate how old one person will be when the other is at a different age.

Code:

```# include <iostream.h> int Diff(int a, int b) { cout << "In Diff(), received " << a << " and " << b << ".\n"; return (a-b); } int GetAge(int a,int y) {         cout << "In GetAge() , received " << y << " also received " << a << ".\n";       return (a-y); } int main() {     cout <<"I'm in main.\n";     int a, b, c, x, y, z, f;     cout <<"This function will hopefully calculate the age difference between\n";     cout <<"me and my nephew.\n";     cout <<"Enter your age.\n";     cin >>a;     cout <<"Enter your newphews age.\n";     cin >>b;     cout <<" \nCalling Difference()\n";     c=Diff(a,b);     cout <<"Back in main.\n";     cout << "c was set to " << c << ".\n";     cout <<"Wasn't that fun?";         cout <<"Now lets try something a little more complex ok?\n";     cout <<"This time we will calculate how old your nephew will be when you are a certain age.\n";     cout <<"How old do you want to be?\n";     cin >>y;     z=GetAge(a,y);     cout <<"The age difference between your actual age and the age you want to be is " <<x << ".\n";     cin >>f;         return 0; }```
It will compile and it works except that the value that is returned from GetAge() is a lengthy number that I know is incorrect. I think I know why there is confusion in the program. When I call the function Diff() I think it is changing the value of int a or it just gets lost. I was thinking that I would have to use a getline(), but I don't know how that works and I am still researching how it works.

If anyone can help I would be most appreciative. :) Thanx
• 08-07-2003
MrWizard
Code:

```z=GetAge(a,y); cout <<"The age difference between your actual age and the age you want to be is " << x << ".\n"; cin >>f;```
You compute the difference and store it in z but then you use x in your output. Also your GetAge and Diff function do the exact same thing. Why do you have cin >> f at the end also?
• 08-07-2003
7stud
z=GetAge(a,y);
cout <<"The age difference between your actual age and the age you want to be is " <<x << ".\n";

What variable do you store the value returned from GetAge()? What variable do you display in the next line?
• 08-07-2003
RoD
This reply might not be the first one cuz i had to install .NET and u know how THAT is lol....

moving on. Lets first get rid of the depreciated header:

ie we'll make

[/code]#include <iostream.h>[/code]

into
Code:

`#include <iostream>`

Now the problem is you are assigning the difference of how old you are, and want to be, to Z and then outputting x. I didnt change your code, which holds much room for improvement, but i did fix it. Also you had

return(a-y) and it was negative because you need to return(y-a);

Fixed:

Code:

```# include <iostream> using namespace std; int Diff(int a, int b) { cout << "In Diff(), received " << a << " and " << b << ".\n"; return (a-b); } int GetAge(int a,int y) {         cout << "In GetAge() , received " << y << " also received " << a << ".\n";       return (y-a); } int main() {     cout <<"I'm in main.\n";     int a, b, c, x = 0, y, z, f;     cout <<"This function will hopefully calculate the age difference between\n";     cout <<"me and my nephew.\n";     cout <<"Enter your age.\n";     cin >>a;     cout <<"Enter your newphews age.\n";     cin >>b;     cout <<" \nCalling Difference()\n";     c=Diff(a,b);     cout <<"Back in main.\n";     cout << "c was set to " << c << ".\n";     cout <<"Wasn't that fun?";         cout <<"Now lets try something a little more complex ok?\n";     cout <<"This time we will calculate how old your nephew will be when you are a certain age.\n";     cout <<"How old do you want to be?\n";     cin >>y;     z=GetAge(a,y);     cout <<"The age difference between your actual age and the age you want to be is " <<z << ".\n";     cin >>f;         return 0; }```
• 08-07-2003
Android
I think you should replace

#include<iostream.h>

with

#include<iostream>
using namespace std;

Not sure though since I just started learning C++ and I'be seen different ways of doing it but I know that #include<iostream.h>
isn't right since there was an error on my compiler Dev-C++.

The problem was in GetAge() and the number you were displaying.

GetAge should return y - a instead of a - y. and you should put z in place of x in the last cout statement.

So the program should look like this.

Code:

```# include <iostream> using namespace std; int Diff(int a, int b) { cout << "In Diff(), received " << a << " and " << b << ".\n"; return (a-b); } int GetAge(int a,int y) {         cout << "In GetAge() , received " << y << " also received " << a << ".\n";       return (y-a); } int main() {     cout <<"I'm in main.\n";     int a, b, c, x, y, z, f;     cout <<"This function will hopefully calculate the age difference between\n";     cout <<"me and my nephew.\n";     cout <<"Enter your age.\n";     cin >>a;     cout <<"Enter your newphews age.\n";     cin >>b;     cout <<" \nCalling Difference()\n";     c=Diff(a,b);     cout <<"Back in main.\n";     cout << "c was set to " << c << ".\n";     cout <<"Wasn't that fun?";         cout <<"Now lets try something a little more complex ok?\n";     cout <<"This time we will calculate how old your nephew will be when you are a certain age.\n";     cout <<"How old do you want to be?\n";     cin >>y;     z=GetAge(a,y);     cout <<"The age difference between your actual age and the age you want to be is " <<z << ".\n";     cin >>f;         return 0; }```
• 08-07-2003
RealityFusion
Guess I was just overlooking that :cool: . I didn't realize that the include file didn't require the .h extension. Also what is this namespace std; that you mention? What does it do, seems to work fine without it so far.
• 08-07-2003
RoD
Namespace is explained in the FAQ, heres a link.

http://faq.cprogramming.com/cgi-bin/...&id=1043284351
• 08-07-2003
RealityFusion
Thanx, I forgot to look for it before I asked. :D
• 08-07-2003
RoD
Quote:

Originally posted by RealityFusion
Thanx, I forgot to look for it before I asked. :D
No big deal
• 08-07-2003
RealityFusion
I read the faq on namespace but I still don't understand why just putting
Code:

`# include <iostream.h>`
is not recommended. It seems a lot more convienent just to use the .h extension as it saves time.
• 08-07-2003
RealityFusion
I think I get it now. If I include these files
Code:

```# include <iostream.h> # include <stdlib.h>```
then could I avoid using namespace? I am going to try it now.
• 08-07-2003
RealityFusion
Wow! That works and it's a time saver for me!
• 08-07-2003
jlou
Quote:

I read the faq on namespace but I still don't understand why just putting
Code:

`# include <iostream.h>`
is not recommended
Quote:

Wow! That works and it's a time saver for me!
Be careful. The reason it is not recommended for C++ programs is because it is a deprecated header for C++. If you want to program good standards conforming C++ (or close to it), then you will use the standard C++ headers (without the .h). If you'd prefer to do it the old way that might not compile some time in the near future, then nobody can stop you, but many people will remind you that it is a bad idea.

In my opinion, it is so easy to use the new header that the benefit of knowing and using the correct method far outweighs the tiny price of having to type std:: in front of everything, or in your case adding a single line of code to the top of your .cpp file.