Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; I am working on a program that takes input from the command line and then works out the math equation ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Segmentation Fault

    I am working on a program that takes input from the command line and then works out the math equation based on what the input was. I am using if, else if, else statments to handle the argument counts. However, if I run the program without putting input on the command line it gives me a "Segmentation Fault" error. I am wanting a custom error message that says "Input Error" which is what I am trying to use the else statement for. However, it won't display input error and only displays the segmentation fault instead.

    Any suggestions are appreaciated!

    Code:
    /*
    Jacob L. Jones
    Computer Programming II
    */
    
    #include <stdio.h>
    #include <math.h>
    
    int main(int argc, char *argv[]) {
    
      float num1;
      float num2;
      float ans;
      char choice;
    
      if (argc == 4){
          sscanf(argv[1], "%f", &num1);
          sscanf(argv[2], "%c", &choice);
          sscanf(argv[3], "%f", &num2);
    
          switch (choice){
          case '+':
          ans = num1 + num2;
          printf("%.2f + %.2f = %.2f\n", num1, num2, ans);
          break;
    
          case '-':
          ans = num1 - num2;
          printf("%.2f - %.2f = %.2f\n", num1, num2, ans);
          break;
    
          case 'x':
          case 'X':
          ans = num1 * num2;
          printf("%.2f x %.2f = %.2f\n", num1, num2, ans);
          break;
    
          case '/':
          if (num2 != 0){
          ans = num1 / num2;
          printf("%.2f / %.2f = %.2f\n", num1, num2, ans);
            }
          else{
            printf("Error! Divison by Zero!\n");
          }
          break;
    
          default:
          printf("%c is not a valid operator!\n", choice);
         
          break;
          }
      }
      else if (argc = 3){
          sscanf(argv[1], "%c", &choice);
          sscanf(argv[2], "%f", &num1);
    
    switch (choice){
          case 'n':
          case 'N':
          ans = -1 * num1;
          printf("-(%.2f) = %.2f\n", num1, ans);
          break;
    
          case 'a':
          case 'A':
          ans = sqrt (num1 * num1);
          printf("|%.2f| = %.2f\n", num1, ans);
          break;
    
          case 's':
          case 'S':
          if (num1 >= 0){
          ans = sqrt(num1);
          printf("Sqrt(%.2f) = %.2f\n", num1, ans);
          }
          else {
            printf("Can't find square root of negative number\n");
          }     
          break;
    
          default:
          printf("%c is not a valid operator!\n", choice);
    
          break;
     }
      }
      else {
        printf("Input Error!!!\n");
      }
    }

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    In line 54, you have a single = instead of a double ==
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    637
    (argc = 3) should be (argc == 3)

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Thanks! You're awesome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault
    By muppy in forum C Programming
    Replies: 10
    Last Post: 06-05-2011, 05:28 PM
  2. segmentation fault
    By xniinja in forum C Programming
    Replies: 5
    Last Post: 07-09-2010, 01:57 PM
  3. Segmentation fault
    By ankitsinghal_89 in forum C Programming
    Replies: 2
    Last Post: 06-28-2010, 01:45 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

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