I need help with my intro to C++ assignment

For my assignment I have to write a program that creates a restaurant billing system. I think that I got it correct but it says that it is incorrect, so I was wondering if someone would be able to look over my code. It says that I need tax of $0.20, and the amount due to be $4.10, which is what I have in my output.



Here is my assignment

Write a program to help a local restaurant automate its breakfast billing system. The program should do the following:

Show the customer the different breakfast items offered by the restaurant.
Allow the customer to select more than one item from the menu.
Calculate and print the bill.
Assume that the restaurant offers the following breakfast items (the price of each item is shown to the right of the item):

food Price
Plain Egg $1.45
Bacon and Egg $2.45
Muffin $0.99
French Toast $1.99
Fruit Basket $2.49
Cereal $0.69
Coffee $0.50
Tea $0.75
Use an array menuList of type menuItemType, as defined in Programming Exercise 3. Your program must contain at least the following functions:

Function getData: This function loads the data into the array menuList.
Function showMenu: This function shows the different items offered by the restaurant and tells the user how to select the items.
Function printCheck: This function calculates and prints the check. (Note that the billing amount should include a 5% tax.)
A sample output is:

Welcome to Johnny's Resturant
----Today's Menu----
1: Plain Egg $1.45
2: Bacon and Egg $2.45
3: Muffin $0.99
4: French Toast $1.99
5: Fruit Basket $2.49
6: Cereal $0.69
7: Coffee $0.50
8: Tea $0.75

You can make up to 8 single order selections
Do you want to make selection Y/y (Yes), N/n (No): Y

Enter item number: 1

Select another item Y/y (Yes), N/n (No): Y

Enter item number: 2

Select another item Y/y (Yes), N/n (No): N

Welcome to Johnny's Resturant
Plain Egg $1.45
Bacon and Egg $2.45
Tax $0.20
Amount Due $4.10



Assignment images link
https://imgur.com/a/6nIlNyt


Here is my code

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Header files section
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

// MenuItem structure
struct MenuItem
{
   string description;
   double price;
   int quantity;
};

// constants declaration
const int MAX_ITEMS = 20;
const double TAX_RATE = 0.05;

// function prototype
void getData(MenuItem menuList[], int &size);
void showMenu(MenuItem menuList[], int size);
void printCheck(MenuItem menuList[], int size);

// start main function
int main()
{
   // create an array of items
   MenuItem menuList[MAX_ITEMS];
   int size = 0;

   // call the getData function
   getData(menuList, size);

   // call the showMenu function
   showMenu(menuList, size);

   // call the printCheck function
   printCheck(menuList, size);

  
   return 0;
} // end of main function


// getData function implementation
// This function loads the data into the array menuList.
void getData(MenuItem menuList[], int &size)
{
   menuList[0].description = "Plain Egg";
   menuList[0].price = 1.45;
   menuList[0].quantity = 0;

   menuList[1].description = "Bacon and Egg";
   menuList[1].price = 2.45;
   menuList[1].quantity = 0;

   menuList[2].description = "Muffin";
   menuList[2].price = 0.99;
   menuList[2].quantity = 0;

   menuList[3].description = "French Toast";
   menuList[3].price = 1.99;
   menuList[3].quantity = 0;

   menuList[4].description = "Fruit Basket";
   menuList[4].price = 2.49;
   menuList[4].quantity = 0;

   menuList[5].description = "Cereal";
   menuList[5].price = 0.69;
   menuList[5].quantity = 0;

   menuList[6].description = "Coffee";
   menuList[6].price = 0.50;
   menuList[6].quantity = 0;

   menuList[7].description = "Tea";
   menuList[7].price = 0.75;
   menuList[7].quantity = 0;

   size = 8;
} // end of getData function


// showMenu function implementation
// This function shows the different items offered by the restaurant and
// tells the user how to select the items.
void showMenu(MenuItem menuList[], int size)
{
   cout << "Welcome to Johnny's Restaurant" << endl;
   cout << "----------MENU ITEMS----------" << endl;
   for (int i = 0; i < size; i++)
   {
       cout << (i + 1) << ". " << left << setw(21) << menuList[i].description << "$" << setw(6) << setprecision(2) << fixed << menuList[i].price << endl;
   }
   cout << "------------------------------" << endl;

   int num;
   int qty;
  
   cout << "\nEnter the item number (1-8) (-1 for exit): ";
   cin >> num;

   while (num != -1)
   {
       if (num >= 1 && num <= 8)
       {
           menuList[num - 1].quantity = 1;
       }
       else
       {
           cout << "Invalid item number!" << endl;
       }

       cout << "Enter the item number (1-8) (-1 for exit): ";
       cin >> num;
   }
} // end of showMenu function


// printCheck function implementation
// This function calculates and prints the check.
// (Note that the billing amount should include a 5 % tax.)
void printCheck(MenuItem menuList[], int size)
{
   double totalPrice = 0;
   double taxAmount = 0;
  
   cout << "\nYour Bill..." << endl;
   cout << endl << "Welcome to Johnny's Restaurant" << endl;
   for (int i = 0; i < size; i++)
   {
       if (menuList[i].quantity > 0)
       {
           cout << left << setw(20) << menuList[i].description << "$" << setw(10) << setprecision(2) << fixed << menuList[i].price << endl;
           totalPrice += menuList[i].price;
       }
   }

   taxAmount = totalPrice * TAX_RATE;
   cout << setw(20) << "Tax:"<< "$" << setw(10) << setprecision(2) << fixed << taxAmount << endl;
   cout << setw(20) << "Amount Due:" << "$" << setw(10) << setprecision(2) << fixed << (totalPrice + taxAmount) << endl << endl;
} // end of printCheck function
I'd also suggest you itemise the bill as follows:

- Items ordered line by line i.e Item, No ordered, price/item, line cost
- Sub total before tax
- Tax on total
- Grand total

ie avoid hiding the results so you can zero on in where the problem might be

Welcome to Johnny's Resturant
Plain Egg 1 $1.45 $1.45
Bacon and Egg 2 $2.45 $4.90

Sub-total $6.35
Tax 5% $0.32

Amount Due $6.67
Hello Alex4321,

You stated that this is your input and output:

Welcome to Johnny's Resturant
----Today's Menu----
1: Plain Egg $1.45
2: Bacon and Egg $2.45
3: Muffin $0.99
4: French Toast $1.99
5: Fruit Basket $2.49
6: Cereal $0.69
7: Coffee $0.50
8: Tea $0.75

You can make up to 8 single order selections
Do you want to make selection Y/y (Yes), N/n (No): Y

Enter item number: 1

Select another item Y/y (Yes), N/n (No): Y

Enter item number: 2

Select another item Y/y (Yes), N/n (No): N

Welcome to Johnny's Resturant
Plain Egg $1.45
Bacon and Egg $2.45
Tax $0.20
Amount Due $4.10


But when I run the program I get this:

Welcome to Johnny's Restaurant
----------MENU ITEMS----------
1. Plain Egg            $1.45
2. Bacon and Egg        $2.45
3. Muffin               $0.99
4. French Toast         $1.99
5. Fruit Basket         $2.49
6. Cereal               $0.69
7. Coffee               $0.50
8. Tea                  $0.75
------------------------------

Enter the item number (1-8) (-1 for exit): 1
Enter the item number (1-8) (-1 for exit): 2
Enter the item number (1-8) (-1 for exit): -1

Your Bill...

Welcome to Johnny's Restaurant
Plain Egg           $1.45
Bacon and Egg       $2.45
Tax:                $0.20


If your program is expected to do the first then it is wrong. Sometimes you have to give what is asked for even if you do not agree with it.

Also no where in the program or output does it say that you can only enter 1 of each item, but that is the way the program is set up. You might want to consider the bill part showing "description Price Qty Extension". Then either allowing for an item to be chosen more than once or ask for the quantity. If allowed.


I think that I got it correct but it says that it is incorrect



For the most part the code is doing its job correctly even with some minor changes. Beyond that you need to explain what is incorrect. The statement as is leaves it open to guess work about what is incorrect.

std::cout << std::fixed << std::setprecision(2);. This only needs done once and not in every "cout" that prints a double. I usually put this near the beginning of "main" or the beginning of the function(s) that need it. Usually it works in "main".

Andy
line 108:
menuList[num - 1].quantity = 1;

looks like it should be:
menuList[num - 1].quantity += 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
cout << "\nYour Bill..." << endl;
   cout << endl << "Welcome to Johnny's Restaurant" << endl;
   for (int i = 0; i < size; i++)
   {
       if (menuList[i].quantity > 0)
       {
           cout
           << left << setw(20) << menuList[i].description
           << setw(10) << setprecision(2) << fixed
           << menuList[i].quantity << "  $" << menuList[i].price
           << "  $" << menuList[i].price * menuList[i].quantity << endl;
           
           totalPrice += (menuList[i].price * menuList[i].quantity) ;
       }
   }
@OP as mentioned you also haven't made provision for entering the number of each item ordered. you need another cin etc and a method to add that to the quantity. (ie not assumed += 1 )


And to avoid long lines but using most of your original code ...
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>

using namespace std;

struct MenuItem
{
    string description;
    double price;
    int quantity;
};

const int MAX_ITEMS = 20;
const double TAX_RATE = 5;

void getData(MenuItem menuList[], int &size);
void showMenu(MenuItem menuList[], int size);
void printCheck(MenuItem menuList[], int size);

int main()
{
    MenuItem menuList[MAX_ITEMS];
    int size = 0;
    
    getData(menuList, size);
    showMenu(menuList, size);
    printCheck(menuList, size);
    
    return 0;
}

void getData(MenuItem menuList[], int &size)
{
    menuList[0] = {"Plain Egg", 1.45, 0 };
    menuList[1] = {"Bacon and Egg", 2.45, 0};
    menuList[2] = {"Muffin", 0.99, 0};
    menuList[3] = {"French Toast", 1.99, 0};
    menuList[4] = {"Fruit Basket", 2.49, 0};
    menuList[5] = {"Cereal", 0.69, 0};
    menuList[6] = {"Coffee", 0.50, 0};
    menuList[7] = {"Tea",0.75,0};
    
    size = 8;
}

void showMenu(MenuItem menuList[], int size)
{
    cout
    << "Welcome to Johnny's Restaurant\n"
    << "----------MENU ITEMS----------\n";
    
    for (int i = 0; i < size; i++)
    {
        cout
        << (i + 1) << ". "
        << left << setw(20)
        << menuList[i].description
        << "$"
        << setw(6) << setprecision(2) << fixed
        << menuList[i].price
        << '\n';
    }
    cout
    << "------------------------------\n\n";
    
    int num{0};
    int qty{0};
    
    while (
           cout
           << "Enter the item number (1-8) (-1 for exit): "
           && cin >> num
           && num != -1
           )
    {
        if (num >= 1 && num <= 8)
        {
            cout << "Enter no. required: ";
            cin >> qty;
            
            menuList[num - 1].quantity += qty;
        }
        else
        {
            cout << "Invalid item number!\n";
        }
    }
}

void printCheck(MenuItem menuList[], int size)
{
    double totalPrice = 0;
    double taxAmount = 0;
    
    cout
    << "Your Bill...\n\n"
    << "Welcome to Johnny's Restaurant\n";
    
    for (int i = 0; i < size; i++)
    {
        if (menuList[i].quantity > 0)
        {
            cout
            << setw(20) << right
            << menuList[i].description
            << right << setw(6) << fixed
            << menuList[i].quantity
            << "  $"
            << right << setw(6) << setprecision(2) << fixed
            << menuList[i].price
            << "  $"
            << right << setw(6) << setprecision(2) << fixed
            << menuList[i].price * menuList[i].quantity
            << '\n';
            
            totalPrice += (menuList[i].price * menuList[i].quantity) ;
        }
    }
    
    taxAmount = round(totalPrice * TAX_RATE) / 100;
    
    cout
    << '\n'
    << setw(38) << right
    << "Sub total: $"
    << right << setw(6) << setprecision(2) << fixed
    << totalPrice << '\n'
    
    << setw(38) << right
    << "Tax: $"
    << right << setw(6) << setprecision(2) << fixed
    << taxAmount << "\n\n"
    
    << setw(38) << right
    << "Amount Due: $"
    << right << setw(6) << setprecision(2) << fixed
    << totalPrice + taxAmount
    
    << "\n\n";
}
Last edited on
Topic archived. No new replies allowed.