+ 3

Some help please

#include <iostream> using namespace std; int main() { int a, b, sum; sum = a+b; cout << "Enter a number?\n"; cin >> a; cout <<"First number is:" << a << endl; cout << "Enter a second number?\n"; cin >> b; cout <<"Second number is:" << b << endl; cout << "The sum of your first and second number i>s:" << sum << endl; if (sum > 19) { cout << "Your sum is greater than 19"; } return 0; }

5th Mar 2017, 1:45 PM
jthe45
8 Respostas
+ 14
Put sum = a+b; below both user inputs. Program runs from top to bottom. Adding a and b prior to user input will result in junk values.
5th Mar 2017, 1:53 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
You should make the sum after inputs ...
5th Mar 2017, 1:52 PM
Alejandro Serrato
Alejandro Serrato - avatar
+ 2
initialize sum to zero during its declaration to avoid garbage values being stored in it. even though c++ supports dynamic initialization, i recommended defining variables where appropriate. try this.... int sum=0; then later after inputs and out puts of a and b, define sum as ...sum=a+b;
5th Mar 2017, 9:56 PM
Michael Jaroya
Michael Jaroya - avatar
+ 1
keeps giving the wrong sum
5th Mar 2017, 1:45 PM
jthe45
+ 1
???!!!##*?
5th Mar 2017, 1:46 PM
jthe45
0
sorry to tell that your question is wrong
7th Mar 2017, 1:05 PM
Roshan Singha
Roshan Singha - avatar
0
please help figure out whats wrong with this code #include<iostream> using namespace std; int main() { int sum; int diff; int prod; int div; int x,y; char opt; char a,b,c,d; cout <<"Please enter a number:" <<endl; cin >>x; cout <<"Enter another number:" <<endl; cin >>y; cout <<"Select if you want to add,subtract,multiply or divide the two numbers:" <<endl; cout <<"a:addition" <<endl; cout <<"b:subtract" <<endl; cout <<"c:multiply" <<endl; cout <<"d:divide" <<endl; cin >> a; cin >> b; cin >> c; cin >> d; if (opt = a) { sum = x+y; cout <<"The sum is:" <<sum <<endl; } else if (opt = b) { diff = x-y; cout <<"The difference is:" <<diff <<endl; } else if (opt = c) { prod = x*y; cout<<"The product is:" <<prod <<endl; } else if (opt = d) { div = x/y; cout<<"The divisant is:" <<div <<endl; } else { cout<<"You did not select any of the above." << endl; cout<<"Thank you try again." <<endl; } /* else { cout<<"wahh" <<endl; }*/ return 0; } only the addition option works thanks.
18th Mar 2017, 7:29 PM
jthe45
0
have another question pliz check it out #include<iostream> using namespace std; int main() { int sum; int diff; int prod; int div; int x,y; char opt; char a,b,c,d; cout <<"Please enter a number:" <<endl; cin >>x; cout <<"Enter another number:" <<endl; cin >>y; cout <<"Select if you want to add,subtract,multiply or divide the two numbers:" <<endl; cout <<"a:addition" <<endl; cout <<"b:subtract" <<endl; cout <<"c:multiply" <<endl; cout <<"d:divide" <<endl; cin >> a; cin >> b; cin >> c; cin >> d; if (opt = a) { sum = x+y; cout <<"The sum is:" <<sum <<endl; } else if (opt = b) { diff = x-y; cout <<"The difference is:" <<diff <<endl; } else if (opt = c) { prod = x*y; cout<<"The product is:" <<prod <<endl; } else if (opt = d) { div = x/y; cout<<"The divisant is:" <<div <<endl; } else { cout<<"You did not select any of the above." << endl; cout<<"Thank you try again." <<endl; } /* else { cout<<"wahh" <<endl; }*/ return 0; } help the add function works but not the rest
18th Mar 2017, 7:33 PM
jthe45