#include<iostream> #include<string> #include<iomanip> #include <fstream> using n
ID: 3552484 • Letter: #
Question
#include<iostream>
#include<string>
#include<iomanip>
#include <fstream>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
bool selected;
};
int getdata(menuItemType[]);
void getOrder(menuItemType[], int);
void showMenu(menuItemType[], int);
void printCheck(menuItemType[], int);
int main()
{
int choices = 0;
menuItemType menu[20];
cout << "Welcome to my Restaurant My menu Item price ";
choices = getdata(menu);
getOrder(menu, choices);
printCheck(menu, choices);
system("pause");
return 0;
}
void printCheck(menuItemType order[], int items)
{
int i;
double total = 0, tax;
cout << " Your Name Restaurant ";
for (i = 0; i<items; i++)
if (order[i].selected)
{
total += order[i].menuPrice;
cout << setw(16) << left << order[i].menuItem << "$" <<
setprecision(2) << fixed << order[i].menuPrice << endl;
}
cout << " Total " << "$" << setprecision(2) << fixed << total << endl;
tax = total*.08;
cout << "Tax " << "$" << setprecision(2) << fixed << tax << endl;
cout << "Amount Due " << "$" << setprecision(2) << fixed << total + tax << endl;
}
int getdata(menuItemType menu[])
{
ifstream infile;
int i = 0;
infile.open("menu.txt"); //open file
if (infile.fail()) //is it ok?
{
cout << "file did not open please check it ";
system("pause");
system("exit");
}
do
{
getline(infile, menu[i].menuItem);
infile >> menu[i].menuPrice;
infile.get();
menu[i].selected = false;
i++;
} while (infile);
infile.close();
return i - 1;
}
void getOrder(menuItemType menu[], int choices)
{
int n;
showMenu(menu, choices);
cout << "Enter your order item number: ";
cin >> n;
while (n != choices + 1)
{
while (n<1 || n>9)
{
cout << "invalid item number ";
cout << "Enter order item number: ";
cin >> n;
}
menu[n - 1].selected = true;
cout << "Enter order item number: ";
cin >> n;
}
}
void showMenu(menuItemType a[], int n)
{
int i;
for (i = 0; i<n; i++)
{
cout << i + 1 << ". " << setw(16) << left << a[i].menuItem << "$" << setprecision(2) << fixed << a[i].menuPrice << endl;
}
cout << i + 1 << ". Finished Ordering ";
}
Explanation / Answer
Your code is giving compilation error because of the calls System("pause"); and System("Exit");
Just remove them and the code will work fine.
// Properly working code
#include<iostream>
#include<string>
#include<iomanip>
#include <fstream>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
bool selected;
};
int getdata(menuItemType[]);
void getOrder(menuItemType[], int);
void showMenu(menuItemType[], int);
void printCheck(menuItemType[], int);
int main()
{
int choices = 0;
menuItemType menu[20];
cout << "Welcome to my Restaurant My menu Item price ";
choices = getdata(menu);
getOrder(menu, choices);
printCheck(menu, choices);
return 0;
}
void printCheck(menuItemType order[], int items)
{
int i;
double total = 0, tax;
cout << " Your Name Restaurant ";
for (i = 0; i<items; i++)
if (order[i].selected)
{
total += order[i].menuPrice;
cout << setw(16) << left << order[i].menuItem << "$" <<
setprecision(2) << fixed << order[i].menuPrice << endl;
}
cout << " Total " << "$" << setprecision(2) << fixed << total << endl;
tax = total*.08;
cout << "Tax " << "$" << setprecision(2) << fixed << tax << endl;
cout << "Amount Due " << "$" << setprecision(2) << fixed << total + tax << endl;
}
int getdata(menuItemType menu[])
{
ifstream infile;
int i = 0;
infile.open("menu.txt"); //open file
if (infile.fail()) //is it ok?
{
cout << "file did not open please check it ";
}
do
{
getline(infile, menu[i].menuItem);
infile >> menu[i].menuPrice;
infile.get();
menu[i].selected = false;
i++;
} while (infile);
infile.close();
return i - 1;
}
void getOrder(menuItemType menu[], int choices)
{
int n;
showMenu(menu, choices);
cout << "Enter your order item number: ";
cin >> n;
while (n != choices + 1)
{
while (n<1 || n>9)
{
cout << "invalid item number ";
cout << "Enter order item number: ";
cin >> n;
}
menu[n - 1].selected = true;
cout << "Enter order item number: ";
cin >> n;
}
}
void showMenu(menuItemType a[], int n)
{
int i;
for (i = 0; i<n; i++)
{
cout << i + 1 << ". " << setw(16) << left << a[i].menuItem << "$" << setprecision(2) << fixed << a[i].menuPrice << endl;
}
cout << i + 1 << ". Finished Ordering ";
}
Another solution is to include the library file stdlib.h and the code will work fine.
// Running code
#include<iostream>
#include<string>
#include<iomanip>
#include <fstream>
#include<cstdlib>
using namespace std;
struct menuItemType
{
string menuItem;
double menuPrice;
bool selected;
};
int getdata(menuItemType[]);
void getOrder(menuItemType[], int);
void showMenu(menuItemType[], int);
void printCheck(menuItemType[], int);
int main()
{
int choices = 0;
menuItemType menu[20];
cout << "Welcome to my Restaurant My menu Item price ";
choices = getdata(menu);
getOrder(menu, choices);
printCheck(menu, choices);
system("pause");
return 0;
}
void printCheck(menuItemType order[], int items)
{
int i;
double total = 0, tax;
cout << " Your Name Restaurant ";
for (i = 0; i<items; i++)
if (order[i].selected)
{
total += order[i].menuPrice;
cout << setw(16) << left << order[i].menuItem << "$" <<
setprecision(2) << fixed << order[i].menuPrice << endl;
}
cout << " Total " << "$" << setprecision(2) << fixed << total << endl;
tax = total*.08;
cout << "Tax " << "$" << setprecision(2) << fixed << tax << endl;
cout << "Amount Due " << "$" << setprecision(2) << fixed << total + tax << endl;
}
int getdata(menuItemType menu[])
{
ifstream infile;
int i = 0;
infile.open("menu.txt"); //open file
if (infile.fail()) //is it ok?
{
cout << "file did not open please check it ";
system("pause");
system("exit");
}
do
{
getline(infile, menu[i].menuItem);
infile >> menu[i].menuPrice;
infile.get();
menu[i].selected = false;
i++;
} while (infile);
infile.close();
return i - 1;
}
void getOrder(menuItemType menu[], int choices)
{
int n;
showMenu(menu, choices);
cout << "Enter your order item number: ";
cin >> n;
while (n != choices + 1)
{
while (n<1 || n>9)
{
cout << "invalid item number ";
cout << "Enter order item number: ";
cin >> n;
}
menu[n - 1].selected = true;
cout << "Enter order item number: ";
cin >> n;
}
}
void showMenu(menuItemType a[], int n)
{
int i;
for (i = 0; i<n; i++)
{
cout << i + 1 << ". " << setw(16) << left << a[i].menuItem << "$" << setprecision(2) << fixed << a[i].menuPrice << endl;
}
cout << i + 1 << ". Finished Ordering ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.