0
我遇到了istringstream不存儲它讀取的值的問題。這裏是我有:istringstream不存儲變量中的任何東西
if(inputFile.good()){ //Make sure file is open before trying to work with it
//Begin Working with information
cout << "\tIn File: " << input << endl;
cout << "------------------------------------" << endl;
int number_of_lines = 0;
std::string line;
while (std::getline(inputFile, line)){
++number_of_lines;
}
Time times[number_of_lines];
double math[number_of_lines];
std::string input;
int hh, mm;
for(int loop=0;loop<number_of_lines;loop++){
std::getline(inputFile, input);
std::istringstream(input) >> mm >> hh >> math[loop];
cout << "hours = " << hh << endl;
times[loop].setTimeHours(hh);
times[loop].setTimeMinutes(mm);
times[loop].show();
cout << "*" << math[loop] << endl;
}
std::cout << "Number of lines in text file: " << number_of_lines << "\n" << endl;
}else{
cout << "Could not open file!!!" << endl;
}
的文件我讀看起來像這樣:
90 1 3.0
1 1 100.0
2 34 5.1
和輸出,當我運行:
In File: data04.txt
------------------------------------
hours = 0
Operation To Be Done = 0:2336552*1.15384e-317
hours = 0
Operation To Be Done = 0:2336552*1.58101e-322
hours = 0
Operation To Be Done = 0:2336552*1.15397e-317
Number of lines in text file: 3
任何人都知道爲什麼它不存儲價值?
你檢查的讀操作是成功的? (只是一個修辭問題,當然是由代碼回答) –
嗯,我只是做了一個'cout << input << endl;'以確保輸入中有東西,但它打印了一個空行,所以某些東西不是'沒有正確的工作!大聲笑 – user2990286
是的。這不會被視爲驗證輸入是否成功。類似於'if(std :: getline(inputFile,line))'或'while(std :: getline(inputFile,line))'或'if(std :: istringstream(intput)>> mm >> hh)將算作驗證。 –