2016-04-24 3 views
-1

私はC++の初心者で、各ウェイターがバーで働く製品を追跡するプログラムを作成しようとしています。その一部は、テキストファイルから価格を読むことです。C++でファイルの特定の行から数値(浮動小数点数)を抽出する方法は?

各行は特定の製品の価格です。

ユーザーは実際に提供された製品の行である製品 "コード"を入力することになっています(これは多くの製品が提供されるためループします)。

ユーザーが数字を入力すると、5としましょう。テキストの5行目の価格を取得するにはどうすればよいですか。

多分、プログラムが起動したときにファイルを配列にインポートする方法がありますが、その方法はわかりません。

更新: 私は最終的にコードのエラーを修正しましたが、私のプログラムには本当に不満です。私が特別価格でY/Nを尋ねたときでさえ、私はちょうど特別価格を入力し、それは「いいえ」と解釈します。また、各ウェイターが獲得したお金の合計を把握し、それぞれのウェイターが他の人に似たテキストファイルに名前を付けるようにしたいとします(たとえば、最初のウェイター名が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私はあなたがこのためにこの全体のプログラムを書くことを期待しませんでした。しかし、私はバッファが何を理解することはできません、私はちょうど1つのproduct_prices.txtとproduct_names.txtを持って、製品コードは、行の番号です。テストされていないコードについては、それは私の知識レベルをはるかに上回っています。私はマップの使い方を知らない。それにもかかわらずありがとう。

製品価格は、ファイルの下に1つずつ表示されます。名前もこの方法で保存されます。たとえば、最初の商品の名前はproduct_names.txtの最初の行にあり、商品の価格はproduct_prices.txtの1行目です。

+0

ユーザーに入力を求める前に、ファイルを解析します。すべての価格を 'std :: vector'に保存します。 – StoryTeller

+1

あなたの質問は非常に幅広く、試行したコードやあなたが特に悩んでいる場所を投稿してください。 –

+0

フォーマットがひどいです。そしてなぜあなたには「goto」のないラベルがありますか? –

答えて

0

次のコードは、入力ファイルの各行が入力ファイルの行に対応するstd::mapに保存され、その値がこのファイルの行。次に、ファイルの行に関連付けられた価格は、のfindメソッドを使用してすぐにアクセスすることができ、マップ内で検索された位置にイテレータを返します。

#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; 
} 

このコードは完全なものではなく、正確な要件を変更する必要があります。製品名と製品コードを格納するよう

、私は次の形式で、これらのデータを含むテキストファイルを持っているでしょう:

あなたのように getlineを使用してライン・バイ・ライン内のコンテンツを読むことができるよう
product_code product_name product_price 
...    ...    ... 

タイプunsigned intのキーは(PRODUCT_CODEを表し

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

:前に、製品のコードを取得するには、入力を解析し、それがその後、このようなstd::map構造でこれらの値を保存する名前と価格ですまた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))); 
    } 
} 

これはかなりの粗解析関数を使用していますが、それはかなりうまく動作するはずです - 示すように、製品データを仮定すると、ファイルにタブ区切りですが、上記。

関連する問題