This program reads temperatures from a file and converts then from F to C. The problem is for some reason if the temperature is negative or has a decimal point, it comes out something like 1e+002. If I don't use setprecision this doesn't happen. I'm sure it's something little I've overlooked, but I've been reading the forums all day trying to find someone with a similar problem and haven't yet.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
#include <cmath>
int main()
{
//variable for the degree symbol
char degreeSymbol = 0xF8;
ifstream fin;
fin.open("temps.txt");
if (!fin.good()) throw"I/O error";
// end of file loop to check for sentinel value of -999
while (fin.good())
{
// read an int from one line of an input file
float a;
fin >> a;
fin.ignore(1000, 10);
// sentinel value
if (a == -999) break;
//programmer supplied formula for conversion
float b = 1.8 * a + 32;
//unformatted output
cout << a << degreeSymbol << " Celsius equals ";
//formatting output to 1 decimal place
cout.setf(ios::fixed|ios::showpoint);
cout << setprecision(1);
//formatted output
cout << b << degreeSymbol << " Fahrenheit." << endl << endl;
cout.unsetf(ios::fixed|ios::showpoint);
}
fin.close();
}
I'm still having an issue whether I leave the cout.unsetf(ios::fixed | ios::showpoint); where it is or move it to after the fin.close(); . Maybe showing my inputs will help, If I use these inputs in my temps.txt file with the unset where it is
1 2 3 4 5 6
100
-40
100.01
5
0
-999
These are the inputs that get echo'd to the console screen
1 2 3 4 5
100
-4e+001
1e+002
5
0
All of the converted outputs are correct, it's just the inputs that it shows wrong
If i move the cout.unsetf(ios::fixed | ios::showpoint); to after the fin.close(); , all of the input temps after the first one get truncated to one decimal place like this
1 2 3 4 5
100
-40.0
100.0
5.0
0.0
the sentinal -999 is only in there to show that it won't print it
You do have setprecision(), set to 1, so it will be truncated to just 1 decimal point. I also changed the declaration of a and b, to a double. I got tired of the warnings during compiling.
#include <fstream>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
//variable for the degree symbol
char degreeSymbol = '\xF8';
double a, b;
ifstream fin;
fin.open("temps.txt");
if (!fin.good())
{
cout << "Temperature list, could not be found. Aborting." << endl;
return 1;
}
// end of file loop to check for sentinel value of -999
while (fin.good())
{
// read an int from one line of an input file
fin >> a;
fin.ignore(2, '\n');// Just my style of doing it. Nothing wrong with yours
// sentinel value
//if (a == -999) break;
//programmer supplied formula for conversion
b = 1.8 * a + 32;
//unformatted output
cout << a << degreeSymbol << " Celsius equals ";
//formatting output to 1 decimal place
cout.setf(ios::fixed | ios::showpoint);
cout << setprecision(1);
//formatted output
cout << b << degreeSymbol << " Fahrenheit." << endl << endl;
}
fin.close();
cout.unsetf(ios::fixed | ios::showpoint);
cin >> a;// Just to prevent screen from closing.
}
Perfect! Line 56 and 62 of JLBorges post was what I needed. Now when I run it the inputs are unformatted and the outputs are formatted. Heres my updated code, which works the way I needed it to.
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
#include <cmath>
int main()
{
//variable for the degree symbol
char degreeSymbol = 0xF8;
ifstream fin;
fin.open("temps.txt");
if (!fin.good()) throw"I/O error";
// end of file loop to check for sentinel value of -999
while (fin.good())
{
// read an int from one line of an input file
double a;
fin >> a;
fin.ignore(1000, 10);
// sentinel value
if (a == -999) break;
//programmer supplied formula for conversion
double b = 1.8 * a + 32;
//unformatted output
cout << a << degreeSymbol << " Celsius equals ";
//formatting output to 1 decimal place
cout.setf(ios::fixed|ios::showpoint);
int default_precision = cout.precision();
cout << setprecision(1);
//formatted output
cout << b << degreeSymbol << " Fahrenheit." << endl << endl;
cout.unsetf(ios::fixed|ios::showpoint);
cout << setprecision(default_precision);
cout.precision();
}
fin.close();
}