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: 3579982 • 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 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 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 <fstream>
#include <iostream>
#include <vector>
using namespace std;

struct material{
   string name;
   string grade;
   double pricePerKg;
};

void printMaterialArray( material* materialArray, int totalMaterials );

int main(){

   material materialArray[1001]; //hoping pricelist.txt has maximum of 1000 materials
   int totalMaterials = 0;

   //read from pricelist.txt
   ifstream in("pricelist.txt");
   while( in >> materialArray[totalMaterials].name ){
       in >> materialArray[totalMaterials].grade;
       in >> materialArray[totalMaterials].pricePerKg;
       totalMaterials = totalMaterials + 1;
   }
   //read the file

   //Display all the materials now, along with their index starting from 0
   printMaterialArray( materialArray, totalMaterials );

   ofstream out("order.txt");
   while(1){
       //prompt user to select a material
       int index;
       double weight;
       char confirm;
       while(1){
           cout << "Enter index of material to order: ";
           cin >> index;
           if( index >= totalMaterials ){ cout << "Invalid Index. Re-enter the index "; }
           else{ break; }
       }
       cout << "Following material is selected: " << endl;
       cout << "Material Name: " << materialArray[ index ].name << endl;
       cout << "Material Grade: " << materialArray[ index ].grade << endl;
       cout << "Material Price per Kg: " << materialArray[ index ].pricePerKg << endl;
       cout << endl;  
       //got correct index, now ask for weight
       cout << "Enter weight in Kg for the order: ";
       cin >> weight;

       cout << "Total cost is: " << weight*materialArray[index].pricePerKg << endl;
       cout << "Should I confirm? (y/n) ";
       cin >> confirm;
       if( confirm == 'y' || confirm=='Y'){
           cout << "Order confirmed!!!" << endl;
           out << "Order :" << endl;
           out << "Material Name: " << materialArray[ index ].name << endl;
           out << "Material Grade: " << materialArray[ index ].grade << endl;
           out << "Material Price per Kg: " << materialArray[ index ].pricePerKg << endl;
           out << "Ordered weight: " << weight << endl;
           out << "Total cost: " << weight*materialArray[index].pricePerKg << endl;
           out << endl;
       }
       else{
           cout << "Order not confirmed" << endl;
       }
       cout << "Do you want to continue order? (y/n) ";
       cin >> confirm;
       if( confirm=='y' || confirm=='Y' ){ continue; }
       break;
   }

   out.close();
   return 0;
}

void printMaterialArray( material* materialArray, int totalMaterials ){
   for(int i = 0; i < totalMaterials; i++){
       cout << i << " " << materialArray[i].name << " ";
       cout << materialArray[i].grade << " " << materialArray[i].pricePerKg << endl;
   }
   return;
}