I am attempting to take a program that I have previously written and break it out into a series of calls to functions. However, when the "menu" program is selected, it prints twice before any data is entered. Been scratching my head for the past few days on this one. Someone please tell me what I'm doing wrong.
Code:
#include <iostream>#include <string>
using namespace std;
int menu()
{
string entree_selected;
do
{
cout << "\nPlease select one of the following options as your meal for this evening:" << endl;
cout << "- Prime Rib" << endl;
cout << "- Lasagna" << endl;
cout << "- Smoked Salmon" << endl;
cout << "Enter your option: ";
getline ( cin, entree_selected, '\n');
} while ( entree_selected != "Prime Rib" && entree_selected != "Lasagna" && entree_selected != "Smoked Salmon" );
if ( entree_selected == "Prime Rib" )
{
cout << "\nThank you. Enjoy your meal." << endl;
}
else if ( entree_selected == "Lasagna" )
{
cout << "\nThank you. Enjoy your meal." << endl;
}
else if ( entree_selected == "Smoked Salmon" )
{
cout << "\nThank you. Enjoy your meal." << endl;
}
}
int calculator( )
{
float first_argument;
float second_argument;
cout << "\nPlease enter a number: ";
cin >> first_argument;
cout << "\nPlease enter another number: ";
cin >> second_argument;
cout << "\n" << first_argument << " + " << second_argument << " = " << first_argument + second_argument << endl;
cout << "\n" << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
cout << "\n" << first_argument << " * " << second_argument << " = " << first_argument * second_argument << endl;
cout << "\n" << first_argument << " / " << second_argument << " = " << first_argument / second_argument << endl;
}
void song()
{
int i = 99; // Bottle count
while ( i > 1 )
{
cout << i << " bottles of beer on the wall." << endl;
cout << i << " bottles of beer." << endl;
i--;
cout << "Take one down. Pass it around." << endl;
cout << i << " bottles of beer on the wall.\n" << endl;
if ( i == 2 )
{
cout << "2 bottles of beer on the wall." << endl
<< "2 bottles of beer." << endl
<< "Take one down. Pass it around." << endl
<< "1 bottle of beer on the wall.\n" << endl;
cout << "1 bottle of beer on the wall." << endl
<< "1 bottle of beer." << endl
<< "Take one down. Pass it around." << endl
<< "No more bottles of beer on the wall.\n" << endl;
cout << "No more bottles of beer on the wall." << endl
<< "No more bottles of beer." << endl
<< "Go to the store. Buy some more." << endl
<< "99 bottles of beer on the wall.\n" << endl;
break;
}
}
}
int main ()
{
string program_selected;
cout << "Please select one of the following programs. 1) menu, 2) calculator or 3) song: ";
cin >> program_selected;
if ( program_selected == "menu" )
{
menu();
}
else if ( program_selected == "calculator" )
{
calculator();
}
else if ( program_selected == "song" )
{
song();
}
else
{
cout << "\nInvalid entry." << endl;
}
}