why is this not returning 4

I'm really confused as to why this is not returning 4. Help please :-)

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

int difference(int a, int b);

int main()
{
int currentYear = 2018;

int graduationYear = 2022;

difference(currentYear,graduationYear);

std::cout << "The year is : " << currentYear << " \nYou graduate in year : "<< graduationYear<< " You have : "<<difference << " years to graduate.\n";

}

int difference(int a,int b)
{
int theDifference;
theDifference = (a-b);

return theDifference;
}
-- INSERT --                                                  1,19          Top
Last edited on
You don't do anything with the return value.

You either have to store the return value in a variable and then print it.

1
2
int yearsLeftTograduation = difference(graduationYear, currentYear);
std::cout << "You have : " << yearsLeftTograduation << " years to graduate.\n";

Or you can call the function directly when printing.

 
std::cout << "You have : " << difference(graduationYear, currentYear) << " years to graduate.\n";
Last edited on
Topic archived. No new replies allowed.