While loop with variable problem

This is a discussion on While loop with variable problem within the C++ Programming forums, part of the General Programming Boards category; Doesn't matter -- you still need to know what it is -- not as a constant, obviously, but as a ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Doesn't matter -- you still need to know what it is -- not as a constant, obviously, but as a variable. You counted when you were loading them in, so you should know, right?

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by dwks View Post
    Well then, after you use a loop like this to initialize the array,
    Code:
        for (counter = 0; counter < listSize && inFile >> studentList[counter].Name
                                 >> studentList[counter].ID
                                 >> studentList[counter].score; counter++)
    take note of the value of counter after the loop has finished executing. studentList[counter-1] will be the last element (assuming some were read at all). If you pass counter to other functions, you can use it for the "(don't know what to put here)". Like so.
    Code:
    for(c = 0; c < counter; c ++)
    This worked but it prints out student [1], [3], [5], [7]....and so on until the end of the file. Why is this?

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Either: You put an extra c++ in the loop, or your read-in function is skipping lines.

  4. #19
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I think the initialize function is not initializing correctly because I used cout to print the counter variable and it amounted to 9 when it should be 18.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd88 View Post
    I think the initialize function is not initializing correctly because I used cout to print the counter variable and it amounted to 9 when it should be 18.
    Post your initializing loop...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Code:
    void initialize (ifstream& inFile, studentType list[], studentType studentList[],
                     int& counter, int listSize)
    {
    
        for (counter = 0; inFile >> studentList[counter].Name
                                 >> studentList[counter].ID
                                 >> studentList[counter].score; counter++)
        {
             inFile >> list[counter].Name >> list[counter].ID >> list[counter].score;
             }
             cout << counter;
    }

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    void initialize (ifstream& inFile, studentType list[], studentType studentList[],
                     int& counter, int listSize)
    {
    
        for (counter = 0; inFile >> studentList[counter].Name
                                 >> studentList[counter].ID
                                 >> studentList[counter].score; counter++)
        {
             inFile >> list[counter].Name >> list[counter].ID >> list[counter].score;
             }
             cout << counter;
    }
    What does the red, and the green, code do?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    The red code sets the boundary for the for loop to read until it reaches the end of the file.

    The green code states what is IN the file.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Todd88 View Post
    The red code sets the boundary for the for loop to read until it reaches the end of the file.

    The green code states what is IN the file.
    That may be what you want it to do. We're asking you to read the code so that you see what it actually does.

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Okay so we deleted the red code and just put inFile as the boundary and it worked

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd88 View Post
    Okay so we deleted the red code and just put inFile as the boundary and it worked
    Except, I think, you will find that you are now getting 1 more in counter than there are items in your file. Cf. your thread from yesterday, and the solution found there.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    For the printIt function I just did this:

    Code:
    void printIt (ifstream& inFile, studentType list[], int& counter)
    {
        int c;
         
        cout << setw(8) << "Name" << setw(15) << "ID" << setw(20) << "Score\n\n";
    
        for (c = 0; c < counter - 1; c++)
        {
             cout << setw(8) << list[c].Name <<  "\t";
             cout << setw(8) << list[c].ID << "\t";
             cout << setw(8) << list[c].score << "\t" << endl;
             }
    }

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, and I suppose instead of being careful when using a knife, you just use band-aid's a lot :-)

    The correct solution is to put the infile << ... stuff inside the condition of the loop, and NOT have it in the body of the for-loop, so that immediately when you detect that you can't read any more, you exit the loop.

    Alternatively, we could use your original loop construct, and then break inside the loop when you can't read any more:
    Code:
        for (counter = 0; counter < listSize; counter++)
       {
           if (!inFile >> studentList[counter].Name
                                 >> studentList[counter].ID
                                 >> studentList[counter].score)
           {
              break; 
           }
        }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Is it wrong to do it my way? Like will it be a bad way down the road after completing the other functions?

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd88 View Post
    Is it wrong to do it my way? Like will it be a bad way down the road after completing the other functions?
    I think it's bad, yes. It's probably not strictly wrong to read one too many and then deduct one - but if you MUST do it that way, then do it inside your read-function, not where you are using the resulting counter variable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weired problem in while loop
    By avisik in forum C Programming
    Replies: 14
    Last Post: 12-01-2008, 12:41 PM
  2. Problem with a loop
    By wiggsfly in forum C Programming
    Replies: 5
    Last Post: 11-30-2008, 11:45 AM
  3. Problem with for loop
    By irsmart in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2006, 04:26 PM
  4. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21