#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int sum;
string userResponse;
string userinput;
cout << "Please enter a number: \n";
cin >> a;
cout << "Now choose what you want to do: +, -, *, %.\n";
cin >> userinput;
if(userinput == "+")
{
cout << "Now choose another number: \n";
cin >> b;
sum = a + b;
cout << "The total of those numbers are: " << sum << endl;
}
if(userinput == "-")
{
cout << "Now choose another number: \n";
cin >> b;
sum = a - b;
cout << "The total of those numbers are: " << sum << endl;
}
if(userinput == "*")
{
cout << "Now choose another number: \n";
cin >> b;
sum = a * b;
cout << "The total of those numbers are: " << sum << endl;
}
if(userinput == "%")
{
cout << "Now choose another number: \n";
cin >> b;
sum = a % b;
cout << "The total of those numbers are: " << sum << endl;
}
if(sum > 20){
cout << "\nHa, your number is greater than 20 \n";
}
if(sum < 20){
cout << "\nCan't catch me out, your number is less than 20 \n";
}
if(sum == 20){
cout << "\nYour number is equal to 20 \n";
}
cout << "\nDoes this calculator work correctly? (Yes/No) \n";
cin >> userResponse;
if(userResponse == "Yes"){
cout << "Thanks for your honesty \n";
}
if(userResponse == "No"){
cout << "I know it works so stop lying";
}
}
But as you can see I have 9 if statements and to me that is a bit excessive, so I was wondering is there anyway to shorten it down by using less if statements. And if so, how?
You need to write the code for every possibility, so in that sense the answer is no.
However, there are more efficient ways to do what you want. The first is to take the code that you're using for every "if", and move it outside. This way you don't have to write it over and over again. Another thing to do is to replace those "if"s with "else if"s. This makes it so when the program finds what it's looking for, it doesn't keep looking. Another approach is to make userInput a char and use a switch case, which I find easier to read (and are faster than a bunch of "if/else if" for the computer).
cout << "Now choose another number: \n";
cin >> b;
if(userinput == "+")
{
sum = a + b;
}
elseif(userinput == "-")
{
sum = a - b;
}
elseif(userinput == "*")
{
sum = a * b;
}
elseif(userinput == "%")
{
sum = a % b;
}
cout << "The total of those numbers are: " << sum << endl;
cout << "Now choose another number: \n";
cin >> b;
switch(userinput[0])
{
case'+' : sum = a + b; break;
case'-' : sum = a - b; break;
case'*' : sum = a * b; break;
case'/' : sum = a / b; break;
default: reportError(); // to do
}
cout << "The total of those numbers are: " << sum << endl;
Thanks both of you guys for the help :D Now the user only needs to enter a calculation, and its also not limited to one calculation like how it could only add or multiply at once.
#include <iostream>
usingnamespace std;
int main()
{
int a;
int b;
int sum;
string userResponse;
char userinput;
cout << "\t / = Divide, * = Multiply";
cout << "\n\nEnter a calculation: ";
cin >> a;
cin >> userinput;
cin >> b;
switch(userinput)
{
case'+' : sum = a + b;
break;
case'-' : sum = a - b;
break;
case'*' : sum = a * b;
break;
case'/' : sum = a / b;
}
cout << "\nThe total of those numbers are: " << sum << endl;
if(sum > 20){
cout << "\nHa, your number is greater than 20 \n";
}
if(sum < 20){
cout << "\nCan't catch me out, your number is less than 20 \n";
}
if(sum == 20){
cout << "\nYour number is equal to 20 \n";
}
cout << "\nDoes this calculator work correctly? (Yes/No) \n";
cin >> userResponse;
if(userResponse == "Yes"){
cout << "Thanks for your honesty \n";
}
if(userResponse == "No"){
cout << "I know it works so stop lying";
}
}
An after thought I just had, say instead of having
1 2
cout << "\nDoes this calculator work correctly? (Yes/No) \n";
cin >> userResponse;
I have
1 2
cout << "\nDo you want use this again? (Yes/No) \n";
cin >> userResponse;]
How would would I make so that if the user types in "Yes", the program will run again and if the user typed "No" it would close down. Is there some sort of loop for that? I thought about doing if statements but that's not exactly efficient, just ctrl+c and ctrl+v"ing" over and over. I think I remember watching a tutorial on something similar but I can't remember what is was.