Reputation: 27
So it is a simple problem. Simply read input from a specific file. Use the input as a Celsius temperature and convert this temperature into Fahrenheit then print the results to the user.
The problem seems to occur when I try to save input from the file. Located in the while block. I get an error I know may be caused by trying to use an int value in getline. I am fairly new to c++ and not sure how to do this. I have tried a myriad of ways but none seem to work. Any help would be greatly appreciated!
I did #include <fstream>
.
The file contains these three values '0 50 100'.
This is the section of the code I have been working with:
//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;
Upvotes: 2
Views: 1457
Reputation: 38
The std::getline function you're using saves an input to a string (see: http://www.cplusplus.com/reference/string/string/getline/). If the argument you pass to the function was a string, it would get the whole line from your file, which is "0 50 100" and put it in your string.
You could trying to save it to a string and then splitting it to three parts and converting to ints using atoi or std::stoi in C++11 (check Convert string to int C++) - this way it would be easier to handle possible errors.
But there's an easier way to do it - assuming your numbers are split by spaces and pretty much everything is correct with them, the ">>" operator breaks on spaces. Try:
inFile >> inVal1;
inFile >> inVal2;
inFile >> inVal3;
Also, using cin.ignore() is not necessary when using the inFile buffer. Every stream has a different buffer associated with it (and cin != inFile) so you don't need to clear the cin buffer to read from your file.
Upvotes: 0
Reputation: 392833
I'd suggest the simplest that would work is:
#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;
}
}
If you really must read a line first, do it like:
#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;
}
}
Upvotes: 1