Can't figure out a scanf("%s",name) segmentation fault

This is a discussion on Can't figure out a scanf("%s",name) segmentation fault within the C Programming forums, part of the General Programming Boards category; Hello i recieve a segmentation fault after input of the scanf, have tried switching it to fgets and that did ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    8

    Can't figure out a scanf("%s",name) segmentation fault

    Hello i recieve a segmentation fault after input of the scanf, have tried switching it to fgets and that did the same thing...heres the code,

    i attempted the tags if anyone can tell me where i went wrong ill gladly edit it for you

    Code:
    int main(){
      char name[20];
      char tempChar[20];
      int choice;
      int num,tempNum;
      FILE *cfPtr;
      instructions();
      scanf("%d",&choice);
      while(choice!=6){
        switch(choice){
        case 1:
          printf("Please enter the name of the item you would like to add\n");
          printf("?:");
          scanf("%s",name);/*<---Problematic scanf*/
          addItem(name);
          break;
    Hopefully you can help me if you need to see any more of my code let me know it is a 250 like file so i didn't see it necessary to put it all up here.

    -RC
    Last edited by laserlight; 07-08-2008 at 02:20 PM. Reason: Corrected code tags.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,885
    The closing code tag is [/code], not [code/]. I have made the correction for you.

    I suggest that you post the code where you switched to use fgets() since that is a more correct solution than scanf() for reading in a string. Try to make the code example the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Okay give me a minute im using putty to remote control a computer at my school from my home computer

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    apologies but my school computer system seems to have crashed thanks for your help though ill post my problem again later when i can access it

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,885
    You should download a compiler and test your program on your local computer. I suggest that you either download Code::Blocks or Microsoft Visual C++ 2008 Express. Both are free IDEs and come with reasonably standard compliant compilers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    8
    Thank you i'll try that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Replies: 7
    Last Post: 12-10-2004, 01:58 AM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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