目標は、プログラムがテキスト ファイルから読み取られ、# 記号で解析され、アイテムと価格が出力されることです。3 つのアイテムがあるため、ループしているため、繰り返す必要があります。また、アイテムの量を (行に基づいて) カウントし、すべての価格を合計して合計価格を計算する必要もあります。解析する必要があるテキスト ファイルは次のようになります。
ハンマー#9.95 のこぎり#20.15 シャベル#35.40
私のコードは次のとおりです。
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ifstream invoice("invoice2.txt");
string name;
int count = 0;
int totalPrice = 0;
float price = 0.0;
while(invoice.open())
{
getline(file, line);
++count;
for (string line; getline(file, line); )
{
size_t sharp = line.find('#');
if (sharp != string::npos)
{
string name(line, 0, sharp);
line.erase(0, sharp+1);
price = stof(line);
cout << "*** Invoice ***\n";
cout << "----------------------------\n";
cout << name << " $" << price << "\n\n";
cout << "----------------------------\n";
cout << count << " items: " << totalPrice;
}
}
}
return 0;
}
テキストファイルが終了するまでループを繰り返す必要があり、その後中断して合計価格を出力する必要があります