Thread: Whats wrong with my array?

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

    Whats wrong with my array?

    Hey im back-

    Ok i have to store in some names and id numbers into arrays from an inputfile desired by the user, and then those arrays will be stored into a record. I started list(as an array of the names and idnumbers) and then league[] which has all the lists in it. Am i just doing this backward or am i missing something simple? Thanks.

    Code:
    void storenames(ifstream& inputfile, playerrecords& list, int& count)
    {
      count=0;
      inputfile >> list.first;
      while(list.first != SENTINEL)
        {
          inputfile >> list.last >> list.idnum;
          count++;
    
          inputfile >> list.first;
        }
      for (int i=0; i < count; i++)
      cout << list.first << list.last << list.idnum << count << endl;
    }
    I know count has to go in there somewhere- that is why I started with league[count].first but that didnt work either. Help!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    What's the format of the file?
    What is 'playerrecords'? What does it look like?

    >> Am i just doing this backward or am i missing something simple?
    What errors do you get?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    playerrecords is a struct:

    Code:
    struct playerrecords
    {
      string first, last;
      int idnum;
      int games[MAXGAMES];
      int gamecount;
      double gameave;
    };
    i am not getting any errors- just nothing is printing out.

    I am not sure if i need to do the inputfile >> firstname and stuff into league which is the array of the records. Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just a stab....
    Code:
      inputfile >> list.first;
      if (list.first == SENTINEL) std::cerr << "Can't get into read loop\n";
      while(list.first != SENTINEL)
    If that doesn't work, then this function just isn't getting called.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Well here is what is happening now- I have figured out how to read it in- its the SENTINEL thing thats getting me. Here is my new code:
    Code:
    void storenames(ifstream& inputfile, playerrecords league[], int& count)
    {
      count=0;
      inputfile >> league[count].first;
      while(league[count].first != SENTINEL)
        {
          inputfile >> league[count].last >> league[count].idnum;
          count++;
    
          inputfile >> league[count].first;
        }
      for (int i=0; i < count; i++)
        cout << league[i].first <<" " << league[i].last <<" " << league[i].idnum <<\
     endl;
    }
    League is my array and list is the record. Now if i put
    Code:
     while(!inputfile.eof())
    it will print everything in mmy data file- which is better then getting nothing- so the problem I am having it stopping at the sentinel. My data file looks like this:
    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
    I need the first read in to stop at the *. Thats where it is giving me probelms. And I cant do league[count].first != SENTINEL because it give me an error saying i cant do that. Any suggestions?

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ammochck21
    And I cant do league[count].first != SENTINEL because it give me an error saying i cant do that. Any suggestions?
    You shure can do it. Just depends how SENTINEL is declared.
    if its something like
    Code:
    const string SENTINEL = "*";
    It would work.
    Kurt

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

    Thank You!!!!

    Thank you sooooo much!!!!!!!!!! That worked- the problem was I had SENTINEL declared as
    Code:
    const char SENTINEL = '*'
    THANK YOU!!!!

    Also......why didnt what i had work? Isnt * a char?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but a char is not a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    I get it! You all are awesome! Thank you! (I am sure I will be back)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dont know what im doing wrong... array
    By jamort in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2009, 12:23 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM