Thread: My adventure in C++

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Question My adventure in C++

    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
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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?

  4. #4
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    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>
    and add using namespace std;

    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;
    }

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    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;
    }

  6. #6
    1479
    Join Date
    Aug 2003
    Posts
    253
    Guess I was just overlooking that . 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.
    Knowledge is power and I want it all

    -0RealityFusion0-

  7. #7
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Namespace is explained in the FAQ, heres a link.

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  8. #8
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thanx, I forgot to look for it before I asked.
    Knowledge is power and I want it all

    -0RealityFusion0-

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by RealityFusion
    Thanx, I forgot to look for it before I asked.
    No big deal

  10. #10
    1479
    Join Date
    Aug 2003
    Posts
    253
    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.
    Knowledge is power and I want it all

    -0RealityFusion0-

  11. #11
    1479
    Join Date
    Aug 2003
    Posts
    253
    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.
    Knowledge is power and I want it all

    -0RealityFusion0-

  12. #12
    1479
    Join Date
    Aug 2003
    Posts
    253
    Wow! That works and it's a time saver for me!
    Knowledge is power and I want it all

    -0RealityFusion0-

  13. #13
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    I read the faq on namespace but I still don't understand why just putting
    Code:
    # include <iostream.h>
    is not recommended
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a fun point-and-click mystery adventure game.
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-13-2008, 07:55 AM
  2. Adventure game
    By MOH123 in forum C++ Programming
    Replies: 7
    Last Post: 09-13-2005, 11:01 PM
  3. Text adventure Idea...
    By none in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2002, 01:06 PM
  4. text adventure
    By BuRtAiNiAn FlY in forum C Programming
    Replies: 1
    Last Post: 09-05-2001, 09:39 PM