What's new

# Trouble with code

• I Doughboy
• 403

#### I Doughboy

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

##### Getting There
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.

Thank you!

#### I Doughboy

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?

Replies
8
Views
469
Sombra
M
Replies
1
Views
426
LawlessBaron
L
Replies
3
Views
317
Im4eversmart
Replies
2
Views
190
Cuddz
Replies
0
Views
586
-Unh0ly-