A problem with my linked lists test.

This is a discussion on A problem with my linked lists test. within the C Programming forums, part of the General Programming Boards category; It would be nice if someone could help with this problem: I run this with a file "numb.num" in my ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Exclamation A problem with my linked lists test.

    It would be nice if someone could help with this problem:
    I run this with a file "numb.num" in my E:\ directory that contains:
    Code:
    123456789
    Here's the program:
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <windows.h>
    #include <dos.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    struct node {
     int x;
     node *next;
    };
    
    void main() {
     ifstream file1("E:\numb.num");
     int total, a[700], n;
     node *root;
     node *p;
     root->next = NULL;
     for (n = 0;file1.eof();n++) {
      file1 >> a[n];
     }
     for (n = 0;file1.eof();n++) {
      root->x = a[n];
      root->next = new node;
     }
     root->next = NULL;
     p = root;
     for (n = 0;n == NULL || p->next == NULL;n++) {
      total += p->x;
      cout << p->x << " ";
      p = p->next;
     }
     cout << endl << "The total is: " << total << endl;
     p->next = NULL;
     file1.close();
    }
    Windows crashes the program. And that's about it. I use Visual C++ 6.0 on Windows 98.
    -Save the whales. Collect the whole set.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    11

    memory Problems

    Hmmm saw your code

    There is a basic problem in the code.

    ifstream file1("E:\numb.num");

    I think this line shud be changed to

    ifstream file1("E:\\numb.num");
    Also in this code

    node *root;
    node *p;
    root->next = NULL;
    you r assigning NULL to root -> next which is pointing to nowhere. Hence this will give error.

    so first do node *root = new node;
    and then you will have to do this also
    root -> next = new node;
    All this you will have to do each time you want to make new node hence you have to change the code everywhere also do it for 'p'

    AND ENJOY

    Be HAPPY

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,418
    Next time

    1. Remove unnecessary header files - you limit the number of people who can help you if you include compiler specific header files.

    #include <windows.h>
    #include <dos.h>
    #include <conio.h>

    2. remember that main returns int

    3. remember that this is the C board, and you posted C++ code.

    > for (n = 0;file1.eof();n++) {
    Mmm - ok, it might work

    > for (n = 0;file1.eof();n++) {
    The condition for stopping the second loop is the same as the one which stopped the first loop, so the 2nd loop will never run.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    your data will also be read a 123456789 becuase there is no delimeter such as a carriage return I'd change the numb.num to be
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10 //you need to test your input for more than one digit
    100
    1000

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C-string problem in linked lists
    By stewie griffin in forum C Programming
    Replies: 6
    Last Post: 01-10-2009, 08:10 AM
  2. linked lists inserting problem
    By sara.stanley in forum C Programming
    Replies: 5
    Last Post: 04-02-2006, 12:26 AM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. One more question about linked lists?
    By shaeng in forum C Programming
    Replies: 2
    Last Post: 06-24-2003, 07:36 AM
  5. Linked List problem
    By spec85 in forum C Programming
    Replies: 1
    Last Post: 06-14-2002, 03:58 AM

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