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:
#include <iostream>
#include <string>
#include <fstream>
usingnamespace 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.