Hello guys. Got a task as below: \" required to write a material order recording
ID: 3576671 • Letter: H
Question
Hello guys.
Got a task as below:
" required to write a material order recording program which applies struct and file IO features. When the program started, it should list the information about all the materials available (from “pricelist.txt”) and allow user to choose the material that he/she wants to order. Next, the program will prompt the user to enter the weight (kg) that he/she wants to order. Finally, the program will ask the user if the order is confirmed. If it is confirmed, the program will record/append the order to the text file name “order.txt "
Together with question , a txt file name “pricelist.txt” is provided which comprises the data of a list of materials. The data includes, material name, grade, and price per kg in usd.
The suggested algorithm for this program is as below, note that you can chose to or to not follow the algorithm provided but must fulfil the items listed in marking scheme.
1. Program started.
2. Create a global struct for the material with appropriate data members.
3. Create an array of the material struct to store all the data from the text file.
3.1 Load all the data from the “pricelist.txt” into the array created in step 2.
4. List and display the materials loaded into the array in step 2.1.
5. Prompt the user to choose the material by enter the index number.
5.1 Record the option entered by the user.
6. Prompt the user to enter the weight in kg for the order.
6.1 Record the weight.
7. Based on the option and weight recorded in step 4&5, calculate and prompt the user if the order is confirmed.
7.1 If it is confirmed, then append the order to the file “order.txt”.
7.2 Else inform the user the data has not been recorded.
8. Prompt if the user would like to continue order.
8.1 If yes, restart step 4.
8.2 Else, end the program.
9. Program end.
---
So the program is above,
i tried solving it as below
But i am not sure how will it calculate the rate with the weight.
Anyone who could help please?
--
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string filename= "pricelist.txt";
string descrip;
string grade;
double price;
ifstream inFile;
inFile.open(filename.c_str());
if (inFile.fail())
{
cout <<" The file was not sucessfully opened. Please check that the file currently exists.";
exit(1);
}
inFile >> descrip >> grade >> price;
while (inFile.good())
{
cout << descrip << ' ' << grade << ' ' << price << endl;
inFile >> descrip >> grade >> price;
}
inFile.close();
}
struct Global
{
string Material_name;
char grade;
double price_per_kg;
};
Global first;
first.Material_name="Cobalt";
first.grade= "A";
first.price_per_kg = 6.5;
Global second={"Cobalt",B,4.5};
Global third={"Nickel",A,2.68};
Global fourth={"Nickel",B,1.78};
Global fifth={"Tin",A,5.12};
Global six={"Tin",B,2.98};
Global seven={"Zinc",A,0.88};
Global eight={"Zinc",B,0.49};
display(first);
display(second);
display(third);
display(fourth);
display(fifth;
display(six);
display(seven);
display(eight);
int num
double weight
cout<<"Enter an index number to choose a material "<<endl;
cin>>num
cout<<"Enter the desired weight in kg for the order"<<endl;
cin>>weight
Price=global[num]*weight
cout<<"You have selected <<global[i] ;
/n to continue Press Y or N to exit
return 0;
}
{
ofstream out2File;
out2File.open("order.txt",ios::app);
cout<<"Writing to file"<<endl;
out2File<<"Material Name ";
out2File<<"Weight"<<endl;
out2File<<"Price ";
out2File<<"6.7";
out2File.close();
cout<<"finished writing, file close"<<endl;
system("PAUSE");
return 0;
}
Explanation / Answer
#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
//structure defined
typedef struct Item
{
string materialName;
char grade;
double price;
}IT;
IT item[20] = {" ", ' ', 0};
//Read specified record from the file and display it
int storeRecord()
{
ifstream f;
//Open the file in read mode
f.open("pricelist.txt", ios::binary | ios::in);
//Set the counter to 1 and flag for availability of the record to 0
int co = 0, fla = 0;
//Move to the starting position
f.seekg(0L, ios::beg);
//Read record from file till end of the file
while(!f.eof())
{
//Display the record
f>>item[co].materialName>>item[co].grade>>item[co].price;
co++;
}
//Close the file
f.close();
//Returns number of records
return co;
}
//Displays the records
void displayRecord(int no)
{
for(int c = 0; c < no - 1; c++)
cout<<" Index: "<<c<<" Item Name: "<<item[c].materialName<<" Item Grade: "<<item[c].grade<<" Price per kg: "<<item[c].price;
}
//Write the record in order.txt file if order is conform
void writeRecord(int ind, double amt)
{
fstream f;
//Open file in append mode
f.open("order.txt", ios::binary | ios::app);
//Check for the file can be opened or not. if not show error message and exit
if(!f)
{
cout<<" Unable to open file";
exit(0);
}
//if it can be opened
else
{
//Write it to the file
f<<item[ind].materialName<<" "<<item[ind].grade<<" "<<item[ind].price<<" "<<amt<<" ";
cout<<" Ordered Recorded";
}
//Close file
f.close();
}
//Accept a record number for order and checks its validity
void recordStatus()
{
int index;
double kg, amt;
char ch;
re:
//Accepts a record number
cout<<" Choose the material by enter the index number: ";
cin>>index;
//Checks for the validity of the record
if(index < 0 || index > storeRecord())
{
cout<<" Invalid Index number: "<<index;
goto re;
}
//Accept the how may kg.
cout<<" Enter the weight in kg for the order: ";
cin>>kg;
//Calculates the amount
amt = item[index].price * kg;
//Displays the amount to be paid
cout<<" Amount to be paid: "<<amt;
//Accepts the conformation for the order from the user
cout<<" Would you like to order the Item: (y / n)";
cin>>ch;
//If yes write the record
if(ch == 'y' || ch == 'Y')
writeRecord(index, amt);
else
cout<<" Data has not been recorded";
}
int main()
{
int inx;
double kg;
char choice;
//Stores and returns number of records in the file
int co = storeRecord();
//Displays the record
displayRecord(co);
//Loops till user choice
do
{
//Asks the user for order
recordStatus();
//Asks the user to continue or stop operation
cout<<" Would like to continue order: (y/n)";
cin>>choice;
if(choice == 'N' || choice == 'n')
break;
}while(1);
}
Output:
Index: 0 Item Name: Cobalt
Item Grade: A Price per kg: 6.5
Index: 1 Item Name: Cobalt
Item Grade: B Price per kg: 4.5
Index: 2 Item Name: Nickel
Item Grade: A Price per kg: 2.68
Index: 3 Item Name: Nickel
Item Grade: B Price per kg: 1.78
Index: 4 Item Name: Tin
Item Grade: A Price per kg: 5.12
Index: 5 Item Name: Tin
Item Grade: B Price per kg: 2.98
Index: 6 Item Name: Zinc
Item Grade: A Price per kg: 0.88
Index: 7 Item Name: Zinc
Item Grade: B Price per kg: 0.49
Choose the material by enter the index number: 2
Enter the weight in kg for the order: 10
Amount to be paid: 26.8
Would you like to order the Item: (y / n)y
Ordered Recorded
Would like to continue order: (y/n)y
Choose the material by enter the index number: 7
Enter the weight in kg for the order: 3
Amount to be paid: 1.47
Would you like to order the Item: (y / n)n
Data has not been recorded
Would like to continue order: (y/n)y
Choose the material by enter the index number: 10
Invalid Index number: 10
Choose the material by enter the index number: 6
Enter the weight in kg for the order:
5
Amount to be paid: 4.4
Would you like to order the Item: (y / n)y
Ordered Recorded
Would like to continue order: (y/n)n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.