+ 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; }
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.
+ 3
You should make the sum after inputs
...
+ 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;
+ 1
keeps giving the wrong sum
+ 1
???!!!##*?
0
sorry to tell that your question is wrong
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.
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