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.
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;
}
It looks like you're new here. If you want to get involved, click one of these buttons!
Comments
hey iam using netbean so how could it be?