I read the the welcome post and I will attempt to sound the least bit like a semi-literate boob as possible. I am doing an assignment for my class at school. Very first programming class, very first semester of college. I am trying hard to understand all of this information. The goal of the code is to have the user choose and operation they would like to use then two numbers that go along with the operation. If he wanted to multiply 3 * 7, they would simply type multiplication, then enter the two numbers. I am stuck on the 'if statements' that would exclude the other operations after one is chosen. Any assistance would be greatly appreciated.
int main()
{
cout<< "Please choose the operation you would liket to use\n";
string desired_operation;
cin>>
if (string plus is chosen, go to addition) {
i
}
{string addition;
double a;
double b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a + b;
cout<<"Result is"<<" "<<result<<"\n";
return 0;}
{ string subtraction;
double a;
double b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a - b;
cout<<"Result is"<<" "<<result<<"\n";
return 0;}
{string multiplication;
double a;
double b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a * b;
cout<<"Result is"<<" "<<result<<"\n";
return 0;}
{string division;
double a;
double b;
cout<<"Enter first number:\n";
cin >> a;
cout <<"Enter the second number:\n";
cin>> b;
double result = a / b;
cout<<"Result is"<<" "<<result<<"\n";
return 0; }
}
The way I would approach this is to read all of the data at the beginning then do the check and perform the operation:
- Read operation character
- Read first operand integer
- Read second operand integer
- If operation character is '+':
- Print result of addition
- Else if operation character '-':
- ...and so on
#include <iostream>
#include <string>
int main()
{
std::cout << "Please choose the operation you would like to use\n"
<< "add/subtract/multiply/divide (all lower case characters)\n" ;
std::string desired_operation ;
std::cin >> desired_operation ;
std::cout << "Please enter the two numbers you would like to " << desired_operation << '\n' ;
double a ;
double b ;
std::cin >> a >> b ;
double result = 0 ;
char symbol = '?' ;
// as of now, we accept only lower case characters in desired_operation
// if desired_operation is "add"
// ie. if the value of ( desired_operation == "add" ) is true
if( desired_operation == "add" ) /* then */ { result = a + b ; symbol = '+' ; }
// otherwise (else), if desired_operation is "subtract"
elseif( desired_operation == "subtract" ) { result = a - b ; symbol = '-' ; }
elseif( desired_operation == "multiply" ) { result = a * b ; symbol = '*' ; }
elseif( desired_operation == "divide" ) { result = a / b ; symbol = '/' ; } // assume for now that b is not zero
else // if desired_operation is not one of the above
{
std::cout << "the operation '" << desired_operation << "' is not supported\n" ;
return 1 ; // by convention, returning a non-zero value from main() indicates failure
}
std::cout << '\n' << a << ' ' << symbol << ' ' << b << " == " << result << '\n' ;
// return 0 ; is implied here. by convention, returning zero from main() indicates success
}