|
-
April 27th, 2017, 10:24 AM
#1
Overload function error
Now I just have issue figuring out how to add the quantities, as in if I make the same selection more than once it only calculates one.
Code:
//
//Preprocessor Directives
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
using namespace std;
//Menu Structure
struct menuOptions
{
int quantity;
double price;
string description;
};
//prototype
void displayMenu(menuOptions menu[], int size);
void getData(menuOptions menu[], int &size);
void printCheck(menuOptions menu[], int size);
//constants
const int max = 8;
const double taxes = 0.05;
// main function
int main()
{
menuOptions menu[max];
int size = 0;
getData(menu, size);
displayMenu(menu, size);
printCheck(menu, size);
system("pause");
// exit main
return 0;
}
//getting the data in to array
void getData(menuOptions menu[], int &size)
{
menu[0].description = "Plain Egg"; menu[0].price = 1.45; menu[0].quantity = 0;
menu[1].description = "Bacon and Egg"; menu[1].price = 2.45; menu[1].quantity = 0;
menu[2].description = "Muffin"; menu[2].price = 0.99; menu[2].quantity = 0;
menu[3].description = "French Toast"; menu[3].price = 1.99; menu[3].quantity = 0;
menu[4].description = "Fruit Basket"; menu[4].price = 2.49; menu[4].quantity = 0;
menu[5].description = "Cereal"; menu[5].price = 0.69; menu[5].quantity = 0;
menu[6].description = "Coffee"; menu[6].price = 0.50; menu[6].quantity = 0;
menu[7].description = "Tea"; menu[7].price = 0.75; menu[7].quantity = 0;
size = 8;
}
//display menu and tell user what to do
void displayMenu(menuOptions menu[], int size)
{
cout << "Welcome to Johnny's Restaurant" << endl;
cout << setw(20) << "Johnny's Menu" << endl;
for (int i = 0; i < size; i++)
{
cout << (i + 1) << ". " << left << setw(21) << menu[i].description << "$" << setw(6) << setprecision(2) << fixed << menu[i].price << endl;
}
cout << "------------------------------" << endl;
int number;
int quantity;
cout << "\nSelect your item 1-8. Press 9 when you are finished: ";
cin >> number;
while (number != 9)
{
if (number >= 1 && number <= 8)
{
menu[number -1].quantity = 1;
}
else
{
cout << "Invalid Option! Item selection 1-8, and press 9 to finish your order!" << endl;
}
cout << "Select your item 1-8. Press 9 when you are finished: ";
cin >> number;
}
}
//calc and print check
void printCheck(menuOptions menu[], int size)
{
double totalPrice = 0;
double totalTax = 0;
cout << "\nYour Check is:" << endl;
cout << endl << "Welcome to Johnny's Restaurant" << endl;
for (int i = 0; i < size; i++)
{
if (menu[i].quantity > 0)
{
cout << left << setw(2) <<menu[i].quantity <<setw(22) << menu[i].description << "$" << setw(12) << setprecision(2) << fixed << menu[i].price << endl;
totalPrice += menu[i].price;
}
}
totalTax = totalPrice * taxes;
cout << setw(24) << "Tax:" << "$" << setw(14) << setprecision(2) << fixed << totalTax << endl;
cout << setw(24) << "Amount Due:" << "$" << setw(14) << setprecision(2) << fixed << (totalPrice + totalTax) << endl << endl;
}
}
Last edited by TheJollyRoger; April 28th, 2017 at 01:22 PM.
-
April 27th, 2017, 11:07 AM
#2
Re: Overload function error
 Originally Posted by TheJollyRoger
I am getting the error of overload and undefined with the lines underneath, but I don't know why.
Code:
void displayMenu(menuOptions menu[], int &size);
void getData(menuOptions menu[], int &size);
void printCheck(menuOptions menu[], int &size);
Code:
//
...
//prototype
void displayMenu(menuOptions menu[], int &size);
void getData(menuOptions menu[], int &size);
void printCheck(menuOptions menu[], int &size);
//constants
const int max = 8;
const double taxes = 1.05;
...
//display menu and tell user what to do
void displaymenu(menuOptions menu[], int size)
{
...
}
You declared
Code:
void displayMenu(menuOptions menu[], int &size);
but implemented
Code:
void displaymenu(menuOptions menu[], int size);
Don't you see the differences?
Last edited by VictorN; April 27th, 2017 at 11:20 AM.
Reason: code tags corrected
Victor Nijegorodov
-
April 27th, 2017, 11:14 AM
#3
Re: Overload function error
Code:
void displayMenu(menuOptions menu[], int &size);
void printCheck(menuOptions menu[], int &size);
...
void displaymenu(menuOptions menu[], int size) {}
void printCheck(menuOptions menu[], int size) {}
The forward declaration needs to match the definition. In these cases the declaration for size is by ref and the definition is by value. Also the spelling of the declaration and definiton for displaymenu!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 27th, 2017, 03:49 PM
#4
Re: Overload function error
-
April 27th, 2017, 03:50 PM
#5
Re: Overload function error
I see it now, I appreciate it, thanks.
-
April 27th, 2017, 04:04 PM
#6
Re: Overload function error
When I try to add several of the same item the program only calculates it once, how can I change that?
-
April 28th, 2017, 12:59 AM
#7
Re: Overload function error
 Originally Posted by TheJollyRoger
When I try to add several of the same item the program only calculates it once, how can I change that?
You have to debug your code to see what, where, and why goes wrong/unexpected!
Victor Nijegorodov
-
April 28th, 2017, 02:24 AM
#8
Re: Overload function error
Some comments on the code posted in post #1
Code:
// Press 9 when you are finished
while (number != -1)
???
Code:
//Select your item 1-8
if (number >= 1 && number <= 8)
Doesn't the size argument stipulate how many items there are on the menu?
Code:
menu[number - 1].quantity = 1;
What about if more than one quantity is wanted?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
April 28th, 2017, 12:32 PM
#9
Re: Overload function error
I have updated the code, and now I am working on how to make the same selection multiply times.
-
April 28th, 2017, 12:36 PM
#10
Re: Overload function error
 Originally Posted by TheJollyRoger
I have updated the code, and now I am working on how to make the same selection multiply times.
Good! Now see my post#7, learn the debugger! Good luck!
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|