2016-04-24 75 views
-1

我是一名C++初學者,嘗試創建一個程序來跟蹤每個服務員在酒吧中服務的產品。其中一部分是從文本文件中讀取價格。如何從C++文件的特定行中提取數字(浮點數)?

每行是一個特定產品的價格。

用戶應該輸入產品「代碼」,這實際上是已經服務的產品線(所有這些將在一個循環中,因爲許多產品將被送達)。

當用戶輸入一個數字,比如說5,我如何從文本的第5行中獲得價格?

也許有一種方法可以在程序啓動時將文件導入數組,但我不知道該怎麼做。

更新: 我終於修復了代碼錯誤,但我真的不滿意我的程序。就我而言,當它問Y/N特價時,我只輸入特價,它解釋爲「NO」。此外,我現在希望它記錄每個服務員獲得的總金額,並且每個服務員都有一個與其他人相似的文本文件名(例如,第一個服務員的姓名位於waiters.txt的第一行,並繼續。 ...),但根據當天的情況,只有4-5名服務員。如何擴展程序而無需從頭開始?

我自己解決了。要從文件中獲取行,請使用此代碼

char product_names[101][15]; 
fstream file("prices.txt"); 
     for(int i = 1; i <= 100; ++i) 
     { 
     file >> product_prices[i]; 
     } 
fstream file2("names.txt"); 
     for(int i = 1; i <= 100; ++i) 
     { 
     file >> product_names[i]; 
     } 

以下是整個程序的最終代碼,歡迎對可能的改進發表評論。

#include <iostream> 
#include<windows.h> 
#include<iostream> 
#include<fstream> 
#include<iomanip> 
#include <string> 
using namespace std; 

int main() 
{ 
float sum[9]={0,0,0,0,0,0,0,0,0}; 
float product_prices[101]; 
int code; 
float total; 
char waiter_name[9][15]; 
float price; 
int wc; 
cout <<"How many waiters are there? \n"; 
int w; // Maximum 9 waiters 
cin >> w; 
for (int i=1; i<=w; ++i) 
    {cin >>waiter_name[i];} 

string a; 
char product_names[101][15]; 
fstream file("prices.txt"); 
     for(int i = 1; i <= 100; ++i) 
     { 
     file >> product_prices[i]; 
     } 
fstream file2("names.txt"); 
     for(int i = 1; i <= 100; ++i) 
     { 
     file >> product_names[i]; 
     } 
ST: 
while (true) 
{ cout << "Please give product code or type -1 when you're done. \n"; 
    cin >> code; 
    if (code==-1) break; 
    cout << "Please give the waiter's code. \n"; 
    cin >> wc; 
    price=product_prices[code]; 
    cout <<"Default price is " << price << " . Type Y/N if you want to make a special price or A to chose another product or waiter\n"; 
    cin >> a; 
    if (a=="Y"||a=="y") { cin >> price; } 
    else if (a=="A" || a=="a") {goto ST;} 

    sum[wc]+=price; 
    cout << waiter_name[wc] <<" : " <<sum[wc] <<"\n"; 

} 
for (int i=1; i<=w; ++i) 
{ 
    cout << waiter_name[i] <<" : " <<sum[i] <<"\n"; 
    total+= sum[i]; 
} 


return 0; 
} 

也@ArchbishopOfBanterbury我沒想到你只是寫這整個程序。但是,我不明白緩衝區的作用,我只有一個product_prices.txt和一個product_names.txt,產品代碼是該行的編號。至於未經測試的代碼,它遠遠超出我的知識水平,我甚至不知道如何使用地圖。不過謝謝你。

產品價格放置在文件中,一個在另一個之下。名字也以這種方式存儲。例如,第一個產品的名稱位於product_names.txt的第一行,它的價格位於product_prices.txt的第一行。

+0

您在請求用戶輸入之前解析文件。將所有價格保存在'std :: vector'中。 – StoryTeller

+1

你的問題非常廣泛,你能否請你張貼一些你試過的代碼,以及你被困在哪裏? –

+0

格式很糟糕。爲什麼你有沒有「goto」的標籤? –

回答

0

下面的代碼提供了一種方法的粗略輪廓,其中輸入文件的每一行都保存到std::map,其中映射的鍵與輸入文件的行相對應,值是價格該文件的這一行。然後可以使用find方法std::map快速訪問與文件的一行相關的價格,該方法將迭代器返回到地圖中搜索的位置。

#include <fstream> 
#include <iostream> 
#include <map> 
#include <string> 
#include <utility> 

int main(void) { 

    std::map<unsigned int, double> line_price_map; 

    // give file name (and absolute path if not in current directory) here 
    const char* file_path = ""; 
    std::ifstream file(file_path); 

    std::string buffer = ""; 
    unsigned int count = 0; 
    while (getline(file, buffer)) { 
     // convert std::string buffer to a double 
     double price = atof(buffer.c_str()); 
     line_price_map.insert(std::make_pair(++count, price)); 
    } 

    // search for a specific line: 
    double search_price = line_price_map.find(5)->second; 
} 

此代碼是不完整的,當然,也需要改變對您的具體要求。

至於存儲產品名稱和產品代碼,我會在下面的格式包含這些數據的文本文件:

product_code product_name product_price 
...    ...    ... 

,這樣你可以讀取行由行使用getline作爲內容之前,分析輸入來獲得該產品的代碼,它的名稱和價格,然後在std::map結構將這些值存儲像這樣的:

std::map<unsigned int, std::pair<std::string, double>> product_map; 

其中unsigned int類型的主要代表產品代碼(也可以是std::string)和std::pair<std::string, double>(與密鑰關聯的映射的值)給出product_name(作爲std::string)和產品價格(作爲double)。

這裏的一些(未經測試)的代碼,我只是寫這樣可以很清楚:

#include <fstream> 
#include <iostream> 
#include <map> 
#include <string> 
#include <utility> 

/** 
* Parses std::string _buffer line input, assigning correct data 
* to code, name and price params. 
*/ 
void parse_input(unsigned int& _prod_code, std::string& _prod_name, 
    double& _prod_price, const std::string& _buffer) { 
    size_t count1 = 0; 
    // find first tab character 
    while (_buffer.at(count1) != '\t') { 
     ++count1; 
    } 
    // set product code to substring of _buffer from beginning 
    // to first tab, converted to integral type 
    _prod_code = atoi(_buffer.substr(0, count1).c_str()); 
    size_t count2 = count1 + 1; 
    // find second tab character 
    while (_buffer.at(count2) != '\t') { 
     ++count2; 
    } 
    // set product code to substring of _buffer from end of first tab 
    // to next tab occurrence 
    _prod_name = _buffer.substr(count1 + 1, count2); 
    size_t count3 = count2 + 1; 
    while (_buffer.at(count3) != '\t') { 
     ++count3; 
    } 
    // set product price to last entry of tabbed columns of input 
    _prod_price = atof(_buffer.substr(count2 + 1, count3).c_str()); 
} 

int main(void) { 
    std::map<unsigned int, std::pair<std::string, double>> product_map; 

    const char* pfile_path = "product_data.txt"; 
    std::ifstream product_file(pfile_path); 

    std::string buffer = ""; 
    // get file contents line by line 
    while (getline(product_file, buffer)) { 
     unsigned int product_code; 
     std::string product_name; 
     double product_price; 
     // call parse_input with product data vars and buffer 
     parse_input(product_code, produce_name, product_price, buffer); 
     // insert data to product_map 
     product_map.insert(std::make_pair(product_code, 
          std::make_pair(product_name, product_price))); 
    } 
} 

它使用一個相當粗糙解析功能,但它應該工作還算不錯 - 作爲顯示產品數據選項卡中的文件中分隔假設以上。

相關問題