2013-12-19 30 views
1

基本上,我需要一個基本的.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; 
} 

感謝您的幫助! :)

+2

你粘貼正確的代碼?您發佈的代碼似乎與蘋果和橘子沒有任何關係。 –

+0

http://www.cplusplus.com/doc/tutorial/files/ –

+0

請在StackOverflow中搜索「從文件解析中讀取C++」。這裏我們不需要重複。 –

回答

0

下面是做這件事的方式:

創建一個類fruit與私有變量namecost

然後創建一個構造函數,該構造函數接受一個字符串(來自輸入文件的一行)。將所有內容提取到':'爲name,後面的值爲cost

你的函數讀取文件需要做這樣的事情:

  • 聲明水果的數組
  • 循環:
    • 得到文件中的一行到一個變量
    • 用這個變量調用水果類,a第二添加創建的對象到一個數組

當你到達文件的末尾,你有水果的數組,其中包含從文件中的所有數據。

學習班,請檢查:http://www.tutorialspoint.com/cplusplus/cpp_classes_objects.htm

瞭解讀取文件,檢查:http://www.cplusplus.com/doc/tutorial/files/

+0

有沒有可以看到的示例代碼?我不太明白如何使這個類和私有變量感謝您 – user3120369

+0

前面創建一個類,谷歌「如何創建一個類在c + +示例」,你會看到幾個教程,例如:http://www.tutorialspoint。 com/cplusplus/cpp_classes_objects.htm – Marjeta

+0

您還有其他問題嗎?請讓我知道,我會詳細解釋我的答案。如果不是,請標記我的答案。 – Marjeta