Only returns first line on while loop

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
Why do you read into fah and then immediately overwrite its value?
What do u mean? ( u always make everything seem so simple lol)
22
23
24
while (planet>>num>>plan>>dia>>cel>>fah) 
    {
fah = cel*9/5+32;
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
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?
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
 
I moved my post up and didn't know u couldn't delete old ones :-/
Last edited on
Anyone else have an idea
closed account (D80DSL3A)
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.
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
1
2
while (planet>>num>>plan>>dia>>cel>>fah)  //bad
while (planet>>num>>plan>>dia>>cel) //good 
u r so wise LB......as always :-/
Topic archived. No new replies allowed.