DarkMantis
DarkMantis

Reputation: 1516

Attempting CodeChef Challenge "ATM" - Getting SIGSEGV runtime error

I have currently just started learning C (as PHP and related languages are more my field), so I thought I'd start off with a challenge.

I decided to go onto CodeChef and try one of their easy practice challenges, but apparently my code gets a SIGSEGV error on runtime.

Could you please check my code to see why I am getting this error, the code runs fine on multiple boxes which I've tried it on (Mac OSX, Linux (CentOS and Ubuntu)).

Here is the code:

#include <stdio.h>
#include <stdlib.h>

#ifndef BANK_CHARGE
  #define BANK_CHARGE  0.5
#endif;

int main (int argc, char **argv){
  // if(argc != 3){
  //   printf("Usage: %s [int] [float]\n", argv[0]);
  //   exit(1);
  // }

  int withdraw = atoi(argv[1]);
  float balance = atof(argv[2]);

  if((withdraw % 5) != 0){
    printf("%.2f", balance);
    exit(1);
  }
  if(withdraw > 2000 || withdraw == 0){
    printf("%.2f", balance);
    exit(1);
  }

  float totalWithdraw = (withdraw + BANK_CHARGE);

  if(totalWithdraw >= balance){
    printf("%.2f", balance);
    exit(1);
  }

  printf("%.2f", balance - totalWithdraw);
  return 0;
}

The challenge is located here: http://www.codechef.com/problems/HS08TEST

But here is the problem summary:

All submissions for this problem are available.

Pooja would like to withdraw X $US from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal the bank charges 0.50 $US.

Calculate Pooja's account balance after an attempted transaction. Input

Positive integer 0 < X <= 2000 - the amount of cash which Pooja wishes to withdraw.

Nonnegative number 0<= Y <= 2000 with two digits of precision - Pooja's initial account balance. Output

Output the account balance after the attempted transaction, given as a number with two digits of precision. If there is not enough money in the account to complete the transaction, output the current bank balance. Example - Successful Transaction

Input: 30 120.00

Output: 89.50

Example - Incorrect Withdrawal Amount (not multiple of 5)

Input: 42 120.00

Output: 120.00

Example - Insufficient Funds

Input: 300 120.00

Output: 120.00

Upvotes: 0

Views: 3397

Answers (1)

PP.
PP.

Reputation: 10865

SIGSEGV - segment fault. This implies that a part of your program is actually attempting to read from or write to a memory address for which your program has no permission.

Something as simple as running the program with no command arguments may cause this operating system error as there are no guarantees that argv[1] or argv[2] are valid memory locations you are permitted to read from. More likely the random numbers contained by argv[1] and argv[2] are almost certainly not valid locations atoi or atof may read from. Unsure why you commented out the check for valid number of arguments because this would protect your program from committing this sin.

I presume you are running on a Unix-based operating system such as Linux to be getting a SIGSEGV in the first place. Thus I recommend you run your program within GNU debugger (gdb) which would also show you the line of code that caused this error. Using a debugger is a great learning technique and will have you solving problems before you take them to the wider community. (hint, hint).

Upvotes: 1

Related Questions