0

This is the code I have so far. I am getting my data from an input file. The file has all the data that I need for each variable that I am calling but when I run it I only get one iteration and it exits the loop after that. I need it to run till it reaches the end of the file.

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main ()
{
    ifstream pTemp("PlanetCelsius.dat");
    int pNumber, pSize;
    string pName;
    double cTemp, fTemp;

    cout << "Number\t" << "Planet Name\t" << "Diameter\t" << "Celsius\t" << "Fahrenheit" << endl;

        while (pTemp >> pNumber) 
        {
            while (pTemp >> pName) 
            {
                while (pTemp >> pSize) 
                {
                    while (pTemp >> cTemp) 
                    {
                        pTemp >> pNumber;
                        cout << pNumber << " ";
                    }
                pTemp >> pName;
            cout << pName << " ";
                }
        pTemp >> pSize;
        cout << pSize << " ";
            }
    pTemp >> cTemp;
    fTemp = 9 * (cTemp) / 5 + 32;
    cout << cTemp << " " << fTemp << endl;
        }

    system ("PAUSE");
        return 0;
}
  • 4
    I sugest you to find a more descriptive title to your question... most people around here (me included) ask questions about our own mistakes or bumps. – Barranka Mar 25 '13 at 22:23
  • Start debugging with cout << "Here" :) – Gaʀʀʏ Mar 25 '13 at 22:25
  • I suggest you to add the programming language you are using to the tags. I can clearly see it is C++ by going inside the post, but I think you could get more results and in a faster way if you added it. – Lucia Pasarin Mar 25 '13 at 22:27
  • Ok I tried to make the title more descriptive and added c++ and Visual-C++ to the tags. – Nathan Brown Mar 25 '13 at 22:38
  • 1
    Ask yourself one question: "*which* loop is running only once, and which ones run *multiple* times?" Think carefully about what you want to do and what your code is, actually, doing and whether you need *all* those `while` loops. And as a sidenote, please [don't use `system("pause")`](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) – Nik Bougalis Mar 25 '13 at 22:40
  • 1
    I see an arrowhead. [Do you see it?](http://www.codinghorror.com/blog/2006/01/flattening-arrow-code.html) – Cody Gray - on strike Mar 25 '13 at 22:44
  • well i need it to out put each part of the file as a separate variable then I have to use the cTemp to turn it to fTemp and output that then repeat the whole loop till it reaches the end of the file. – Nathan Brown Mar 25 '13 at 22:48
  • Ok i edited the post with some of the changes that I made after trying some things but i still am not getting the loop to work the way i want it to. – Nathan Brown Mar 25 '13 at 23:40

2 Answers2

1
while (cin.good)
{
    pTemp >> pNumber >> pName >> pSize >> cTemp >> pNumber;
    cout << pNumber << " " << pName << " " << pSize << " ";
    fTemp = 9 * (cTemp) / 5 + 32;
    cout << cTemp << " " << fTemp << endl;

}
return 0;

I haven`t seen your file structure, but I gues this code snippet will work.

spin_eight
  • 3,925
  • 10
  • 39
  • 61
  • thanks I ended up doing something like that but used while (pTemp >> pnumber) instead of cin.good and it seems to work the same. Thanks alot. – Nathan Brown Mar 26 '13 at 01:03
0

You're doing:

while(there's a number)
  while(there's a string)
    while(there's a number)
     while(there's a number)
     take first number
    take first string
  take first number
take first number

and your program fetches all the numbers it can in the inner-most while loop messing with your number-string-number-number sequence.

When the first while is checked for the second time, it cannot find a number because the inner-most loop already fetched every number till the first string and it exits.

Using while()s is a bad choice, try to parse the file as spin_eight suggested.

Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108