【问题标题】:How do I get multiple lines of strings and integers to read from and input file and display in C++?如何获取多行字符串和整数以读取和输入文件并在 C++ 中显示?
【发布时间】:2016-11-05 01:19:34
【问题描述】:

我被分配读取下面列出的数据文件:

Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49

将此数据输入C++,然后以矩阵形式显示,并输出CD的总价格和销售了多少CD。到目前为止,我只能让第一行正确显示,并且无法弄清楚我需要更改什么才能让其余部分显示。这是我到目前为止所写的。我还没有开始输出代码,感觉好像我不会有问题。

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

int main()
{
ifstream inputFile, processLine;
ofstream outputFile;
string title, band;
double price, total = 0;

inputFile.open("orders.txt");
while (!inputFile.eof())
{
    getline(inputFile, title);
    getline(inputFile, band);
    inputFile >> price;
    total += price;

    cout << "Welcome to Megan McCracken's Online Music Store" << endl;
    cout << "You have submitted the following order:" << endl;
    cout << "***************************************************************************" << endl;
    cout << "Title" << setw(46) << "Artist" << setw(24) << "Cost" << endl;

    cout << title << setw(28) << band << setw(22) << fixed << setprecision(2) << price << endl;
    cout << title << setw(30) << band<< setw(19) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
    cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;

    cout << "--------------------------------------------------------------------------" << endl;
    cout << "Total Due:" << setw(75) << fixed << setprecision(2) << total << endl;
    cout << "==========================================================================" << endl;

}


    /* getline(inputFile, title);
    cout << left << title;
    getline(inputFile, band);
    cout << setw(23) << band;
    inputFile >> price;
    cout << setw(10) << fixed << setprecision(2) << price << endl; */






system("pause");
return 0;

}

【问题讨论】:

标签: c++ string double output inputstream


【解决方案1】:

这段代码存在多个问题。

while (!inputFile.eof())

This is always a bug。有关详细信息,请参阅链接文章。

但这不是唯一的问题。

getline(inputFile, band);
inputFile >> price;

不要在同一个输入流上同时使用std::getline()operator&gt;&gt;operator&gt;&gt; 在处理空白时具有一定的、严格限制的语义。在这里,operator&gt;&gt; 不会消耗尾随的换行符,因此在下一次循环迭代中,第一个 getline() 将偏离轨道。

虽然经常建议使用一些创可贴修复,但在这种情况下,最简单的解决方案是干脆不使用operator&gt;&gt;

第三次使用getline(),将第三行读入std::string。然后用它来构造一个独立的std::istringstream,并与它一起使用operator&gt;&gt;。问题解决了。

修复后,有一些逻辑错误需要修复。报告的标题可能应该在循环之前显示,并且在循环内唯一应该做的就是显示单行收据,并将总数相加,然后在收到文件结尾后显示总数.

【讨论】:

  • 所以你修复的效果很好,但是假设我们不能使用 std::istringstream 有什么选择。我们只允许使用 getline 和 inputFile >> 函数。有没有办法做到这一点?
  • 这不是一个合理的假设。 std::istringstream 是 C++ 库的一部分。它会存在,并且工作得很好,在任何你能找到 std::getlineoperator&gt;&gt; 的地方。
猜你喜欢
  • 1970-01-01
  • 2021-05-17
  • 2023-04-09
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多