基本上,我需要一個基本的.txt文件 - 可能是一個.csv文件,該文件將被程序讀取並使用位於文件中的數字。 我想這個文件,看起來像這樣:我如何從外部文件中讀取
橙子:1.50
蘋果:2.10
梨:4.70
然後,我會希望程序從第一行看1.50和將其分配給變量orangeCost。那麼我想第二部分閱讀第二行和分配2.10變量蘋果等
這是我到目前爲止有:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
int garden();
//Lawn
int lawnLength;
int lawnWidth;
int lawnTime = 20;
float lawnCost = 15.50;
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
//Concrete Patio
int concreteLength;
int concreteWidth;
int concreteTime = 20;
float concreteCost = 20.99;
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
//Wooden Deck
int woodenDeckLength;
int woodenDeckWidth;
int woodenDeckTime = 30;
float woodenDeckCost = 15.75;
cout << "Length of wooden deck required: "; // Asks for the length
cin >> woodenDeckLength; // Writes to variable
cout << "Width of wooden deck required: "; // Asks for the width
cin >> woodenDeckWidth; // Writes to variable
int woodenDeckArea = (woodenDeckLength * woodenDeckWidth); //Calculates the total area
cout << endl << "Area of wooden deck required is " << woodenDeckArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (woodenDeckArea * woodenDeckCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl; //Prints total time
//Rectangular Pond
int rectangularPondLength;
int rectangularPondWidth;
int rectangularPondTime = 45;
float rectangularPondCost = 25.00;
cout << "Length of rectangular pond required: "; // Asks for the length
cin >> rectangularPondLength; // Writes to variable
cout << "Width of rectangular pond required: "; // Asks for the width
cin >> rectangularPondWidth; // Writes to variable
int rectangularPondArea = (rectangularPondLength * rectangularPondWidth); //Calculates the total area
cout << endl << "Area of rectangular pond required is " << rectangularPondArea << " square meters"; //Prints the total area
cout << endl << "This will cost a total of " << (rectangularPondArea * rectangularPondCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl; //Prints total time
//Water Features
int waterFeatures;
int waterFeaturesTime = 60;
float waterFeaturesCost = 150.00;
cout << "Number of water features required: "; // Asks for the amount of water features needed
cin >> waterFeatures; // Writes to variable
cout << endl << "Number of water feature(s) required is " << waterFeatures << " water feature(s)"; //Prints the total area
cout << endl << "This will cost a total of " << (waterFeatures * waterFeaturesCost) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl; //Prints total time
//Garden Lights
int gardenLights;
int gardenLightsTime = 10;
float gardenLightsCosts = 5.00;
cout << "Number of garden lights required: "; // Asks for the amount of water features needed
cin >> gardenLights; // Writes to variable
cout << endl << "Number of garden light(s) required is " << gardenLights << " garden light(s)"; //Prints the total area
cout << endl << "This will cost a total of " << (gardenLights * gardenLightsCosts) << " pounds"; //Prints the total cost
cout << endl << "This will take a total of " << (gardenLights* gardenLightsTime) << " minutes" << endl << endl; //Prints total time
//Quotation
string quotation;
cout << "Do you want to save a quotation? " << endl; //Asks if they want to save a quotation
cin >> quotation; //Saves if they want to save a quotation or not
if (quotation == "yes")
{
std::string nameOfFile;
cout << "What would you like to save the quotation as? (Maybe using your last name) " << endl;
cin >> nameOfFile;
ofstream outfile(nameOfFile + ".txt"); // Opens file in write mode
//Lawn
outfile << "The total lawn area is " << lawnArea << " square meters" << endl; // Writes users data into the file
outfile << "The total cost for the lawn is £" << (lawnArea * lawnCost) << endl; // Writes users data into the file
outfile << "The total time for the lawn is " << (lawnArea * lawnTime) << " minutes" << endl << endl; // Writes users data into the file
//Concrete Patio
outfile << "The total concrete patio area is " << concreteArea << " square meters" << endl;
outfile << "The total cost for the concrete patio is £" << (concreteArea * concreteCost) << endl;
outfile << "The total time for the concrete patio is " << (concreteArea * concreteTime) << " minutes" << endl << endl;
//Wooden Deck
outfile << "The total wooden deck area is " << woodenDeckArea << " square meters" << endl;
outfile << "The total cost for the wooden deck is £" << (woodenDeckArea * woodenDeckCost) << endl;
outfile << "The total time for the wooden deck is " << (woodenDeckArea * woodenDeckTime) << " minutes" << endl << endl;
//Rectangular Pond
outfile << "The total rectangular pond area is " << rectangularPondArea << " square meters" << endl;
outfile << "The total cost for the wooden deck is £" << (rectangularPondArea * rectangularPondCost) << endl;
outfile << "The total time for the wooden deck is " << (rectangularPondArea * rectangularPondTime) << " minutes" << endl << endl;
//Water Features
outfile << "The total number of water feature(s) needed are " << waterFeatures << endl;
outfile << "The total cost for the water feature(s) is £" << (waterFeatures * waterFeaturesCost) << endl;
outfile << "The total time for the water feature(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;
//Garden Lights
outfile << "The total number of garden light(s) needed are " << gardenLights << endl;
outfile << "The total cost for the garden light(s) is £" << (gardenLights * gardenLightsCosts) << endl;
outfile << "The total time for the garden light(s) is " << (waterFeatures * waterFeaturesTime) << " minutes" << endl << endl;
//CLOSE FILE
outfile.close(); // Close's the file.
cout << "Quotation saved." << endl;
}
else
{
cout << "Quotation not saved." << endl;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
感謝您的幫助! :)
你粘貼正確的代碼?您發佈的代碼似乎與蘋果和橘子沒有任何關係。 –
http://www.cplusplus.com/doc/tutorial/files/ –
請在StackOverflow中搜索「從文件解析中讀取C++」。這裏我們不需要重複。 –