What's new

Trouble with code

  • Thread starter I Doughboy
  • Start date
  • Views 403
I Doughboy

I Doughboy

Premium
Premium
Messages
1,430
Reaction score
102
Points
145
I am writing a simple code that is supposed to input temperatures from a .txt file, convert them to Celsius, and then output the Celsius to another .txt file. My main problem occurs in the " while (inFile >> fnum) { " portion of the code below. I am able to input the numbers from File1.txt, but when it goes to convert them, it doesn't seem to like doing division. If I take out the " (5 / 9) " portion of the conversion, it will input the number, minus the 32, and output the resulting number. When I leave the " (5 / 9) " portion in, all it does is out 0s. Any help is appreciated.

The numbers I used in the inFile.txt file are irrelevant. They just need to be 10 numbers that it can reference.

What I have so far:

Code:
 #include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;
ofstream outFile;

int fnum;
int cnum1;
float cnum;
int x;

inFile.open("File1.txt");
if (inFile.fail()) {
cerr <<"The file was not able to open." << endl;
exit(1);
}

while (inFile >> fnum) {
//This should be something like, cnum = ((fnum - 32) * (5 / 9))
cnum = 5 / 9;[/b]
cnum1 = (fnum - 32);
x = cnum * cnum1;
cout << "Temperature = " << x << " " << endl;
}

outFile.open("File2.txt");
outFile << x << endl;

outFile << "\nThis is the end of the of the file";

inFile.close();
outFile.close();

return 0;
}
 
amd42

amd42

Getting There
Messages
826
Reaction score
595
Points
180
5 / 9 is an integer expression which will always result in zero. Make the 9 be a float by changing it to 9.f and you'll be good to go.
 
I Doughboy

I Doughboy

Premium
Premium
Messages
1,430
Reaction score
102
Points
145
I got that to work, but now I need it to append all 10 numbers to the output file. Right now it will only wright the 10th number to the file. Would you know how to do that?
 
Top Bottom
Login
Register