Returning assigned string variable

ok, so I'm new to programming and currently trying to get a running knowledge of some basic C++. This is a practice program.

The order menu is called fine, but I cant get the last function to work. The goal is to have the user input what they want, then based off what their input is the "returnChoice" variable is assigned a message, after that its all returned and outputted in the main functon.

No errors are listed by the compiler or in running it, but after the user inputs what they want it just skips a line and ends. Here's the code.


#include <iostream>
#include <string>
using namespace std;
void Menu();
string ordrChoice(string);


int main ()
{
string Choice;
Menu();
ordrChoice(Choice);
cout << Choice << endl;

return 0;
}

void Menu ()
{
cout << "Welcome to the coffee house!" << endl
<< "=============================" << endl
<< "MENU:" << endl
<< "Basic Coffee Frappucino Gift Card" << endl
<< "Mocha Latte Pastry Coffee Beans" << endl
<<"What would you like to order?" << endl
<< "\n";

}

string ordrChoice (string returnChoice)
{
string aryOrdrChoice[] = { "Basic Coffee", "Frappucino", "Gift Card", "Mocha Latte", "Pastry", "Coffee Beans"};
getline(cin, returnChoice);


if (returnChoice == aryOrdrChoice[0])
{
string returnChoice = "You ordered a Basic Coffee";
return (returnChoice);
}
if (returnChoice == aryOrdrChoice[1])
{
string returnChoice = "You ordered Frappucino";
return (returnChoice);
}
if (returnChoice == aryOrdrChoice[2])
{
string returnChoice = "You ordered a Gift Card";
return (returnChoice);
}
if (returnChoice == aryOrdrChoice[3])
{
string returnChoice = "You ordered a Mocha Latte";
return (returnChoice);
}
if (returnChoice == aryOrdrChoice[4])
{
string returnChoice = "You ordered a Pastry";
return (returnChoice);
}
if (returnChoice == aryOrdrChoice[5])
{
string returnChoice = "You ordered Coffee Beans";
return (returnChoice);
}
else
{
string returnChoice = "Invalid choice. Please choose an item from the menu.";
return (returnChoice);
}
}
This is probably what you want:
1
2
3
4
5
6
7
8
9
10
int main ()
{
string Choice;
Menu();
//ordrChoice(Choice);//This returns a string. You want to use it with cout (or printf)
cout << ordrChoice(Choice) << endl;//ordrChoice(Choice) returns the string and puts it in cout.
cin.ignore();

return 0;
}
Thanks! guess it was just a matter of putting cout before the function name.
Topic archived. No new replies allowed.