2016-04-24 157 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))); 
    } 
} 

它使用一个相当粗糙解析功能,但它应该工作还算不错 - 作为显示产品数据选项卡中的文件中分隔假设以上。

相关问题