unexpected and unwanted output

hey, I'm having problems with coding this program. the output is not what i wanted. any ideas?

this is the question:
The manager of the Diner by the Valley restaurant plans to have a computer available for
students to place their orders when they enter the campus centre. They have a limited menu
selection available that includes sandwiches and drinks. The manager wants the computer screen
to allow the students to select from a list of available sandwich and drink options. A large board
is located on the wall at the location where the computer will reside. It shows the menu of items
available and lists the price of each item.
In addition to white bread, they have whole wheat and rye bread. No charge is included for
different bread types; however, they do consider that selection a special request. The interface
should enable special requests such as bread types to be selected. Students should be able to
enter other special requests such as "Hold the onion" or "Hold the mayo". Water is available for
each order at no charge, but it must be ordered.
Prices are as follows:
• Sandwich = $2.00
• Tea = $1.80,
• Coffee = $2.30
• Juice = $1.50.
The manager needs a user-friendly interface where a student can choose options easily and at the
end, the program should display the current order selections and show the total cost of the order.
Refer to the scenario given above and write codes that make extensive use of selection
statements (if-else) to create a program for Diner by the Valley restaurant.
A student should be able to make various selections using the program e.g. selections for
sandwiches and drinks, bread types, drinks, make a special request etc. The total should contain
the total value of the order the student has to pay.
Also, include a do-while loop that asks the user whether he wants to place another order. The
user response of y or Y should allow for another transaction while any other response should
terminate the program.

this is my code:
using namespace std;

float getMenuItems();
float getBreadType();
float getSpecialRequest();
float getWater();
float getTotal();

int main()
float menu, type, request, water;
float total=0;
char y, n, key;

cout << "\n\t\tWelcome to Diner by the Valley Restuarant." << endl;
cout << "\t\t------------------------------------------" << endl;


cout << "\n\nYour Order:" << endl;
cout << "Menu Selected: " << menu << endl;

cout << "Bread Type: " << type << endl;

cout << "Special Request: " << request << endl;

cout << "Water Selected: " << water << endl << endl;

cout << "Your Total Charge is : $" << total << endl << endl;

cout << "Do you want to place another Order?\n(Press 'y' for Yes or any other key to Terminate): " ;
cin >> key;
if (key = y )
cout << key << endl;

while (key = n);

return 0;

float getMenuItems()
float menu, total = 0, drinks;
const float sandwhich = 2, tea = 1.80, coffee = 2.30, juice = 1.50;

cout << "\n\nPlease choose your Menu Items" << endl;
cout << "1.Sandwhich\n2.Drinks" << endl;
cout << "Choice: " ;
cin >> menu;

if (menu = 1)
cout << sandwhich;

else if (menu = 2)
cout << "Please choose your drink" << endl;
cout << "1.Tea\n2.Coffee\n3.Juice" << endl;
cin >> drinks;

if(drinks == 1)
drinks = drinks + tea;
else if(drinks == 2)
drinks = drinks + coffee;
else if(drinks == 3)
drinks = drinks + juice;
cout << "I'm sorry, your choice is invalid. Please try again." << endl;


return menu;

float getBreadType()
float type;

cout << "\nPlease choose the Bread Type" << endl;
cout << "1.White braed" << endl;
cout << "2.Wheat bread" << endl;
cout << "3.Rye bread" << endl;
cout << "Choice:";
cin >> type;

return type;

float getSpecialRequest()
float request;

cout << "\nPlease choose your special request" << endl;
cout << "1.Hold the onion" << endl;
cout << "2.Hold the mayo" << endl;
cout << "3.None" << endl;
cout << "Choice:";
cin >> request;

return request;

float getWater()
float water, y, n;

cout << "\nWould you prefer water as well? (Press 'y' for Yes and 'n' for No)";
cin >> water;

if (water = y)
cout << y;
else if (water = n)
cout << n;
cout << "Invalid choice.Please try again.";

return water;

float getTotal()
float total, drinks;
const float sandwhich = 2, tea = 1.80, coffee = 2.30, juice = 1.50;

total = total + sandwhich;
total = total + drinks;

return total;
Last edited on
What output are you getting - as against what is expected. What debugging of the code have you done to try to find out the issue? Where does this code differ from your tried program design?

PS. When posting code, please use code tags so that the code is readable and post formatted code.

// code goes here

float menu, type, request, water;

These variables are never set to a value - so when the are displayed they have whatever value happens to be in memory at their location. The functions return a value but the return value is never used. The return value needs to be assigned to a value. eg

menu = getMenuItems();

and similar.

if (key = y )

variable y has again not been initialised - so could have any value. Also, this is an assignment not an equality test. = is assignment, == is equality test. Do you mean:

if (key == 'y')

and similar in other parts of the code.

It's good practice to always initialise a variable when it is defined.
Last edited on
Registered users can post here. Sign in or register to post.