Design, edit, compile, and execute a C++ application that gives a menu with thre
ID: 3670528 • Letter: D
Question
Design, edit, compile, and execute a C++ application that gives a menu with three options:
--- 1. Create a sequential data file has the information about the items of store. The information will be item description (should be its name), quantity, and price.
--- 2. Read an existing data file with the information of the items of the store (item description, quantity, and price). In this option, the user can enter a quantity by keyboard and the program will shows the list of items quantity is less than a given one.
--- 3. Exit
The application must contemplate handling exceptions with the data files used. For example, create a data file that exists, open a data file that does not exist, and others.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h> \header file for exit function
#include<fstream> \header file for reading and writing to file
using namespace std;
char na[10][25];
int q[25]; \global variables for storing values in files
int p[25];
void main()
{
while(1) \for displaying menu
{
cout<<"Choose one of the options from below:"
cout<<"--- 1. Create a sequential data file has the information about the items of store. The information will be item description (should be its name), quantity, and price. ";
cout<<"--- 2. Read an existing data file with the information of the items of the store (item description, quantity, and price). In this option, the user can enter a quantity by keyboard and the program will shows the list of items quantity is less than a given one.";
cout<<"--- 3. Exit";
cin>>n;
switch(n) \ for choosing one of the options
{
case 1:
create();
case 2:
read();
case 3:
exit();
}
}
}
void create()
{
bool b = true;
int num;
char name[20];
while(b)
{
cout<<"Enter the name of the file to be created: ";
cin>>name;
if (std::ifstream(name)) \checking whether there is some file of the same name already or not
std::cout << "File already exists Enter some other name: ";
else
b=false;
}
ofstream wr;
wr.open(name,ios::out) \opening the file in write mode
cout<<"Enter the number of items for which you want to insert data ";
cin>>num;
cout<<"Enter the name, quantity and price of the items respectively ";
for(int i=0; i<n; i++)
{
cin>>na[][i]>>q[i]>>p[i]; \taking input from the user
wr<<na[][i]<<q[i]<<p[i]; \writing to the file
}
cout<<"Data to the file"<<name<<" has been successfully written. ";
wr.close();
}
void read()
{
int qu;
bool b = true;
char name[20];
int myquan[25]; \ for reading from the file
while(b)
{
cout<<"Enter the name of the file to be opened ";
ifstream re(name);
if(re.fail())
cout<<"File does not exist Enter some other name ";
else
b=false;
}
re.open(name);
cout<<"Enter the quantity which is to be compared ";
cin>>qu;
cout<<"Here is the list of items' quantity with quantity less than the above mentioned value";
for(int i=0;;i++)
{
re>>myquan[i]; \copying the quantity from file to array
if(qu>q[i])
cout<<q[i];
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.