help me before i go insane!!

This is a discussion on help me before i go insane!! within the C Programming forums, part of the General Programming Boards category; sorry about that must have pressed enter or something, my message was posted before i'd finished! here's the code i ...

  1. #1
    andy_d
    Guest

    help me before i go insane!!

    sorry about that must have pressed enter or something, my message was posted before i'd finished! here's the code i need help with

    main()
    {
    BSTreeADT t;
    char buffer[15];

    t = createBSTree();

    /*this is an example of manual input into the program
    t = insertItem(createItem("apples"), t);

    however I want the "apples" string to be entered by the user at the following prompt
    */

    while (buffer != "****last")
    {
    printf("Enter first word:");
    scanf("%s", buffer);

    t = insertItem(createItem(buffer), t);
    }

    }

    please, please help I keep getting a segmentation fault and I am in danger of hurting my computer!

    andy d

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Fortunately, I posted a reply to a similar post earlier, so I don't have to retype it. The advice is still good.
    http://www.cprogramming.com/cboard/s...threadid=13151

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

  3. #3
    andy_d
    Guest
    The thing is that when i enter data as decribed within the comments (see code). I receive no errors and the programs run perfectly.

    My problem must be something to do with the way in which I am expressing the input variable string. I was hoping that you would spot an easy mistake. I understand the meaning of a segmentation fault but in this case do not believe it to be the cause.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Okay then, let's see if we can't find the problem. First you want to make sure that your buffer is large enough and that the user can't overflow it
    scanf("%s", buffer);
    should be
    fgets ( buffer, sizeof buffer, stdin );

    This way you can cover your boundaries. Next you want to make sure that there is something besides garbage originally in your buffer just to be thorough, anytime you use a while or for loop with an uninitialized buffer as the condition you're asking for trouble.
    char buffer[15];
    should be
    char buffer[15] = {0};
    or
    char buffer[15];
    memset ( buffer, '\0', 15 );

    Or you can change your while loop to a do..while, but those tend to be harder to follow than while loops.

    I get the feeling that these two changes will solve your segmentation fault since with manual input the program seems to work fine, but if it doesn't I'll need to take a look at your createItem and insertItem functions. If we're lucky then it will be something simple like this

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insane MIdget Assassins and Abachler
    By abachler in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-03-2009, 11:10 AM
  2. MSXML driving me insane!
    By bling in forum Windows Programming
    Replies: 4
    Last Post: 09-30-2008, 12:23 PM
  3. OMG I'm Going Insane (insertion function...help!)
    By spinner in forum C Programming
    Replies: 2
    Last Post: 05-04-2003, 02:22 PM
  4. Need Help. Going Insane
    By spidereen in forum Windows Programming
    Replies: 0
    Last Post: 03-26-2003, 12:45 PM
  5. need help quickly, going insane with this
    By Mkk84 in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2002, 09:58 AM

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