Help with problem

Ok,
so for my beginner C++ class, I was instructed to write a program with a bool function that returns true if the sum of two integers is greater than 10, and false if it is less than or equal to 10. For some reason, even when the sum is less than 10, it always returns true and says the sum is greater than 10. Any help is appreciated, thanks.

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
38
 #include<iostream>
  2 using namespace std;
  3 
  4 bool sumthemup(int first, int second);
  5 int first;
  6 int second;
  7 
  8 int main()
  9 {
 10   cout<<"Enter in two integers"<<endl;
 11   cin>>first;
 12   cin>>second;
 13   sumthemup(first,second);
 14   if(true)
 15   {
 16     cout<<"The sum is greater than 10"<<endl;
 17   }
 18   else
 19   {
 20     cout<<"The sum is less than or equal to 10"<<endl;
 21   }
 22   return 0;
 23 }
 24 
 25 bool sumthemup(int first, int second)
 26 {
 27   int sum;
 28   first + second == sum;
 29   if(sum > 10)
 30   {
 31     return true;
 32   }
 33   else
 34   {
 35     return false;
 36   }
 37 }
Line 13: You're ignoring the result of the bool function.
Line 14: if (true) is always true.

What you want is:
13
14
15
16
17
18
if (sumthemup(first,second))
{  // do something when true
}
else
{  // do something when false
}


Line 28 is bogus. Is should be:
 
  sum = first + second;


sumthemup can be simplified considerably:
25
26
27
bool sumthemup(int first, int second)
{  return (first + second) > 10;
}


Don't know how you got the line numbers inside your post. It's not necessary to do that. Code tags will number your code automatically.
Last edited on
Hi,

It looks like you wrote:
if(true)
{
cout<<"The sum is greater than 10"<<endl;
}

if(true) will always evaluate to true and it will always output "the sum is greater than 10"

Instead of what you wrote, use:
bool morethanten = sumthemup(first,second);
if(morethanten)
{
cout << "True" << endl;
}

Secondly, in sunthemup(int first, int second) you wrote:
first + second == sum;
It should be: first + second = sum;
You were using the comparison operator, not the assignment operator.
You were also putting "sum" on the wrong side; you should do it like this:
sum = first + second.
Now the program should work.
Thank you both for your help.
Topic archived. No new replies allowed.