Thread: getline to store values in an array

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    3

    Unhappy getline to store values in an array

    Hi. I'm trying to store values read from a file into an array, and then manipulate them as arrays of doubles. I have problems storing the values, as the only I get is the last value read. I would also like to know how to manipulate the char* array to work numerically with its elements.

    This is my code...

    int count=0;
    char buffer[MAX_SAMPLES];
    char *array[MAX_SAMPLES];

    while(!file1.eof())
    {
    file1.getline(buffer,20);
    array[count]=buffer;
    cout<<array[count]<<"\n";
    }

    for(count=0;count<MAX_SAMPLES;count++)
    cout<<array[count]<<"\n"; //here i get just the last value read




    I will do this with 4 different files, and then i need to compare the values on each of them to obtain the maximum... the code is:




    for(int i=0; i<MAX_SAMPLES; i++)
    {
    max1 = (array1[i] > array2[i] ?
    array1[i]:carray2[i]);

    max2 = (array3[i] > array4[i] ?
    array3[i]:array4[i]);

    max = (max1 > max2 ? max1:max2);

    newArray[i] = max;
    }

    And to make the comparison, the elements have to be numbers!!!


    Please help me... I know you can.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I have problems storing the values, as the only I get is the last value read.
    You don't increment count anywhere in the loop, so the first element of the array will always be the last item read.

    >And to make the comparison, the elements have to be numbers!!!
    So read them in as numbers into an array of doubles, or read them as strings, validate, and then place them in an array of doubles.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Yes, sorry, i sent you the wrong version. this is the programme...



    int count=0;
    char buffer[MAX_SAMPLES];
    char *array[MAX_SAMPLES];



    while(!file1.eof())
    {
    file1.getline(buffer,20);
    array[count]=buffer;
    //cout<<array[count]<<"\n";
    count++;

    }

    for(count=0;count<MAX_SAMPLES;count++)
    cout<<array[count]<<"\n";


    I still get the last value when i print it.

    if i try to change to double array[MAX_SAMPLES] i get an error message that cannot convert from chars to doubles.

    what about that validation you mention?

    tnx.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one way.
    Code:
       int count=0;
       int i;
       double array[MAX_SAMPLES];
    
       while(file1 >> array[count])
          count++;
    
       for(i=0; i<count; i++) 
          cout << array[i] << endl;
       file1.close();

  5. #5
    Unregistered
    Guest
    ....
    array[count]=buffer;
    ....

    everytime you just save the address of the buffer to all the array[], so all of the array[] have the same address.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    3
    Hey!! thanks swoopy, it worked nice!! I thought I had to use getline or fgets, but it seems to be easier this way, and also the problem of the doubles is solved.

    Really, thank you...

    I owe you one...

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Glad to help. Just a note, if you did want to use getline (perhaps to make sure your file has valid numbers), you could use atof() or strtod() to convert the character array to a double.
    The following code assumes one double per line.
    Code:
       int count=0;
       int i;
       char buffer[80];
       double array[MAX_SAMPLES];
    
       file1.getline(buffer,80);
       while(!file1.eof())
       {
          array[count++] = atof(buffer);
          file1.getline(buffer,80);
       }
    
       for(i=0; i<count; i++) 
          cout << array[i] << endl;
       file1.close();
    Last edited by swoopy; 07-10-2002 at 10:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Eliminating duplicate values from a 20x2 array
    By desipunjabi in forum C Programming
    Replies: 2
    Last Post: 10-29-2005, 09:11 PM
  3. loop to store values
    By keendoc in forum C++ Programming
    Replies: 1
    Last Post: 10-28-2005, 02:43 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM