Feb 14, 2015 at 6:21am UTC
I do not know why it does not finish reading the dat file. Any idea?
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
int Number;
int Planet_Name;
int Diameter;
double fah;
double cel;
string num,plan,dia;
ifstream planet;
planet.open ("PlanetCelsius.dat" );
cout << setw(20) << "\n" << "Number" << setw( 15 ) << "Planet_Name" << setw( 10 ) << "Diameter" << setw( 12 ) << "Celsius " <<setw(12)<< "Fahrenheit" << endl;
cout << setprecision( 2 ) << setiosflags( ios::fixed | ios::showpoint );
while (planet>>num>>plan>>dia>>cel>>fah)
{
fah = cel*9/5+32;
cout<< setw(2)<<num<< setw( 15 )<<plan<< setw( 12 )<<dia << setw( 12 )<<cel<<setw(10)<<fah<<endl;
}
std::cout << "Press ENTER to continue..." ;
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
Last edited on Feb 14, 2015 at 6:23am UTC
Feb 14, 2015 at 6:38am UTC
Why do you read into fah and then immediately overwrite its value?
Feb 14, 2015 at 6:42am UTC
What do u mean? ( u always make everything seem so simple lol)
Feb 14, 2015 at 6:50am UTC
Because that was the only way I could find to add the math in to make that string do what I wanted.
Last edited on Feb 14, 2015 at 6:51am UTC
Feb 14, 2015 at 6:53am UTC
Line 22 reads from the file into that variable. Then immediately after that you assign a completely new and unrelated value into that same variable.
Could you show an example PlanetCelsius.dat file?
Feb 14, 2015 at 6:57am UTC
yes I can, but even w/o fah = cel*9/5+32; it does the same thing
example PlanetCelsius.dat file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
4
Mars
6794
-63
5
Jupiter
142984
17
6
Saturn
120536
-130
7
Uranus
51118
-197
8
Neptune
49532
-200
Last edited on Feb 14, 2015 at 5:19pm UTC
Feb 14, 2015 at 7:06am UTC
I moved my post up and didn't know u couldn' t delete old ones :-/
Last edited on Feb 14, 2015 at 5:21pm UTC
Feb 14, 2015 at 5:46pm UTC
Try LB's suggestion. Don't read into fah. There are only 4 data items per planet. With the read to fah, you're reading 5 items per planet. I think a misalignment of data types (trying to read a word into a variable of type double) is occurring on the 2nd iteration, causing the read failure.
Feb 14, 2015 at 6:09pm UTC
On the first line it works though, how would I be able to get it to work on the rest of them though?
Ill show you the output
1 2 3 4
num plan dia cel fah
output below
1 Mercury 4878 117.00 242.60
Last edited on Feb 14, 2015 at 6:09pm UTC
Feb 14, 2015 at 6:25pm UTC
u r so wise LB......as always :-/