Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming C++ -- Question : Creating Struct and arrays and Order list. In this

ID: 3578919 • Letter: P

Question

Programming C++

--

Question : Creating Struct and arrays and Order list.

In this submission, required to write a material order recording program which applies struct and file IOfeatures. 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 this assignment question document, 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.
Program started.
Create a global struct for the material with appropriate data members.
Create an array of the material struct to store all the data from the text file.
Load all the data from the “pricelist.txt” into the array created in step 2.
List and display the materials loaded into the array in step 2.1.
Prompt the user to choose the material by enter the index number.
Record the option entered by the user.
Prompt the user to enter the weight in kg for the order.
Record the weight.
Based on the option and weight recorded in step 4&5, calculate and prompt the user if the order is confirmed.
If it is confirmed, then append the order to the file “order.txt”.
Else inform the user the data has not been recorded.
Prompt if the user would like to continue order.
If yes, restart step 4.
Else, end the program.
Program end.
--

The contents of Price list are as follows:

Cobalt A 6.5
Cobalt B 4.5
Nickel A 2.68
Nickel B 1.78
Tin A 5.12
Tin B 2.98
Zinc A 0.88
Zinc B 0.49

Explanation / Answer

#include <iostream>

#include <cstring>struct info

{

string metal;

char grade;

float price;

};

int main()

{

struct info a[8];

char n;

do{

ifstream infile;

infile.open("pricelist.txt");

cout<<"the list of metals available are: ";

for(int i=0;i<8;i++)

{

cot<<i+1<<" ";

infile>>a[i].metal;

cout<<a[i].metal<<"   ";

infile>>a[i].grade;

cout<<a[i].grade<<"    ";

infile>>a[i].price;

cout<<a[i].price<<"   ";

cout<<" ';

}

infile.close();

cout<<"choose the metal you want";

int ch;

cin>>ch;

cout>>"enter the quantity you want to purchase: ";

int weight;

cin>>weight;

float amount=a[ch-1].price*weight;

try

{ofstream outfile;

outfile.open("order.txt");

outfile<<a[ch-1].metal;

outfile<<a[ch-1].grade;

outfile<<a[ch-1].price;

outfile<<amount;

cout<<"order has been confirmed and amount to be paid is: "<<amount;

}

catch(Exception e)

{cout<<"cannot process the order";

}

cout<<"would you like to continue[y/n)";

cin>>n;

}while(n!='n');

return 1;

}