Thread: For loop not running properly

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    I used this code and it worked correctly:

    Code:
    for (counter = 1; inFile >> studentList[counter].Name
                    >> studentList[counter].ID
                    >> studentList[counter].score; counter++)
    
                    {
             cout << setw(8) << studentList[counter].Name <<  "\t";
             cout << setw(8) << studentList[counter].ID << "\t";
             cout << setw(8) << studentList[counter].score << "\t" << endl;
             }

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd88 View Post
    I think it is printed out at the end. That is why there are random numbers there?
    Well, the random numbers are there because you are trying to read ONE MORE TIME when you can't actuall read any more data. And since the read fails, the data for that student never gets filled in, and you get "rubbish" from whatever the variable happens to contain.

    --
    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.

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Before I look further into this, would this be an optimal way to make a program that asks a user to input a P for print, C for change, D for delete and A for add for the array that is in the file p4_data1.txt? If not, I basically wasted all this time for nothing Sorry for all the questions.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd88 View Post
    Before I look further into this, would this be an optimal way to make a program that asks a user to input a P for print, C for change, D for delete and A for add for the array that is in the file p4_data1.txt? If not, I basically wasted all this time for nothing Sorry for all the questions.
    Seems like a good starting point to be able to read the data in, and I presume from your previous post that this is working now?

    By the way, is your code outputing Alex now? If so, what's in student[0]?

    --
    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.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by matsp View Post
    Seems like a good starting point to be able to read the data in, and I presume from your previous post that this is working now?

    By the way, is your code outputing Alex now? If so, what's in student[0]?

    --
    Mats
    It is outputting Alex now so I assume Alex is student[0].

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are outputting Alex as part of the for-loop you posted above, then that would probably mean that Alex is in position 1, actually.

    --
    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.

  7. #22
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Thank you very much for your help and if I have any more questions I'll be sure to ask because you sure seem like you know your C++!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-09-2008, 11:09 AM
  2. Check number of times a process is running
    By linuxwolf in forum Windows Programming
    Replies: 6
    Last Post: 10-17-2008, 11:08 AM
  3. Monitor a running instance of MS Word
    By BobS0327 in forum C# Programming
    Replies: 0
    Last Post: 07-18-2008, 12:40 PM
  4. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 10:59 PM
  5. plz hlp me. program not running properly
    By jfl in forum C Programming
    Replies: 5
    Last Post: 02-11-2002, 03:58 PM