Question

In: Computer Science

Create a struct MenuItem containing fields for name (a string) and price (a float) of a...

  1. Create a struct MenuItem containing fields for name (a string) and price (a float) of a menu item for a diner. Create a ReadItem() function that takes an istream and a MenuItem (both by reference) and prompts the user for the fields of the MenuItem, loading the values into the struct's fields. Create a PrintItem() function that takes an output stream (by reference) and a MenuItem (by value) and prints the MenuItem fields to the stream in a reasonable one-line format. Create a main() that declares a MenuItem struct, calls ReadItem(), then calls PrintItem(). Test with a few different items.
  2. Make another program modified to include an array of 10 MenuItem structs. Modify ReadItem() to not prompt for, but instead just read the data for a struct field-by-field, and call it once for each MenuItem. Then call PrintItem() for each struct to create a menu. Use files, opened and closed in main(), for all input and output. Create a test file with the 10 items and prices, and test with it. Send me the test file and the output file.
  3. Make another program modified so that after it reads the MenuItems from the file (as per part 2), it prints a menu (on the screen) for the user, collects an order, and prints a diner check. You should not have to modify PrintItem(). Write a function that takes the array of MenuItems and an array of ints, prints the menu (number the items 1 through 10), and then loops, prompting the user to select items until they have either selected all 10 items on the menu or entered -1 to indicate they are through ordering. Don't allow the user to select an item that isn't on the menu or an item they have already selected. You may assume the user will enter at least one valid selection. Keep the user's selections in the array of integers. Return the number of entries the user made. Then print a diner check with the items the user selected and the total price of the meal. Use another function to print the check, passing the two arrays, the number of items selected, and the output stream to the function. Use the PrintItem() function to print the items, both on the check and on the menu. You may assume no tax. Print all output to the screen.

Hints:

If you use an istream & for an input stream parameter, you may pass either cin or an input file handle to a function. Similarly, if you use an ostream & for an output stream parameter, you may pass in either cout or an output file handle to the function. The only restriction is that you may only use operations that would work on cin or cout in the function. For example, you may not open() or close()inside the function.

You may use either C-strings or C++ string objects for the menu item names. Use multi-word item names like "bacon cheeseburger" not just "cheeseburger". To read a C-string containing white space characters, use

in.getline( data, MAX, '\n' );

where in is either cin or an input file handle (or a parameter of istream & type), data is an array of char at least MAX+1 in length, MAX is the maximum number of characters to read from the input stream, and '\n' is the character at which to stop reading. To read a C++ string object containing white space characters, use

getline( in, data );

where in is either cin or an input file handle (or a parameter of istream & type) and data is a C++ string object. Here, you don't have to worry about the maximum length of the string being read.

On Windows, after extracting a number, you will need to call in.ignore(); to get rid of the newline before you try another getline(). Do this inside the ReadItem() function before it returns. To number the items in the third exercise, print the number before calling PrintItem(). PrintItem() should not end the line itself. You might not need to do this on Mac or Linux systems.

This is for introduction to C++ please don't make code complex. add comments for methods.

Solutions

Expert Solution

you can find the output of the code in the sceenshot provided below.Thank you

#include<iostream>

#include<string>

#include<iomanip>

#include <fstream>

using namespace std;

//declaring structure for item name and price

struct Menu_Item

{

string name;

float price;

};

// function for input

void Read_Item(istream&, Menu_Item&);

//function for output

void Print_Item(ostream&,Menu_Item);

//function for receiving items

int get_Order(Menu_Item[],int[]);

//function to recheck the ordered items

void print_Check(Menu_Item[],int[],int,ostream&);

int main()

{

Menu_Item order1[10];

ifstream in;

ofstream out;

int i1=0,chosenvalue,options[10];

//opening the menu file you have to write it.

in.open("C:/Users/j/Desktop/menu.txt");   

if(in.fail())

{ cout<<"could not open the file\n";

}

cout<<"Welcome Restaurant\n My menu items\n Item\t price\n";

for(i1=0;i1<10;i1++)

//reads items in menu file

Read_Item(in,order1[i1]);

for(i1=0;i1<10;i1++)

{cout<<i1+1<<". ";

Print_Item(cout,order1[i1]);

cout<<endl;

}

chosenvalue=get_Order(order1,options);

print_Check(order1,options,chosenvalue,cout);

in.close();

return 0;

}

void print_Check(Menu_Item order1[],int options[],int items1,ostream& out)

{int i1;

float sum=0;

out<<"\n\n Restaurant\n";

for(i1=0;i1<items1;i1++)

{sum+=order1[options[i1]].price;

out<<setw(16)<<left<<order1[options[i1]].name<<"Rs"<<

setprecision(2)<<fixed<<order1[options[i1]].price<<endl;

//printing up to two digits so that it prints .00 value eg Rs10.00

}

out<<"\n\ntotal due \t"<<"Rs"<<sum<<endl;

}

int get_Order(Menu_Item menu[],int options[])

{int n1,m1=0;

bool chosenvalue[10]={false},data;

do

{cout<<"please Enter order item number(-1 to exit): ";

cin>>n1;

if(n1==-1)

return m1;

while(n1<1||n1>10)

{cout<<"invalid \n";

cout<<"please Enter order item number: ";

cin>>n1;

}

while(chosenvalue[n1-1])

{ cout<<"you've already choosed\n";

cout<<"please Enter order item number: ";

cin>>n1;

while(n1<1||n1>10)

{cout<<"invalid\n";

cout<<"Enter order item number: ";

cin>>n1;

}

}

chosenvalue[n1-1]=true;

options[m1]=n1-1;

m1++;

}while(m1<10);

}

void Read_Item(istream& in, Menu_Item& order1)

{getline(in,order1.name );

in>>order1.price;

in.get();

}

void Print_Item(ostream& out,Menu_Item a1)

{out<<setw(16)<<left<<a1.name<<"Rs"<<setprecision(2)<<fixed<<a1.price;

}

---output---

Thank you..!!


Related Solutions

Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;...
QUESTION 60 Given the following Product structure: struct Product {     string name;     double price;     int quantity;     bool equals(const Product&); }; how would you define the equals function so two products are equal if their names and prices are equal? a. bool equals(const Product& to_compare) {     return (name == to_compare.name && price == to_compare.price); } b. bool Product::equals(const Product& to_compare) {     return (name == to_compare.name || price == to_compare.price); } c. bool equals(const Product& to_compare)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price)...
package compstore; public class Desktop{ private String brand; private float price; public Desktop(String brand, float price) { this.brand = brand; this.price = price; } public String getBrand() { return brand; } public float getPrice() { return price; } } package compstore; public class DeskTopDeals{ // assume proper variables and other methods are here public void dealOfTheDay(Desktop[] items, int numItems){ /**************************** * your code would go here * ****************************/ } } Given the above Desktop class, write code that should go...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price)...
package construction; public class Bid{ private String contractor; private float price; public Bid(String contractor, float price) { this.contractor = contractor; this.price = price; } public String getContractor() { return contractor; } public float getPrice() { return price; } } package construction; public class ContractorBids{ // assume proper variables and other methods are here public void winningBid(Bid[] bids, int numBids){ /**************************** * your code would go here * ****************************/ } } You are doing renovations on your building, and multiple contractors...
1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory.
  1. The Item class includes two fields: a String for the name and a double for the price of an item that will be added to the inventory. 2. The Item class will require one constructor that takes a name and price to initialize the fields. Since the name and price will be provided through TextFields, the parameters can be two Strings (be sure to convert the price to a double before storing this into the field). 3. Write...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and a double for the price. 2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object. 3. The Soda class...
Create a table with name “S_YOURStudentID” with the following fields with required constraints. Screenshots are to...
Create a table with name “S_YOURStudentID” with the following fields with required constraints. Screenshots are to be attached.                                                                                               For example, if your botho student id is 1212121, your table name should be “S_1212121”.                                   Student id Student Name Age Gender City Course name Module Name Internal mark 1 Internal mark 2 Internal mark 3 Total internal mark Insert minimum 10 records/rows to the table. Input the data based on Question 5. Screenshots are to be attached.                                                                                                                              Create two users...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT