while loops

Discussion in 'C++' started by Angie, Jul 30, 2003.

  1. Angie

    Angie Guest

    I have a program that needs to read information from a file. This
    information contains a name, a difficulty level and 9 scores.

    The problem I am having is that my while loop isn't looping. It's only
    reading the first full line of the file. Any suggestions?

    Thanks in advance!
    Angie


    int main()
    {
    // declare variables
    int SIZE;
    double difficulty, score, total_score;
    string name;

    ifstream infile;
    infile.open("MP6dive.dat");
    if (!infile)
    {
    cout << "Trouble opening data file\n" ;
    return 1;
    }

    infile >> SIZE;

    infile >> name >> difficulty;
    total_score = 0;

    while(infile)
    {
    cout << name << setw(8) << difficulty << setw(7);

    int i;
    for(i=0; i <= JUDGES; i++)
    {
    infile >> score;
    cout << score << " ";
    total_score = score + total_score;
    }

    double final_score = total_score * difficulty;

    cout << final_score << endl;

    infile >> name >> difficulty;
    }

    infile.close();

    return 0;
    } // end main
    Angie, Jul 30, 2003
    #1
    1. Advertising

  2. "Angie" <> wrote...
    > I have a program that needs to read information from a file. This
    > information contains a name, a difficulty level and 9 scores.
    >
    > The problem I am having is that my while loop isn't looping. It's only
    > reading the first full line of the file. Any suggestions?


    Yes. Post the contents of the file along with the source.

    Victor
    Victor Bazarov, Jul 30, 2003
    #2
    1. Advertising

  3. Angie

    jwtroll05 Guest


    >It's only reading the first full line of the file.


    You only told it to read the first full line of the file.

    Instead of if(!infile)... try this:

    while(infile)
    {
    infile >> name >> difficulty;
    cout << name << difficulty;

    total_score = 0;
    for(int i = 0; i < 9; i++)
    {
    infile >> score;
    cout << score;
    total_score += score;
    }
    }

    --
    Posted via http://dbforums.com
    jwtroll05, Jul 30, 2003
    #3
  4. Angie

    jwtroll05 Guest

    Yeah...sorry. I viewed the original message in an internet newsgroup
    reader and it saw the < signs and the > signs as html tags or something
    and a lot of the code was missing. Ignore my previous post.

    --
    Posted via http://dbforums.com
    jwtroll05, Jul 31, 2003
    #4
  5. Angie

    Angie Guest

    Angie wrote:

    > The problem I am having is that my while loop isn't looping. It's only
    > reading the first full line of the file. Any suggestions?


    I figured it out and it was a simple error on my part. Thanks for looking
    over it for me!
    (Loops and I have a very strained relationship...)

    Angie
    Angie, Aug 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. for and while loops

    , Jun 28, 2006, in forum: Python
    Replies:
    6
    Views:
    386
    Bruno Desthuilliers
    Jun 29, 2006
  2. cman

    Testing pointers in while-loops

    cman, Mar 2, 2007, in forum: C Programming
    Replies:
    6
    Views:
    324
    Chris Thomasson
    Mar 3, 2007
  3. Zachary  Turner

    Variable declaration and while loops

    Zachary Turner, Jun 18, 2007, in forum: C++
    Replies:
    10
    Views:
    556
    Andre Kostur
    Jun 18, 2007
  4. zxo102
    Replies:
    2
    Views:
    243
    zxo102
    Jul 19, 2007
  5. Me
    Replies:
    2
    Views:
    237
Loading...

Share This Page