segmentation fault

This is a discussion on segmentation fault within the C Programming forums, part of the General Programming Boards category; this seems weird to me... their seems to be something wrong with the while loop conditions.. the way it is ...

  1. #1
    kiz
    kiz is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    wats wrong with this

    this seems weird to me... their seems to be something wrong with the while loop conditions.. the way it is written now i wouldnt have though that the loop would even start because vname+viso+vaperture=0

    heres my code

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int vname=0, viso=0, vaperture=0, error=0, quit=0, iso;
    
    char selection[2], name[20], aperture[2];
    
    while ((strcmp(selection,"Q")!=0) || (vname + viso + vaperture != 0))
            {
            fprintf(output,"\n1. First name\n2. ISO setting\n3. Aperture setting\n\nPlease enter a menu number or '?' for help\n"
            );
            fscanf(input,"%s[2]",selection);
            if (strcmp(selection,"1")==0)
                    {
                    fprintf(output,"\nPlease enter your first name\n");
                    fscanf(input,"%s[20]",name);
                    error=1;
                    vname=1;
                    }
            else if (strcmp(selection,"2")==0)
                    {
                    fprintf(output,"\nAcceptable ISO settings include:\n\n6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100,"
                    " 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500, 3200, 4000, 5000 and 6400.\n\n"
                    "Please enter your ISO setting\n");
                    fscanf(input,"%d",iso);
                    }
            }
    }
    also i get a segmentation error when i try to input the iso in the else if part

  2. #2
    kiz
    kiz is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    segmentation fault

    how come i get a segmentation fault when i try to store the iso integer?? in the else if state ment

    Code:
    int main(int argc, char * argv[])
    {
     // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    int vname=0, viso=0, vaperture=0, error=0, iso=0;
    
    char selection[2], name[20], aperture[2];
    
    while (vname + viso + vaperture != 3 &&  strcmp(selection,"Q") != 0)
            {
            fprintf(output,"\n1. First name\n2. ISO setting\n3. Aperture setting\n\n"
            "Please enter a menu number or '?' for help\n");
            fscanf(input,"%s[2]",selection);
            if (strcmp(selection,"1")==0)
                    {
                    fprintf(output,"\nPlease enter your first name\n");
                    fscanf(input,"%s[20]",name);
                    error=1;
                    vname=1;
                    }
            else if (strcmp(selection,"2")==0)
                    {
                    fprintf(output,"\nAcceptable ISO settings include:\n\n6, 8, 10, 12, 16, 20, 25, 32, 40, 50, 64, 80, 100,"
                    " 125, 160, 200, 250, 320, 400, 500, 640, 800, 1000, 1250, 1600, 2000, 2500,3200, 4000, 5000 and 6400.\n\n"
                    "Please enter your ISO setting\n");
                    fscanf(input,"%d",iso);
                    }
            }
    }
    thanx

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    fscanf(input,"%d",&iso);

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    One thing that jumps out is that you use selection in the while condition before initializing it.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You should have modified the code in the other thread instead of starting a new one. See my comment there, which also applies to this version.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The two threads have been merged.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 02:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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