Thank you everyone for your quick replies and help! To grumpy and King Mir, thank you for showing me how you would shorten it, I feel like writing shorter code comes with more experience because currently I find it really hard to initially write that way... I have to write it the long way it seems and then try to think of ways to shorten it... Like the one below it gets the job done but its much longer than it should be, at least I have a feeling it can be shortened a lot haha, I will keep working at it...
1. Take the "menu program" you wrote earlier and break it out into a series of calls to functions for
each of the menu items. Add the calculator and "100 bottles of beer" as two different functions
that can be called.
2. Make your calculator program perform computations in a separate function for each type of
computation.
Code:
#include <iostream>
#include <string>
using namespace std;
double add (double x, double y)
{
return x + y;
}
double mult (double x, double y)
{
return x * y;
}
double sub (double x, double y)
{
return x - y;
}
double div (double x, double y)
{
return x / y;
}
string selection;
int main()
{
while (selection != "Calculator" && selection != "99")
{
cout << "Please pick a selection from the following menu by typing it out and hitting enter:" <<endl;
cout << "Calculator" <<endl;
cout << "99" <<endl;
cin >> selection;
}
if (selection == "Calculator")
{
string op;
double x;
double y;
cout << "Please enter an arithmetic operator (+,-,*,/): ";
cin >> op;
cout << "Please enter a number: ";
cin >> x;
cout << "Please enter another number: ";
cin >> y;
if ( op == "+")
{
cout << "The sum is: " << add(x,y);
}
if ( op == "-")
{
cout << "The difference is: " << sub(x,y);
}
if ( op == "*")
{
cout << "The product is: " << mult(x,y);
}
if ( op == "/")
{
cout << "The dividend is: " << div(x,y);
}
}
if (selection == "99")
{
for ( int beer = 99; beer > 0; beer--)
{
cout << beer << " Bottles of Beer on the wall, " << beer << " Bottles of Beer, take one down and pass it around, " << beer - 1 << " Bottles of Beer on the wall." <<endl;
}
}
}