0
這是我的程序。它應該從輸入文件讀取每行並以整潔的格式顯示在控制檯上。但是,getline只讀取第一行。getline函數只讀取第一行
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
int main(int argc, char *argv[]){
ifstream inFile;
string state, quantityPurchased, pricePerItem, itemName;
inFile.open("lab11.txt");
if (inFile.fail()){
perror ("Unable to open input file for reading");
return 1;
}
string line;
istringstream buffer;
while(getline(inFile, line)){
buffer.str(line);
buffer >> state >> quantityPurchased >> pricePerItem >> itemName;
cout << left << setw(2) << state << " ";
cout << setw(15) << itemName << " ";
cout << right << setw(3) << quantityPurchased << " ";
cout << setw(6) << pricePerItem << endl;
}
}
輸入文件看起來是這樣的:
TX 15 1.12 Tissue
TX 1 7.82 Medication
TX 5 13.15 Razors
WY 2 1.13 Food
WY 3 3.71 Dinnerware
但它顯示,因爲這(正在舉辦前):
TX 15 1.12 Tissue
TX 15 1.12 Tissue
TX 15 1.12 Tissue
TX 15 1.12 Tissue
TX 15 1.12 Tissue
哦,非常感謝。 – 2015-03-31 21:04:48