Thread: segmentation fault

  1. #1
    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
    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
    28,413
    The two threads have been merged.
    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

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, 03: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, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM