I was trying to set up a simple calculator. I have no idea why it's not working
correctly. No matter what operation I input, it says input not accepted.
#include <iostream>
#include <string>
usingnamespace std;
int main()
// Prompts user to enter an operation followed by operands. Has Bugs
{
string operation = "???"; // Initiates string, operation. Can holds 4 common operations depending on human input.
string message = "???"; // Initiates string, message. Can hold 2 messages as a result of human input.
int answer = 0; // Initiates variable, answer
int left_input = 0; // Initiates variable, left_input
int right_input = 0; // Initiates variable, right_input
cout << "---------------------------Welcome!----------------------------" << endl;
cout << "\n"; // New line (Just for miniscule asthetics)
cout << "---------------------------Sign Key----------------------------" << endl; // Shows a key for humans who don't recognize certain operations.
cout << "\n";
cout << "Add = +" << endl;
cout << "Subtract = -" << endl;
cout << "Multiply = *" << endl;
cout << "Divide = /" << endl;
cout << "\n";
cout << "\n";
cout << "Please input left hand operand.\n"; // Prompts human for left hand operation input
cout << "\n";
cin >> left_input;
cout << "\n";
cout << "Please input right hand operand.\n"; // Prompts human for right hand operation input
cout << "\n";
cin >> right_input;
cout << "\n";
cout << "Please input the operation you wish to use.\n"; // Prompts human for operation input
cin >> operation;
cout << "\n";
if (operation == "+") // If typed operation is +, Initiate equation
message = "If you wish to continue, reboot this program";
answer = left_input + right_input;
if (operation == "-") // If typed operation is -, Initiate equation
message = "If you wish to continue, reboot this program";
answer = left_input - right_input;
if (operation == "*") // If typed operation is *, Initiate equation
message = "If you wish to continue, reboot this program";
answer = left_input * right_input;
if (operation == "/") // If typed operation is /, Initiate equation
message = "If you wish to continue, reboot this program";
answer = left_input / right_input;
if (operation != "+") // If typed operation is not + initiate message
message = "Input not accepted. Reboot Program";
if (operation != "-") // If typed operation is not - initiate message
message = "Input not accepted. Reboot Program";
if (operation != "*") // If typed operation is not * initiate message
message = "Input not accepted. Reboot Program";
if (operation != "/") // If typed operation is not / initiate message
message = "Input not accepted. Reboot Program";
cout << "Your answer is " << answer << "\n"; // Prints answer on screen
cout << message << endl; // message can either be a human unaccepted input error or message prompting restart for continued usage
return 0;
}
1) You perform all four arithmetic operations and the answer will always be a quotient.
2) Three of the statements checking the operator will always be true hence the message.
you else if statements of a switch. I would also reccommend inputting the operator as a character since you know its one character and not an array of characters.
#include <iostream>
usingnamespace std;
float division (float a, float b)
{
float r;
r=a/b;
return (r);
}
float multiplication (float a, float b)
{
float r;
r= a*b;
return (r);
}
float addition(float a, float b)
{
float r;
r=a+b;
return(r);
}
float subtraction(float a, float b)
{
float r;
r=a-b;
return(r);
}
int main()
{
char opselect;
bool playagain = true;
char playagainchar;
float ma;
float mb;
float ans;
cout<<"Welcome to my Calculator!\n\n";
while (playagain == true)
{
cout<<"Please select an operation:\n\n";
cout<<"D Division \nM Multiplication \nA Addition \nS Subtraction \n\n";
cout<<"Please use a letter to select the operation: ";
cin>>opselect;
if (opselect == 'D' || opselect == 'd')
{
cout<< "What two numbers would you like to divide: ";
cin>>ma;
cin>>mb;
ans = division (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
elseif (opselect == 'M' || opselect == 'm')
{
cout<< "What two numbers would you like to multiply: ";
cin>>ma;
cin>>mb;
ans = multiplication (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
elseif (opselect == 'A' || opselect == 'a')
{
cout<< "What two numbers would you like to add: ";
cin>>ma;
cin>>mb;
ans = addition (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
elseif (opselect == 'S' || opselect == 's')
{
cout<< "What two numbers would you like to subtract: ";
cin>>ma;
cin>>mb;
ans = subtraction (ma, mb);
cout<<"The answer is: "<<ans<<"\n";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
else
{
cout<< "Wrong Selection ";
cout<<"Would you like to do another Calculation (Y/N): ";
cin>>playagainchar;
cout<<"\n\n";
if(playagainchar != 'y') playagain = false;
}
}
cout<<"Thanks for calculating!";
cin.get();
return 0;
}
personally i would write a variation of the infamous stroustop calculator, but in the spirit of keeping it simple, the only thing i would change about giblits is do testing so it doesnt have to be ' ' seperated
i stand by what i said, however your code is not compliant with proper math laws bdanielz. it wont let you do 0 / 4. it will say that is in error, but its perfectly acceptable. its only an error if its number / 0.