C++ Create a program

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?
--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#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;
}
 




Last edited on
I like reading code with code tags.

http://www.cplusplus.com/articles/jEywvCM9/

I really don't like it without...
Corrected Sir, Sorry for the inconvenience.
OP: did you try and compile your program at all? I did and got 24 errors, some of them quite fundamental. You have more problems than just not being
sure how

will it calculate the rate with the weight.
I suggest you compile the program, read the error messages, try and rectify as many of them as you can and then

come back here
i did try my best..
Well, there are lot of fundamentals missing

For example this line:
Price=global[num]*weight
The variable Price is not declared.
global[num] is accessing an array element, but there is no array named global. The line should end with a semicolon.

Also, within the same block of code, there are a lot of independent objects such as
second or eight. These probably belong in that array which was not defined.

Also, none of that code, starting from the line
Global first; is contained inside any function, which it must be.

Later, there's some code which begins like this:
1
2
{
    ofstream out2File;
That looks like a function which has lost its header.

It's clear that you have some knowledge of C++, but it looks like you're trying to go too far, too fast, before having a grasp of fundamentals. Maybe you should slow down a little bit and reconsider the foundations.

See for example the tutorial on functions:
http://www.cplusplus.com/doc/tutorial/functions/

Topic archived. No new replies allowed.