Thread: Reading and comparing in an array

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Reading and comparing in an array

    Ok here is my new thing for those of you who already looked in here. I am having a problem reading in the scores into array in the records in the array! I have an inputfile:
    Code:
    joe smith 23
    alice west 45
    bob MILLS 82
    Margaret North 5
    *
    
    45 182 210 43 170 301 -1
    82 150 97 -220 178 223 300 -1
    11 100 105 99 -1
    5 97 100 110 123 96 108 130 112 -1
    Well I have been able to read in the names and id numbers for the info before the first sentinel (*). Now i have to read in the id number (ex. 45) and see if its a number that corresponds to a person that is in my array already. If it is, i need to read in the rest of the scores until the second sentinel(-1) and then read in the next idnumber. If it is not an idnumber that is in my array already, i need to ignore it. Here is my code and my questions:
    Code:
    void readidnumbers(ifstream& inputfile, playerrecords league[], int count)
    {
      int count2;
      num;
      int scores;
    
      inputfile >> num;
      while(!inputfile.eof())
       {
         for (int i = 0; i < count; i++)
         {
          if (num == league[i].idnum)
           {
            inputfile >> scores;
            while (scores != SENTINEL2)
            {
             for(int j = 0; j < MAXGAMES; j++)
               inputfile >> scores;
            }
           }
          if (num != league[i].idnum)
            inputfile.ignore(100, '\n');
         }
       }
    Am i using the ignore function right? here is my struct:
    Code:
    struct playerrecords
    {
      string first, last;
      int idnum;
      int games[MAXGAMES];
      int gamecount;
      double gameave;
    };
    games[MAXGAMES] is where i want to store my scores at...so when i do inputfile >> scores- is it going in there? Also I will need to sort out the invalid scores as well (can only be between 0 and 300). Will that be easy for me once I get the scores read in? Thanks!!!
    Last edited by ammochck21; 12-06-2006 at 08:27 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think you are ignoring too early. Add a variable that starts off as false inside your while loop. Then, if the id matches an id in your league, you set the variable to true to indicate you found a match. Then after the for loop has checked all of the entries in league, if the variable is still false you know that no id matched and you can call ignore. You are calling ignore correctly (assuming there will never be more than 100 characters in a line. You might want to use 1000 just in case).

    To store the value in games[], you can check to see if the value you read into scores is the SENTINEL. If it is, then you break out of your for loop. If it is not, then you assign scores to league[i].games[j]. The while (scores != SENTINEL2) code doesn't do anything because the for loop inside will keep reading in scores. You need to check for (scores != SENTINEL2) inside the for loop.

    I would sort out invalid scores inside the for loop as well. So you need to check if the score is the SENTINEL2. If it is, break out of the for loop. Then check if the score is valid. If it is, add it to the array. Also, only if the score is valid should you increment j, because j is counting how many valid scores are being added to the games array. You can solve that by removing the j++ from the for loop and only calling it when you assign a score to the array.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Thanks for responding! Well inthe time I basically scrapped that function and started over. but i am in the process of trying to read in the scores to the games now. I dont have much but this is where its getting me. Here is my code:
    Code:
    void readidnumbers(ifstream& inputfile, playerrecords league[], int count)
    {
      int num;
      bool found = false;
      int loc;
    
      inputfile >> num;
      while (!inputfile.eof())
       {
         for (int i = 0; i < count; i++)
           if (league[i].idnum == num)
             inputfile >>
       }
    }
    i am getting stuck as to how to read in the scores into games. Do i need ANOTHER for loop in there for maxgames? So say I read in 45 and it matchs to league[o].idnum. Now I want to read in the scores to league[0].games [???]. That is what i dont understand. So now i need a for loop to read in the scores to games?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> So now i need a for loop to read in the scores to games?
    You need another loop, but it does not necessarily have to be a for loop. The first for loop loops through all the people in the league array. You need a second loop to loop while you read in all the scores. That loop needs to stop if you reach MAXGAMES, or if you read in the SENTINEL2. Once you determine that a score you read in is valid, you just assign it to the next open spot in league[i].games[]. You have to keep track of what the next open spot is in the league[i].games[] array.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    So would it be like this- as far as reading in the scores to the games spots?

    Code:
    inputfile >> num;
      while (!inputfile.eof())
       {
         for (int i = 0; i < count; i++)
           if (league[i].idnum == num)
             inputfile >> score;
             while(score != SENTNEL2)
              {
                if (score >= 0 && score <=300)
                  for (int i=0; i < MAXGAMES; i++)
                    inputfile >> league[i].games[i];

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Forget the code for a minute. Try to write out all the things you need to do in a step by step process. I'll start with what you had before:

    - Read in a number from the file.
    - While the end of file has not been reached:
    - - For each player in the league array:
    - - - Check to see if the number read in matches the id for that player.
    - - - If it matches then:
    - - - - Read in another number that will be a score.
    - - - -


    Now you finish. I already gave what I thought needed to be added in my previous post.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Unhappy

    I am just not getting this at all. I am sorry to be a pain- and Daved I totally see what you mean in your previous post- and I have wrriten it out- but i just cannot put it into code. This is what I have. I keep going through it and through it but something isnt right. At one point i was able to print out the last idnumber and the scores that read in- including the sentinel tho. So i just dont know why I cant get this. Am I even close?

    Code:
    void readidnumbers(ifstream& inputfile, playerrecords league[], int count)
    {
      bool found = true;
      int num;
      int score;
      int count2;
    
      inputfile >> num;
      while (!inputfile.eof())
       {
         for (int i = 0; i < count; i++)
          {
           if (league[i].idnum == num)
            {
             inputfile >> score;
             while (score != SENTINEL2 && (score >= 0 && score <= 300))
               {
                  for (int i= 1; i < MAXGAMES; i++)
                     inputfile >> league[i].games[i];
               }
            }
           else
             found = false;
          }
       inputfile >> num;
      }
    }
    So i run through it and i see in the first idnumber which is 45. While thats not the end of file, I run through the 13 players i have stored to see if it matches. Well it doesnt match league[0].idnum so then it is returned as flase- and goes back up to the for loop and counts to 1. Now i am at league[1].idnum. 45 does match so I get into the if statement. This is where its tricky for me....because now i read in the first score (182). While it is not SENTINEL2 (-1) and while it is between 0 and 300, (Dont i need to assign league[1].games[0] to the first score? I had this in but then it didnt make sense). Ok so now I a guess i am ignoring 182 here and saying if the scores are between 0 and 300 read them in to the next locations in games. I think there is some serious problems here but i just cannot put into code what I am trying to say! Im sorry I dont understand!

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You can do this however you'd like, but I really think you should forget the code and write out the pseudo-code that I started. If you have written that out, post that so we can help you with that. It is the flow of the program that you are not getting right. This is either because you are not approaching the problem correctly, or because you are not implementing it correctly. If we can verify that the pseudo-code is correct, then we can work on implementing it.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    well with some help from my professor- i think i was able to fix the code. I wrote it out but it still wasnt working right. Now i have it reading and storing in the games but my print out is a little off. here is my code

    Code:
    void readidnumbers(ifstream& inputfile, playerrecords league[], int count)
    {
      bool found = false;
      int num;
      int score;
      int gamecount;
    
      inputfile >> num;
      while (!inputfile.eof())
       {
         for (int i = 0; i < count; i++)
          {
           if (league[i].idnum == num)
            {
              found = true;
              gamecount =0;
             inputfile >> score;
             while (score != SENTINEL2)
               {
                if (score >= 0 && score <= 300)
                  league[i].games[gamecount] = score;
                  cout << league[i].games[gamecount] << " ";
                  gamecount++;
               inputfile >> score;
               }
            }
          }
           if (!found)
             inputfile.ignore(100, '\n');
    
      inputfile >> num;
      }
    }
    here is what its printing
    Code:
    182 210 43 170 1 150 97 -1076056036 178 223 300 97 100 110 123 96 108 130 112
    File reading in:
    Code:
    45 182 210 43 170 301 -1
    82 150 97 -220 178 223 300 -1
    11 100 105 99 -1
    5 97 100 110 123 96 108 130 112 -1
    Last edited by ammochck21; 12-07-2006 at 10:33 PM.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Sorry, I can't even claim to have skimmed this thread. But this...
    Code:
    while (!inputfile.eof())
    ...certainly stands out to me.

    Learn not to do this early, please.
    http://www.daniweb.com/techtalkforum...155265-18.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I wrote it out but it still wasnt working right.
    Are you maybe missing braces around your if block? You indented a whole block but didn't add braces.

    >> I can't even claim to have skimmed this thread.
    Dave_Sinkula, in this case the problems with eof not getting set don't occur because the read happens before the eof() check. I agree that the solutions presented are better, though, just because they check for read errors.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Daved
    >> I can't even claim to have skimmed this thread.
    Dave_Sinkula, in this case the problems with eof not getting set don't occur because the read happens before the eof() check. I agree that the solutions presented are better, though, just because they check for read errors.
    Yeah, it's just a bad habit I hate to see perpetuated.

    Check for only one of several possible errors after you've already continued on?
    Or merely check for complete, absolute, and total success with greater ease before continuing?

    Duh.[/soapbox]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Well because me (and our class) is new at C++ our professor wants us to use (!inputfile.eof()). When I get better at this I will learn better tricks! But thanks for the tips!

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    85

    Question

    can someone tell me why this isnt printing the invalid scores print statement. It is only printing this:
    Code:
    NAME      ID #
    Bob Mills        82
    GAMES BOWLED
    9211040
    
    NAME      ID #
    Margaret North         5
    GAMES BOWLED
    9211040
    
    NAME      ID #
    Joe Smith        23
    GAMES BOWLED
    9211040
    (I know the last number is wrong) but i want it to print this in between Margaret North and Joe Smith because that is the next number to be read in:
    Code:
    17 is an invalid ID number
    Here is the function:
    Code:
    void printinfo(ifstream& inputfile2, playerrecords league[], int count, int gamecount)
    {
        bool found = false;
        int num2;
    
        inputfile2 >> num2;
        while (!inputfile2.eof())
          {
            for (int i = 0; i < count; i++)
              {
                if (league[i].idnum == num2)
                  {
                    found = true;
                    cout << "NAME" << setw(10) << "ID #" << endl;
                    cout << league[i].first << " " << league[i].last << setw(10) << league[i].idnum << endl;
                    cout << "GAMES BOWLED" << endl;
                    cout << gamecount << endl;
                    cout << endl;
                  }
              }
            if (!found)
              cout << num2 << " is an invalid ID number." << endl;
         inputfile2 >> num2;
          }
    }
    thanks!

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you should set the bool found = false;
    inside the while loop before for loop

    Ps. and work on your alignement - the current hard to follow
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. comparing data in two files
    By nynicue in forum C Programming
    Replies: 25
    Last Post: 06-18-2009, 07:35 PM
  2. reading through an array
    By ammochck21 in forum C++ Programming
    Replies: 11
    Last Post: 11-29-2006, 07:02 AM
  3. Comapring values in an array.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 05-12-2006, 10:22 AM
  4. HELP! Reading from txt file and comparing to input
    By disco_dog in forum C Programming
    Replies: 7
    Last Post: 12-05-2004, 06:30 AM
  5. Comparing array elements
    By Tride in forum C Programming
    Replies: 8
    Last Post: 09-13-2003, 12:10 PM