Hello helena97,
I have not worked on the "pay" function yet. It was more necessary to get the order part right first.
These are the changes I made working with what you had to start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#include <iostream> // <--- There is no <iostream.h>.
#include <iomanip>
#include <string>
#include <cctype>
#include <chrono> // <--- For code used in case 3 of the "menu" function.
#include <thread> // <--- For code used in case 3 of the "menu" function.
#include <conio.h> // <--- Not necessary. Something I used in testing to speed things up. Used in "F_order".
//#include <stdlib.h>
void menu();
void F_order(const double price[]); // <--- Added array to function.
void pay();
void display();
constexpr std::size_t MAXSIZE{ 9 }; // <--- Because there 9 items on the menus.
struct queue
{
std::string kod;
std::size_t s_qty{};
double s_price{};
struct queue *next;
};
struct queue *front = NULL;
struct queue *rear = NULL;
//global variable
// <--- Should not use global variables like these.
//int table_no, kod; // <--- "table_no" used only once and "kod" never used as an int.
void main()
{
constexpr double price[9]{ 1.0, 2.0 , 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 }; // <--- Change prices as needed.
int table_no; // <--- Better placed here. Or maybe in the struct?
int choose;
bool cont{ true }; // <--- Added. Used for do/while loop.
// <--- No use as of now. Just bypassed for testing.
//std::cout << "Enter your table number : ";
//std::cin >> table_no;
do
{
std::cout << "\nChoose a number\n";
std::cout << "1 = TO display menu\n"; // <--- Changed '=' to ' = '. Looks better.
std::cout << "2 = TO place customer order\n";
std::cout << "3 = TO display the customer order\n";
std::cout << "4 = TO diplay payment amount for customer order\n";
std::cout << "5 = Exit\n";
std::cout << "Enter choice: ";
std::cin >> choose;
std::cout << std::endl;
switch (choose)
{
case 1:
menu();
break;
case 2:
F_order(price);
break;
case 3:
display();
break;
case 4:
pay();
break;
case 5:
cont=false;
break;
}
} while (cont); // <--- Changed.
}
|
And in the "F_order" function I did this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
void F_order(const double price[])
{
struct queue *temp = NULL;
char choice;
std::size_t count{};
do
{
int code{};
temp = new queue;
// would be helpful to display available items.
if (count < MAXSIZE)
{
std::cout << "Enter food code\n";
std::cin >> temp->kod;
std::cout << "Enter Quantity: ";
std::cin >> temp->s_qty;
code = std::stoi(temp->kod); // <--- Converts string to an int.
switch (code)
{
case 1:
temp->s_price = price[0];
break;
case 2:
temp->s_price = price[1];
break;
case 3:
temp->s_price = price[2];
break;
case 10:
temp->s_price = price[3];
break;
case 20:
temp->s_price = price[4];
break;
case 30:
temp->s_price = price[5];
break;
case 100:
temp->s_price = price[6];
break;
case 200:
temp->s_price = price[7];
break;
case 300:
temp->s_price = price[8];
break;
default:
break;
}
}
count++;
temp->next = NULL;
if (front == NULL)
{
front = rear = temp;
}
else
{
rear->next = temp;
rear = temp;
}
std::cout << "would you like to add more y/n?" << std::endl;
//std::cin >> choice;
choice = _getch(); // <--- Used this way for testing.
} while (std::tolower(choice) != 'n');
std::cout << std::endl;
}
|
Notice I added quantity and price to the struct. Since this struct becomes a node in a linked list each node having all the information you need makes it easier to display and use in the "pay" function.
The program can do multiple orders, but the linked list would become a problem on the second or third order because it would only add to the linked list, so the second order would be correct, but say the display would start at order one and also display order two and so on.
You would either have to delete the current linked list or change the address of "front" for each new order.
You did a nice job with the linked list code. That impressed me.
Thinking about this storing each struct in a vector of structs would be easier. After you are done with an order "
vectorName.clear();
" will erase everything allowing you to start with a new order.
Almost for got I did the "display" function this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void display()
{
if (front == NULL)
std::cout << "NO ORDER\n";
else
{
struct queue * temp = front;
std::cout << std::fixed << std::showpoint << std::setprecision(2);
std::cout << " Item Qty Price" << std::endl;
while (temp->next != NULL)
{
std::cout << std::setw(4) << temp->kod << " " << std::setw(3)
<< temp->s_qty << std::setw(5) << " " << temp->s_price << std::endl;
temp = temp->next;
}
std::cout << std::setw(4) << temp->kod << " " << std::setw(3)
<< temp->s_qty << std::setw(5) << " " << temp->s_price << std::endl;
//std::cout << temp->kod << std::endl;
}
}
|
The while condition is causing the extra cout after the while loop. I have not spent much time on this yet, but I know it is fixable so that each node can be printed inside the while loop.
Hope that helps,
Andy