simple greeting code, subtraction error.

Hi, i just started with c++ like 1½ hour ago, and I'm experimenting a bit.
I wrote a script that looks like this:
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
#include <iostream>


using std::cout;
using std::cin;

int main()
{
    char name[31];
    int age;
    int difference;
    int my_age=31;
    difference = (age - my_age);
    
    cout << "What's your name?\n";
    cin >> name;
    cout << "How old are you?\n";
    cin >> age;
    
    if (age == 31)
    {
              cout << "Nice to meet you " << name << ", I'm " << age << " years old aswell";
              }
              else
              {
                  cout << "Nice to meet you " << name << ", it differs " << difference << " years between us";
                  }
                 
                                    
    
    system("pause");
return 0;
}


When I type in for instance 41 as age it says: "Nice to meet you name, it differs 2009187012 years between us". I would like to know what I've done wrong. I would also like to know if I type in a number that's smaller than 31 how I can make the smallest number subtract from the larger one. I hope you understand what I mean.
I'm sure that there is likely a better way, but I just put difference in an if /else statement:

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
37
#include <iostream>


using std::cout;
using std::cin;

int main()
{
    char name[31];
    int age;
    int difference;
    int my_age=31;

    cout << "What's your name?\n";
    cin >> name;
    cout << "How old are you?\n";
    cin >> age;

    if (age <=30)
    {
        difference = my_age - age;
        cout<< "Hello "<< name<< " our ages differ "<< difference<< " years.\n";
    }
    else if (age >=32)
    {
        difference = age - my_age;
        cout<< "Hello "<< name<< " our ages differ "<< difference<< " years.\n";
    }
    else if (age == 31)
    {
        difference = 0;
        cout<< "Hello "<< name<< " we are the same age!!!\n";
    }

    system("pause");
return 0;
}
You assigned the value of (age-my_age) to difference before getting the the users age, so you ended up getting garbage for an answer.

There is an absolute value function abs(), that you can use to output the positive difference.
abs(12) = 12
and
abs(-13) = 13
To use it put #include<cmath> at the top of your code.
No, actually abs() is in "cstdlib" NOT "cmath".
Are you sure?
cmath wrote:
inline double
abs(double __x)
{ return __builtin_fabs(__x); }

inline float
abs(float __x)
{ return __builtin_fabsf(__x); }

inline long double
abs(long double __x)
{ return __builtin_fabsl(__x); }


Edit: Okay so there's actually an abs() function in both headers.

cstdlib wrote:
inline long long
abs(long long __x) { return __x >= 0 ? __x : -__x; }

inline long long
llabs(long long __x) { return __x >= 0 ? __x : -__x; }
Last edited on
Right, and only the one in cstdlib exists in C, and it should be an int.
http://www.cprogramming.com/fod/abs.html
Last edited on
Thanks for all the replies, I prefer Danny Toledos method since that was closest to my "knowledge" at the time. I now saw what I did wrong, thanks for pointing that out Browni3141.
Topic archived. No new replies allowed.