How To Use fstream To Read Multiple Lines

At the moment I am using my code to read multiple files, but i would like to be able to use my program to read certain lines in the single file, for example, at the moment I have server text files like this:
 
 lawn 20

 
 concrete 30

What I would like is them to all be in one file like this:
1
2
lawn 20
concrete 30


This is a sample of my code at the moment:
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main() {
int gardenn();

//class costs
//{
//   private:
//      string name;   // Name
//      float cost;  // Cost
//};
//					


	//Lawn
	int	lawnLength;			
	int lawnWidth;
	int lawnTime = 20;
	
	float lawnCost;
	string lawnName;
	ifstream lawn;
	lawn.open("lawnprice.txt");
	lawn >> lawnName >> lawnCost;

	cout << "Length of lawn required: "; // Asks for the length
	cin >> lawnLength; // Writes to variable
	cout << "Width of lawn required: "; // Asks for the width
	cin >> lawnWidth; // Writes to variable
	int lawnArea = (lawnLength * lawnWidth); //Calculates the total area
	cout << endl << "Area of lawn required is " << lawnArea << " square meters"; //Prints the total area
	cout << endl << "This will cost a total of " << (lawnArea * lawnCost) << " pounds"; //Prints the total cost
	cout << endl << "This will take a total of " << (lawnArea * lawnTime) << " minutes" << endl << endl; //Prints total time
	int totalLawnTime = (lawnArea * lawnTime);
	
	//Concrete Patio
	int	concreteLength;			
	int concreteWidth;
	int concreteTime = 20;
	float concreteCost;
	string concreteName;
	ifstream concrete;
	concrete.open("concreteprice.txt");
	concrete >> concreteName >> concreteCost;

	cout << "Length of concrete required: "; // Asks for the length
	cin >> concreteLength; // Writes to variable
	cout << "Width of concrete required: "; // Asks for the width
	cin >> concreteWidth; // Writes to variable
	int concreteArea = (concreteLength * concreteWidth); //Calculates the total area
	cout << endl << "Area of concrete required is " << concreteArea << " square meters"; //Prints the total area
	cout << endl << "This will cost a total of " << (concreteArea * concreteCost) << " pounds"; //Prints the total cost
	cout << endl << "This will take a total of " << (concreteArea * concreteTime) << " minutes" << endl << endl; //Prints total time
	int totalConcreteTime = (concreteArea * concreteTime);
You should generalize your code and use a loop. also wouldn't hurt to have a function to do your calculating. Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Here you can ask your questions about dimensions and calculate cost
float calculate(string item, float unitCost);

int main()
{
    // Read your items and costs in a while loop
    string item;
    float  unitCost;
    ifstream catalog;
    catalog.open("priceCatalog.txt");
    while (catalog >> item >> unitCost)
    {
        calculate(item, unitCost);
    }
    
    return 0;
}


I'm leaving the implementation of the calculate function as an exercise, although you pretty much have it implemented twice in your current code.
Topic archived. No new replies allowed.