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 functionsthat can be called.
I tried this and I don't understand why it isn't working. I even replaced
"int menu()", "int song()", and "int calc()" with their raw script and it worked fine. If someone could help me with this and help me understand a little bit more about functions I would really appreciate it.
Note that I have tested each of the other individual functions and they all work perfectly.
Code:
#include <iostream>
#include <string>
using namespace std;
int song();
int calc();
int menu();
int main()
{
string choice;
cout << "Choose: 1. Song, 2. Calculator. 3. Menu.\n";
cin >> choice;
if(choice=="1")
{
int song();
}
else if(choice=="2")
{
int calc();
}
else if(choice=="3")
{
int menu();
}
else
{
cout << "Invalid entry.";
}
}
int song()
{
for(int i=99;i>0;i--)
{
cout << i << " bottles of beer on the wall. " << i << " bottles of beer.\n";
cout << "Take one down and pass it around. " << i-1 << " bottles of beer on the wall.\n";
}
}
int calc()
{
double x;
double y;
string input;
cout << "Enter your first number.\n";
cin >> x;
cout << "Enter your arithmetic operator (arithmetic operators are +, -, *, or /).\n";
cin >> input;
cout << "Enter your second number.\n";
cin >> y;
if(input=="+")
{
cout << x << " + " << y << " = " << x+y << ".";
}
else if(input=="-")
{
cout << x << " - " << y << " = " << x-y << ".";
}
else if (input=="*")
{
cout << x << " * " << y << " = " << x*y << ".";
}
else if (input=="/")
{
cout << x << " / " << y << " = " << x/y << ".";
}
else
{
cout << "Invalid data.\n";
}
}
int menu()
{
string choice;
while(true)
{
cout << "Choose a color: Red, Blue, or Green.\n";
cin >> choice;
if(choice=="Red")
{
cout << "You chose red.\n";
break;
}
else if(choice=="Blue")
{
cout << "You chose blue.\n";
break;
}
else if(choice=="Green")
{
cout << "You chose green.\n";
break;
}
else
{
cout << "Invalid input.\n";
}
}
cout << "Thank you for using my program.\n";
}