Read a line in a file and store in an array C++

I have an assignment that asks me to read a file in .txt as following:
"Item: Wood 2.5
Item: Metal 5.5
Item: Cat 900
Item: Spear 50.7
Recipe: Spear = Wood + Wood + Metal ;"

I am asked to print out:

What file to load?
items1.txt
Making Wood, profit=0
Making Metal, profit=0
Making Cat, profit=0
Making Spear, profit=40.2
This means if any item is not included in the recipe, its profit is zero (as the problem defines that way).

This is just my general approach and I am kinda stuck. Please help me move forward with this thought. Thanks!
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <sstream> 

using namespace std;
string nextstring(string str, int start_index);
int split(string str, string a[], int max_size);

int main()
{	
	ifstream in_stream;
	
	string fileName;
	cout << "Enter the file name : ";
	cin >> fileName;
	
	in_stream.open(fileName.c_str());
	
	//error checking
	if (in_stream.fail())
	{
		cout << "File could not be opened." << endl;
		exit(1);
	}
	
	string items[50];
	double items_value[50];
	string recipe[50];
	string rname = recipe[0];
	double profit = 0;
	int j = 0;
    string lines;
    int number_of_lines = 0;
	
	while(getline(in_stream, lines))
    {  
        if(lines.substr(0,5) == "Item:")
			{	
				int beginning = lines.find_first_of(' ') + 1;
				int next_space = lines.find(" ", beginning);
				string temp = (lines.substr(next_space));
				items_value[j] = atof(temp.c_str());
				items[j] = lines.substr(beginning,lines.find_first_of(' ', beginning) - beginning);
				j++;
			}
			
        if(lines.substr(0,7) == "Recipe:")
        {			
			int max_size = lines.length();
			int cnt = split(lines,recipe,max_size);
			double profit1 = 0;
			double profit2 = 0;
			for(int j = 3; j < cnt; j++)
			{
				for(int i = 0; i < 4; i++)
					{
						if((recipe[j] == items[i]) && (recipe[j] != "+")&& (recipe[j] != ";"))
						{
						cout << "Making " << items[i] << ", " << "profit = 0" << endl;			
						profit1 += items_value[i];						
						}
						if(recipe[1] != items[i])
						{
						profit2 = 0;
						}
					}
			}
			for(int i = 0; i < cnt; i++)
			{
				if((recipe[1] == items[i]))
				{
	
					profit = items_value[i];
					cout << "Making " << items[i] << ", " << "profit = ";
				}

			}
			cout << profit - profit1 << endl;
			

		}	
	}
	in_stream.close();
	return 0; 
}

string nextstring(string str, int start_index)
{
	
	int y =0;
	y = str.find(' ',start_index);
	y = y-start_index;
	str = str.substr(start_index,y);	
	return str;
	
}

int split(string str, string a[], int max_size)
{
	int i;
	int num = 0;
	for (i=0; i<max_size; i++)
	{
		
		a[i] = nextstring(str,num);
		num = num + a[i].length() + 1;
		
		if(num >= str.length())
		{
			i++;
			break;
		}
	}
	
	return i;
} 


Last edited on
I am somewhat close I think. I get the following output:
1
2
3
4
5
6
Enter the file name : items1.txt                                             
Making Wood, profit = 0                                                        
Making Wood, profit = 0                                                        
Making Metal, profit = 0                                                        
Making Spear, profit = 40.2                                                     
Press any key to continue . . .   


and also

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Enter the file name : items2.txt                                               
Making Rubber_Chicken, profit = 0                                               
Making Glasses, profit = 0                                                     
Making Substitute_Teacher, profit = 65.8                                        
Making Lint, profit = 0                                                         
Making Lint, profit = 0                                                         
Making Lint, profit = 0                                                         
Making Lint, profit = 0                                                        
Making Lint, profit = 0                                                         
Making Cat_Toy, profit = 49.95                                                  
Making Wood, profit = 0                                                        
Making Lint, profit = 0                                                         
Making Lint, profit = 0                                                         
Making Glasses, profit = 0                                                      
Making Scare_Crow, profit = 100.58                                              
Making Cat_Toy, profit = 0                                                      
Making Rubber_Chicken, profit = 0                                               
50                                                                            
Press any key to continue . . .  
 
Last edited on
Bump
Topic archived. No new replies allowed.