TXT INFILEから変数情報を割り当てる:これは私のテキストファイルであるC++
12345 shoe 5 0 34534 foot 72 1 34562 race 10 0 34672 chicken 24 150 88 pop 65 0
私はこのファイルを取得し、行ごとを行く、識別子itemNum
として最初の番号を割り当て、第二する必要があります単語はitemName
、第3の数字はitemPrice
、最後の数字はitemAdjusmentValue
です。最後の2つの数字であるitemPrice
とitemAdjusmentValue
を使って算術演算を行う必要があります。これまで
コード:文
'>>' の前にトークンのプライマリ表現を期待しながら、
using namespace std; // making a struct to store each value of the cuadre struct Cuadre { int itemNum; string itemName; int itemPrice; int itemAdjusment; }; int main(){ ifstream infile("sheet_1.txt"); string checkLine; if (infile.is_open()){ while (infile.good()){ getline (infile, checkLine); cout << checkLine << endl; } } else cout << "error with name of file" << endl; vector<Cuadre> procedures; Cuadre line; while(Cuadre >> line.itemNum >> line.itemName >> line.itemPrice >> line.itemAdjusment){ procedures.push_back(line); }
このコードは
私は本当にカント最後にエラーが発生しますこれを行う方法に関する具体的なチュートリアルを見つけると、私は良い量を見てきました。
「checkLine >>」ではありませんか? – Barmar
そして、ファイルから読み込むループの内側にあるはずです。それ以外の場合は、最後の行を処理しているだけです。 – Barmar
@JonathanWakely既に私の答えでそれを修正しました。 – Barmar