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

This is a Programming C++ question. -- Question : Creating Struct and arrays and

ID: 3579077 • Letter: T

Question

This is a Programming C++ question.

--

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 <string>
#include <algorithm>
#include <sstream>
#include <vector>
#include <fstream>

using namespace std;


struct items{
   string name;
   string grade;
   float rate;  
};

struct items *item_arr = new struct items[100];
void split(const std::string& str, std::vector<std::string>& v) {
std::stringstream ss(str);
ss >> std::noskipws;
std::string field;
char ws_delim;
while(1) {
if( ss >> field )
v.push_back(field);
else if (ss.eof())
break;
else
v.push_back(std::string());
ss.clear();
ss >> ws_delim;
}
}

int getPricefromFile(){
   string line;
   ifstream myfile ("/home/local/NEXTEDUCATION/mayankagrawal/priceList.txt");
   int count=0;
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
vector<string> v;
split(line,v);
item_arr[count].name=v[0];
item_arr[count].grade=v[1];
item_arr[count].rate=::atof(v[2].c_str());
count++;
}
myfile.close();
return count;
}

else cout << "Unable to open file";

}

void writeOrderintoFile(struct items i,float amount, float weight){
   ofstream myfile ("order.txt");
if (myfile.is_open())
{
myfile <<i.name << " "<<i.grade<< " "<<weight <<" "<<amount;
}
else cout << "Your order is not recorded";

}


int main(){

   int count =   getPricefromFile();
   int input=-1;
   float weight =0;
   cout<<"Enter number to choose the materials:"<<endl;
   for(int i=0;i<count;i++){
       cout<<i<<": "<<item_arr[i].name<<" "<<item_arr[i].grade<<" "<<item_arr[i].rate<<endl;
   }
   do{
   cin>>input;
   cout<<"Enter the weight:"<<endl;
   cin>>weight;
   float amount = weight*item_arr[input].rate;
   writeOrderintoFile(item_arr[input],amount,weight);
   cout<<"Want to continue"<<endl;
   string resp;
   cin>>resp;
   if(resp=="no"){
       break;
   }
   }while(true);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote