having trouble with exercise please help.

I am completely lost. I dont know if i'm using the correct format for this program. This is just an excerise program. I'm studying and I can't seem to figure this out. the excersie ask to write a program that inputs 3 integers and outputs either the word yes or the word no. program should output yes when any of the integers equals the sum of the other two intergers. and output no if there is no number that is the sum of the other two. pleas help!!! I have included the code i'm working on but I know its not correct because it wont run.

#include <iostream>
#include <string>

using namespace std;

int main(void)
{
// input
cout << "enter three numbers";
cout << "seperated by spaces" << endl;

int number1;
int number2;
int number3;
cin >> number1 >> number2 >> number3;
cin.ignore(99,'\n');

// process

int sum1 = number1 + number2;
int sum2 = number1 + number3;
int sum3 = number2 + number3;
int sum4 = number2 + number1;
int sum5 = number3 + number1;
int sum6 = number3 + number2;

//
if (sum1 == number3)
if (sum2 == number2)
if (sum4 == number3)
if (sum3 == number1)
if (sum6 == number1)
if (sum5 == number2)

bool yes = sum1 == number3 || sum2 == number2
|| sum4 == number3
||sum3 == number1
|| sum6 == number1
|| sum5 == number2;
string word;
if (yes) word = "Yes";
else word = "no";

cout << word << endl;

cout << endl << "press enter to finsih..";
cin.ignore(99,'\n');
return 0;
}
ariana2608

Code tags please - the <> button on the right.


1
2
3
4
5
6
if (sum1 == number3)
if (sum2 == number2)
if (sum4 == number3)
if (sum3 == number1)
if (sum6 == number1)
if (sum5 == number2)


Look on the reference pages - top left of this page - to see how if statements work.

They must have an action (statements) to carry out if the conditional is true, and optionally an action to carry out if false.

Good luck!!

Also - you need semicolons at the end of each statement.
Last edited on
You are almost right just remove this from your code
1
2
3
4
5
6
if (sum1 == number3)
if (sum2 == number2)
if (sum4 == number3)
if (sum3 == number1)
if (sum6 == number1)
if (sum5 == number2)

This will allow bool to initialize iff sum5 == number2....
That's what causing you trouble for your program....
one suggestion is there rather than using string word,
you can directly use
1
2
3
4
if(yes)
   cout<<"Yes";
els
    cout<<"No";

This will be more efficient one.
Thanks for your help!! I think my program works now. I was having such a hard time figuring out what I was doing wrong. Not only that this whole if statements and booleans is kinda tricky. Thanks again!
Topic archived. No new replies allowed.