Whats wrong with my array?

This is a discussion on Whats wrong with my array? within the C++ Programming forums, part of the General Programming Boards category; Hey im back- Ok i have to store in some names and id numbers into arrays from an inputfile desired ...

  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
    ZuK
    ZuK is offline
    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
    21,941
    Yes, but a char is not a string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:01 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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