// Allows the user to select a cold sandwich
cout << "Select a sandwich number from Chuckie's Kitchen Cold Sandwiches: ";
cin >> ColdSandwichesWithChips;
So what are the things you're doing in your program?
- Display the menu choices
- Select a cold sandwich
- Select a side order
- Select a beverage
- Compute the bill
Do those sound like things that can be put into functions?
PLEASE USE CODE TAGS (the <> formatting button) when posting code. http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.
Yes I did the first part which is this program, now I have to break it into function calls and prompt the user to exit the program after he take the order. Then print the receipt of what he ordered.
It says exactly: In Lab 2 you will modularize Lab 1 (which is the above program). This means, you will break down the various components of the program into functions. You should also prompt the user for an order until the user chooses to exit the program. This time, be sure to print a receipt that includes what the user ordered and the item's cost. At the top of the receipt, add the name and address of the restaurant. (Note: You do NOT need to implement using arrays).
I did everything needed for the program, but I have errors which say that some variables were not declared though I declared them. If you may please check the reason; here's my program:
#include <iostream>
#include <iomanip>
using namespace std;
int ColdSandwichesWithChips, SideOrder, BeveragesAndWater;
void FinalBill (double TotalBill, double ColdSand, double SiOr, double Bev);
int ColdSands (int);
int SideOrders (int);
int Beverages (int);
// Allows the user to select a cold sandwich
cout << "Select a sandwich by its number from Chuckie's Kitchen Cold Sandwiches: ";
cin >> ColdSandwichesWithChips;
// Allows the user to select a beverage
cout << "Select a beverage by its number from Chuckie's Kitchen Beverages & Water: ";
cout << "\n";
cin >> BeveragesAndWater;
48 1 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
64 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'ColdSand' was not declared in this scope
68 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'ColdSand' was not declared in this scope
72 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'ColdSand' was not declared in this scope
76 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'ColdSand' was not declared in this scope
80 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'ColdSand' was not declared in this scope
85 1 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
101 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'SiOr' was not declared in this scope
105 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'SiOr' was not declared in this scope
109 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'SiOr' was not declared in this scope
113 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'SiOr' was not declared in this scope
118 1 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
139 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
143 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
147 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
151 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
155 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
159 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
163 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
167 3 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'Bev' was not declared in this scope
172 1 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
184 44 C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'TotalBill' was not declared in this scope
You can't define functions within functions. You can call a function in main, but the definition of that function is going to be separate and outside of the main opening and closing curly braces.
In the function prototype for FinalBill, the code says the function will receive four double parameters, but those variables are never actually defined in the main function. (You did have them defined in your original post) void FinalBill (double TotalBill, double ColdSand, double SiOr, double Bev);
Also - your function prototypes don't match up with the function definitions later on in terms of return type and parameters sent.
void FinalBill (double TotalBill, double ColdSand, double SiOr, double Bev);
void FinalBill()
int ColdSands (int);
void ColdSands()
int SideOrders (int);
void SideOrders()
int Beverages (int);
void Beverages()
Okay so I took ColdSand, SiOr and Bev to the main function, should I take total bill too and just put:
void FinalBill(); or void FinalBill(TotalBill); ?
How should I place the tokens?
it also says:
C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] a function-definition is not allowed here before '{' token
C:\Users\hometech\Documents\WElHouraniLab1.cpp [Error] 'TotalBill' was not declared in this scope
I did everything and made major changes; my program is compiling but when black box opens it tells that: process exited with return value 0.
**I tried removing the return 0; nothing changed. Here is my program:
#include <iostream>
#include <iomanip>
using namespace std;
void FinalBill (double ColdSand, double SiOr, double Bev);
double TotalBill;
int ColdSandwichesWithChips, SideOrder, BeveragesAndWater;
int ColdSands (int);
int SideOrders (int);
int Beverages (int);
double ColdSand,
SiOr,
Bev;
// Allows the user to select a cold sandwich
cout << "Select a sandwich by its number from Chuckie's Kitchen Cold Sandwiches: ";
cin >> ColdSandwichesWithChips;
}
// Allows the user to select a beverage
cout << "Select a beverage by its number from Chuckie's Kitchen Beverages & Water: ";
cout << "\n";
cin >> BeveragesAndWater;
}
//Displays the total bill
void FinalBill()
{
TotalBill = ColdSand + SiOr + Bev;
cout << "\n";
cout << "Your Receipt:";
cout << "\n\n\t\t\t Chuckie's KITCHEN" << endl;
cout << "\n";
cout << "\t\t\t 245 GREEN ST. \n\n";
cout << "\t\t at 4th Ave & Green St, Albany, NY \n\n";
cout << "\t\t Store Telephone #: 449-0720 \n\n\n";
cout << fixed << "Your total bill is $" << TotalBill << "." << endl;
cout << "\n\n\n";
cout << "Come Again!:)" << endl;
}
You still need to call the functions from main to have them execute. Basically this is the order the different sections go in.
// include statements
// function prototypes (must match how you call function in main and define function in terms of return type and parameters)
// main function
// function definitions
Here's an example using one of the functions you have just to illustrate how it might work.
#include <iostream>
usingnamespace std;
//function prototype
int ColdSands(); //function doesn't take a parameter but does return the value of the option person selected from the menu
//main function - program entry
int main()
{
int ColdSandwichesWithChips; //variable to hold result of ColdSands function
//store value returned from Sandwich menu function in ColdSandwichesWithChips
ColdSandwichesWithChips = ColdSands();
if (ColdSandwichesWithChips == 1)
//... the if/else statements
return 0;
}
//function definition
int ColdSands()
{
int n; // declare variable to store menu choice
cout << "COLD SANDWICHES w/chips: \n\n";
cout << "1- TURKEY w/mayo, lettuce, tomato, onion, & slice of cranberry sauce \n";
cout << "2- ROAST BEEF w/Russian dressing \n";
cout << "3- HAM & CHEESE \n";
cout << "4- TUNA w/mayo, lettuce, tomato \n";
cout << "5- DELUXE MIX w/lettuce, tomato, onion, dressing \n";
cout << "\n\n";
// Allows the user to select a cold sandwich
cout << "Select a sandwich by its number from Chuckie's Kitchen Cold Sandwiches: ";
cin >> n;
//return value selected back to main function
return n;
}
May I PLEASEEEE know what's wrong about this? I have to turn this in in 50 minutes or I'll score a 0. Please help pleaseeee!
#include <iostream>
#include <iomanip>
using namespace std;
//Declaring variables
void FinalBill (double ColdSand, double SiOr, double Bev);
double TotalBill;
int ColdSands (int);
int SideOrders (int);
int Beverages (int);
double ColdSand,
SiOr,
Bev;
//Main function
int main()
{
int ColdSandwichesWithChips,
SideOrder,
BeveragesAndWater;
ColdSandwichesWithChips = int ColdSands();
SideOrders = int SiOr();
Beverages = int Bev();
// Allows the user to select a cold sandwich
{
cout << "Select a sandwich by its number from Chuckie's Kitchen Cold Sandwiches: ";
cin >> ColdSandwichesWithChips;
return ColdSandwichesWithChips;
}
// Allows the user to select a side order
{
cout << "Select a side order by its number from Chuckie's Kitchen Side Orders: ";
cin >> SideOrder;
return SideOrder;
}
// Allows the user to select a beverage
{
cout << "Select a beverage by its number from Chuckie's Kitchen Beverages & Water: ";
cout << "\n";
cin >> BeveragesAndWater;
return BeveragesAndWater;
}
//Displays the total bill
void FinalBill()
{
TotalBill = ColdSand + SiOr + Bev;
cout << "\n";
cout << "Your Receipt:";
cout << "\n\n\t\t\t Chuckie's KITCHEN" << endl;
cout << "\n";
cout << "\t\t\t 245 GREEN ST. \n\n";
cout << "\t\t at 4th Ave & Green St, Albany, NY \n\n";
cout << "\t\t Store Telephone #: 449-0720 \n\n\n";
cout << fixed << "Your total bill is $" << TotalBill << "." << endl;
cout << "\n\n\n";
cout << "Come Again!:)" << endl;
}