2013-06-02 77 views
2

所以這是一個簡單的問題。只需閱讀來自特定文件的輸入即可。使用輸入作爲攝氏溫度並將此溫度轉換爲華氏溫度,然後將結果打印給用戶。從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; 
+0

哪一行導致錯誤?你遇到了什麼錯誤? –

+0

while循環中的第一個getline。它說沒有匹配的功能。 – user2445852

+0

'getline'接受'string'作爲它的第二個參數,而不是int。 –

回答

0

std :: getline函數y您正在使用將輸入保存到字符串中(請參閱:http://www.cplusplus.com/reference/string/string/getline/)。如果你傳遞給函數的參數是一個字符串,它會從你的文件中得到整個行,即「0 50 100」,並將其放入你的字符串中。

您可以嘗試將其保存爲字符串,然後將其拆分爲三部分並使用C++ 11中的atoi或std :: stoi轉換爲整數(檢查Convert string to int C++) - 這樣可以更容易地處理錯誤。

但有一個更簡單的方法來做到這一點 - 假設你的號碼之間用空格分開,幾乎一切是正確的他們,在空間中的「>>」操作符。嘗試:

inFile >> inVal1; 
inFile >> inVal2; 
inFile >> inVal3; 

此外,使用inFile緩衝區時不需要使用cin.ignore()。每個流都有一個與之相關的不同緩衝區(和cin!= inFile),因此您不需要清除cin緩衝區即可從文件中讀取。

+0

謝謝大家的幫助,我真的很感激。我只是嘗試了你的例子,它完美地工作。我不知道我可以從使用我的文件變量獲得輸入,但現在看它是有道理的! – user2445852

+0

我很高興我能幫忙,現在正在工作:) – olasia

1

我建議,將工作最簡單的是:

#include <fstream> 
#include <iostream> 

int main() 
{ 
    std::ifstream inFile("celsius_input.dat"); //open the file 

    double celsius; 
    while (inFile >> celsius) 
    { 
     double fahrenheit = (9.0/5.0) * celsius + 32.0; 
     std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl; 
    } 
} 

如果你真的必須閱讀行首先,這樣做︰

#include <fstream> 
#include <iostream> 
#include <string> 

int main() 
{ 
    std::ifstream inFile("celsius_input.dat"); //open the file 

    std::string input; 
    while (std::getline(inFile, input)) 
    { 
     double celsius = std::strtod(input.c_str(), nullptr); 
     double fahrenheit = (9.0/5.0) * celsius + 32.0; 
     std::cout << "The input value " << celsius << " degrees, converted into fahrenheit is " << fahrenheit << " degrees" << std::endl; 
    } 
}