Thread: Why is this code causing the Arduino Mega 2560 to crash???

  1. #1
    Registered User
    Join Date
    Aug 2018
    Posts
    7

    Why is this code causing the Arduino Mega 2560 to crash???

    Code:
     static int DepositFunds(void){
      int i;
      double depositAmount;
      float currentBalance;
      char accountBalance[10];
      char amount[10];
    
    
        i = GetAccount();
    
    
        if(i <= arrayLength)
        {
          printf("Enter The Amount You Would Like to Deposit: \n");
          scanf("%s", &amount);
          depositAmount = atof(amount);
          currentBalance = userAccountBalance[i];
          userAccountBalance[i] = currentBalance + depositAmount;
          dtostrf(userAccountBalance[i],10,2,accountBalance);
          printf("********************************************** \n");
          printf("Credit Card Number: %s \n", userAccount[2][i]);
          printf("Deposit Amount: $%s \n", amount);
          printf("********************************************** \n");
          printf("New Account Balance: $%s \n", accountBalance);
          printf("********************************************** \n\n");
          return 0;
        }
        else
        {
          printf("Error!. Account Not Found.");
          return 1;
        }
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Why is this code causing the Arduino Mega 2560 to crash???
    Have you run the program with your debugger?

    Look at this snippet:
    Code:
          printf("Enter The Amount You Would Like to Deposit: \n");
          scanf("%s", &amount);
          depositAmount = atof(amount);
    First you don't need the ampersand when dealing with C-strings, the name of the variable is all you need.

    Second, but why are you using a C-string? Why not just get the value as a double?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,537
    What are the declarations of all those global arrays?

    > if(i <= arrayLength)
    Array subscripts start from 0, so the normal test for validity is < N, not <= N.

    Does the width you pass to dtostrf() specifically allow room for the \0 at the end?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What`s causing the console to crash when running this code?
    By Madis Aasalo in forum C Programming
    Replies: 3
    Last Post: 02-18-2017, 01:30 AM
  2. Is my code causing my system to crash?
    By camel-man in forum C Programming
    Replies: 1
    Last Post: 04-14-2011, 05:54 PM
  3. fscanf causing a crash
    By dougwilliams in forum C Programming
    Replies: 6
    Last Post: 11-18-2007, 04:52 PM
  4. what is causing this seemingly simple app to crash?
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 08:36 PM
  5. allegro causing a crash
    By kooma in forum Game Programming
    Replies: 5
    Last Post: 04-06-2002, 02:01 PM

Tags for this Thread