In: Computer Science
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.
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..!!