Thread: help with infile and getline using for loop

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    10

    Thumbs down help with infile and getline using for loop

    I have to use a for loop to read file from a text file, now my problem is that it keeps overwriting the previous data it collected. does anyone know how i can use a for loop and store it as different cars.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if you have need for the stuff read in outside the loop then you need to store it in a container of some sort.

    Code:
    int array[10];
    ifstream fin("myFile.txt");
    int index = 0;
    fin >> array[index++];
    while(fin && index < 10)
    {
      fin >> array[index++];
    }
    for(int i = 0; i < index; i++)
    {
      cout << array[i] << endl;
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    we can't use an array.. the program is supposed to be a circular doubly linked list..

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Elad was just using an array as an example. Look carefully, he said:

    if you have need for the stuff read in outside the loop then you need to store it in a container of some sort.
    This means for your application you need to replace the example code he had to store the data in the array with whatever code you have to store the data into the linked list. It brings to mind... can we see your code? You haven't posted it and it would help a great deal. If the code is lengthy, please include it as an attachment. Otherwise, make sure to use the code tags.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    I'm at work so I can not telnet into my account at school. They put so many restrications on the computers, so i cant even click on start to try to telnet. is there a way i can get my file off the server without downloading anything new(they wont let me do that either)

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    ok i am able to telnet now... click here to go see the main prog. if you need me to post any of the include files let me know.

    here it is:
    http://sourcepost.sytes.net/source/s...source_id=1666

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    can anyone help me!!!!!????????

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    One technique I use in debugging is to comment out large portions of my program and focus on just an isolated section of the code. In your case I would comment out everything after:

    int total;
    infile>>total;
    int k=total;

    and put in the simple line:

    cout << k << endl;


    at that point. If you get the expected value for k appearign on the screen then you know you have opened the file and successfully read in the first value therein, and can shift focus elsewhere.


    Here's a possible elsewhere:

    when adding nodes to a list each new node should have it's own address. Each of the new addresses are obtained by a call to the new operator. In your code you only call new once, before the for loop with the following lines:

    car *c1_ptr;
    c1_ptr=new car;


    I would move the latter of the two lines into the for loop right after the opening { of the for loop and before any other code in the for loop so that each time through the for loop you get a new address for the new car object that can then be stored in the list. Otherwise you will just be overwriting the data in the car that is at the address pointed to be c1-ptr each time through the loop.

  9. #9
    Unregistered
    Guest
    then when i move it inside the for loop i get errors later on in the program (141) that say c1_ptr is undeclared because i declared it in the loop and it "died" within the loop. what can i do?

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    10
    that was me ^ there... i forgot to type in my username and password....

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    memory declared with new shouldn't go out of scope.

    Code:
      
    #include <fstream>
    
    int main()
    {
      //an array of 4 pointers to type int
      int * array[4];
      //a dummy holding pointer
      int * ptr;
    
      int i;
      for(i = 0; i < 4; i++)
      {
        //declare a new address for the ptr each time through the loop
        ptr = new int;
        //initialize the int to which we are pointing
        *ptr = i * 3;
        //store value in ptr in the approapriate element of the array
        array[i] = ptr;
       }
    
       //display the values pointed to by the pointers in the array
       for(i = 0; i < 4; i++)
      {
        cout << *array[i] << endl;
       }
       
       //now delete the dynamic memory
       for(i = 0; i < 4; i++)
       {
           delete array[i];
        }
    
      return 0;
    }
    this should work just fine and is equivalent to what you are trying to do, except using an array rather than a list. I'm at work too, and don't have a compiler to prove it though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 29
    Last Post: 11-01-2002, 04:46 PM