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

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...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
protected String name;                                      &nbsp
protected String name;                                                                                                           protected ArrayList<Stock> stocks;     protected double balance;                                                                              + Account ( String name, double balance) //stocks = new ArrayList<Stock>() ; + get and set property for name; get method for balance + void buyStock(String symbol, int shares, double unitPrice ) //update corresponding balance and stocks ( stocks.add(new Stock(…..)); ) + deposit(double amount) : double //returns new balance                                                                 + withdraw(double amount) : double //returns new balance + toString() : String                                                                  @Override     public String toString(){         StringBuffer str =...
Create a Word file containing the following: The full name of the “test subject” The test...
Create a Word file containing the following: The full name of the “test subject” The test subject is a person, not yourself, who can say the names of the products. The best test subject is one who is adamant about being an expert who can distinguish brand A from B. You cannot be the test subject because you generate the sequence, and the test subject cannot see it for an unbiased test. Pets and babies are not allowed as they...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly...
Create a class named Lease with fields that hold an apartment tenant’s name, apartment number, monthly rent amount, and term of the lease in months. Include a constructor that initializes the name to “XXX”, the apartment number to 0, the rent to 1000, and the term to 12. Also include methods to get and set each of the fields. Include a nonstatic method named addPetFee() that adds $10 to the monthly rent value and calls a static method named explainPetPolicy()...
Java problem Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English,...
Java problem Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English, French, German), int creditPoints.  Class Contract has an array of courses with methods addCourse(Course), deleteCourse(type, stream, name) sort(), display().  Courses are sorted by stream, type, name.  If 2 courses are equal raise a custom exception in method sort().  Make Contract implement Storable.
Write a recursive method to determine if a String is a palindrome. Create a String array...
Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. Write a recursive method to determine if a String is a palindrome. Create a String array with several test cases and test your method. In Java
Part 1 readFile(String filename) In this method you are passed a String with the name of...
Part 1 readFile(String filename) In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below. File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Create a C++ program that follows the specifications below: *Define a struct with 4 or more...
Create a C++ program that follows the specifications below: *Define a struct with 4 or more members. *Application must have at least one user-defined function *Declare an array of your struct using a size of 10 or more *Load the date for each element in your array from a text file *Display the data in your array in the terminal *provide brief comments for each line of code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT