read from file
Jun 1, 2013 at 1:20am UTC
So part of my school assignment is to read from a file. In my file, I have "0 50 100". Why is it that when I cout the contents of Line1, Line2 & Line3, I get something other than 0 50 & 100. I have tried declaring the variables as string, int, float & double and I get something different each time but NEVER 0 50 or 100.
Everything else works as intended.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//-------------------Function declarations
int GetUserInput();
int ConvertTemp(double CelsiusTemp);
void PerformFileOperations();
//-------------------Main body
int main()
{
double ConvertedTemp;
double CelsiusTemp;
CelsiusTemp = GetUserInput();//GetUserInput function
ConvertedTemp = ConvertTemp(CelsiusTemp);//ConvertTemp function
cout << CelsiusTemp << " degrees celsius converts to " << ConvertedTemp << " degrees farenheit." << endl;
PerformFileOperations();
return 0;
}//end main
//------------------Functions
//------------------GetUserInput
int GetUserInput ()
{
double CelsiusTemp;
cout<< "Enter Celcius Temperature to Convert::" << endl;
cin>>CelsiusTemp;
return (CelsiusTemp);
}//end GetUserInput
//------------------ConvertTemp
int ConvertTemp (double CelsiusTemp)
{
double ConvertedTemp;
ConvertedTemp = (9.0/5.0) * CelsiusTemp + 32.0;
return ConvertedTemp;
}//end ConvertTemp
//------------------PerformFileOperations
void PerformFileOperations()
{
int Line1, Line2, Line3;
ifstream DataFile;
DataFile.open("celsius_input.dat" );
DataFile >> Line1 >> Line2 >> Line3;
DataFile.close();
cout << Line1 << ' ' << Line2 << ' ' << Line3 << endl;
}//end PerformFileOperations
Jun 1, 2013 at 6:58am UTC
works fine for me
my output
Enter Celcius Temperature to Convert::
100
100 degrees celsius converts to 212 degrees farenheit.
0 50 100
Last edited on Jun 1, 2013 at 6:59am UTC
Jun 1, 2013 at 11:39am UTC
Thanks. Maybe this is one of the quirks of Xcode.
Topic archived. No new replies allowed.