Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello guys.
Hope you people are doing well.

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?

What I have tried:

--
C++
#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 <<"\n The file was not sucessfully opened. \n 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;
}
Posted
Updated 12-Dec-16 13:22pm
Comments
Richard MacCutchan 11-Dec-16 9:54am    
Help with what? Please explain exactly which part of the program you are having trouble with, and what errors or invalid results you see.
Member 12898483 11-Dec-16 19:28pm    
actually the calculation part, after the user selects a materail , it should calcualt the price. that part.
Richard MacCutchan 12-Dec-16 3:16am    
OK, and what exactly is the difficulty in doing the calculation?

1 solution

For such calculation you must create an array.

C++
Global materials[8];//zero indexed
  //first as sample
  materials[0].Material_name="Cobalt";
  materials[0].grade= "A";
  materials[0].price_per_kg = 6.5;

Price=global[num].price_per_kg *weight; //access to array of structs and its member for calculating the price

This tutorial should help to understand it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900