所以这是一个简单的问题。只需从特定文件中读取输入。使用输入作为摄氏温度并将此温度转换为华氏温度,然后将结果打印给用户。
当我尝试保存文件中的输入时,似乎出现了问题。位于 while 块中。我收到一个错误,我知道这可能是由于尝试在 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;