New to programming, Segmentation Fault?

This is a discussion on New to programming, Segmentation Fault? within the C Programming forums, part of the General Programming Boards category; I've been trying to look elsewhere online, but the code I see is all very complicated compared to my knowledge ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    New to programming, Segmentation Fault?

    I've been trying to look elsewhere online, but the code I see is all very complicated compared to my knowledge level. I'm trying to create a program that does exactly this and I keep getting a "Segmentation fault" error after it asks for the two integers.

    Please enter two integers: -1 3

    Description Data
    ----------- ----
    Input 1 -1
    Input 2 3
    Sum 2
    Half of Input 1 -0.5
    Half of Input 2 1.5
    Quotient 0


    Here's my code as it is currently, I know it's messed up, but I literally started last week and my teacher isn't helping. What all is causing this segmentation fault?

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    // create a and b variables aka local declarations
       int a;
       int b;
       int Sum;
       int Half_of_Input_1;
       int Half_of_Input_2;
       int Quotient;
    
    // statements
       printf("\nName: Herp Derp\n");
    
       printf("\nPlease enter two integers:\n");
       scanf("%d %d, &a, &b");
       scanf("%d %d %d %d, &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient");
    //inputting of data
    
    Sum = a + b;
    Half_of_Input_1 = a / 2;
    Half_of_Input_2 = b / 2;
    Quotient = a / b;
    
    // chart data
       printf("%20c%20c", "Description Data");
       printf("%20c%20c", "___________ ____");
       printf("%20c%20d", "Input 1", a);
       printf("%20c%20d", "Input 2", b);
       printf("%20c%20d", "Sum", Sum);
       printf("%20c%19.1f", "Half of Input 1", Half_of_Input_1);
       printf("%20c%19.1f", "Half of Input 2", Half_of_Input_2);
       printf("%20c%20d", "Quotient", Quotient);
    
    return 0;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Compiling with warnings turned up:
    Code:
    new.c:17: warning: too few arguments for format
    new.c:18: warning: too few arguments for format
    new.c:27: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:27: warning: too few arguments for format
    new.c:28: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:28: warning: too few arguments for format
    new.c:29: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:30: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:31: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:32: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:32: warning: format ‘%19.1f’ expects type ‘double’, but argument 3 has type ‘int’
    new.c:33: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    new.c:33: warning: format ‘%19.1f’ expects type ‘double’, but argument 3 has type ‘int’
    new.c:34: warning: format ‘%20c’ expects type ‘int’, but argument 2 has type ‘char *’
    scanf is used like this:
    Code:
    scanf("%d", &intVar);
    Last edited by rags_to_riches; 09-13-2010 at 11:54 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Green -> correct
    Red -> wrong

    Quote Originally Posted by dc210 View Post
    Code:
    #include <stdio.h>
    int main (void){
    // create a and b variables aka local declarations
       int a;
       int b;
       int Sum;
       int Half_of_Input_1;
       int Half_of_Input_2;
       int Quotient;
    
    // statements
       printf("\nName: Herp Derp\n");
    
       printf("\nPlease enter two integers:\n");
       scanf("%d %d", &a, &b");
       scanf("%d %d %d %d", &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient");
    //inputting of data
    
    Sum = a + b;
    Half_of_Input_1 = a / 2;
    Half_of_Input_2 = b / 2;
    Quotient = a / b;
    
    // chart data
       printf("%20c%20c", "Description Data");
       printf("%20c%20c", "___________ ____");
       printf("%20c%20d", "Input 1", a);
       printf("%20c%20d", "Input 2", b);
       printf("%20c%20d", "Sum", Sum);
       printf("%20c%19.1f", "Half of Input 1", Half_of_Input_1);
       printf("%20c%19.1f", "Half of Input 2", Half_of_Input_2);
       printf("%20c%20d", "Quotient", Quotient);
    
    return 0;
    }
    try this:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    // create a and b variables aka local declarations
       int a;
       int b;
       int Sum;
       int Half_of_Input_1;
       int Half_of_Input_2;
       int Quotient;
    
    // statements
       printf("\nName: Herp Derp\n");
    
       printf("\nPlease enter two integers:\n");
       scanf("%d %d", &a, &b);
       scanf("%d %d %d %d", &Sum, &Half_of_Input_1, &Half_of_Input_2, &Quotient);
    //inputting of data
    
    Sum = a + b;
    Half_of_Input_1 = a / 2;
    Half_of_Input_2 = b / 2;
    Quotient = a / b;
    
    // chart data
       printf("%20s", "Description Data");
       printf("%20s", "___________ ____");
       printf("%20s %20d", "Input 1", a);
       printf("%20s %20d", "Input 2", b);
       printf("%20s %20d", "Sum", Sum);
       printf("%20s %19.1d", "Half of Input 1", Half_of_Input_1);
       printf("%20s %19.1d", "Half of Input 2", Half_of_Input_2);
       printf("%20s %20d", "Quotient", Quotient);
    
    return 0;
    }
    Last edited by brack; 09-13-2010 at 01:09 PM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Thank you very much for the help guys. It's running properly, but it's misaligned a lot. I don't really know how to do field width properly yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 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