所以這是一個簡單的問題。只需閱讀來自特定文件的輸入即可。使用輸入作爲攝氏溫度並將此溫度轉換爲華氏溫度,然後將結果打印給用戶。從fstream讀取輸入
這個問題似乎發生時,我嘗試從保存文件輸入。位於時段。我知道可能是由於嘗試在getline中使用int值導致的錯誤。我對C++相當陌生,不確定如何做到這一點。我嘗試了無數種方法,但似乎沒有任何工作。任何幫助將不勝感激!
我做#include <fstream>
。
該文件包含這三個值 '0 50 100'。
這是我一直在用的代碼段:
//values for the files three input values
int i = 0, inVal1 = 0 , inVal2 = 0, inVal3 = 0,
farenheit1 = 0, farenheit2 =0,farenheit3 = 0;
ifstream inFile; //Input file variable
inFile.open("celsius_input.dat"); //open the file
if(!inFile){
cout << "Unable to open file";
exit(1); //terminate with error
}//end if
while (inFile)
{
cin.ignore();
getline(inFile, inVal1);
getline(inFile, inVal2);
getline(inFile, inVal3); // read the files values
inFile.close();//Close file
} //end while
farenheit1 = (9.0/5.0) * inVal1 + 32.0; //formula
farenheit2 = (9.0/5.0) * inVal2 + 32.0; //formula
farenheit3 = (9.0/5.0) * inVal3 + 32.0; //formula
cout << "The first Inputed Value, " << inVal1
<< " degrees, Converted Into Farenheit Is "
<< farenheit1 << " Degrees!" << endl; //output of results
cout << " " << endl;
cout << "The Second Inputed Value, " << inVal2
<< " degrees, Converted Into Farenheit Is "
<< farenheit2 << " Degrees!" << endl; //output of results
cout << " " << endl;
cout << "Teh Third Inputed Value, " << inVal3
<< " degrees, Converted Into Farenheit Is "
<< farenheit3 << " Degrees!" << endl; //output of results
cout << " " << endl;
哪一行導致錯誤?你遇到了什麼錯誤? –
while循環中的第一個getline。它說沒有匹配的功能。 – user2445852
'getline'接受'string'作爲它的第二個參數,而不是int。 –