3

I've got a piece of coursework to get done quickly which requires me to be able to debug the code in a certain way. In order to complete the assignment I have to be able to run the program I've been given and use breakpoints to guide the program step by step. The program we have been given is a basic view of an ATM and has a number of errors.

Please don't fix the errors in the code, but can someone please tell me what I can do about the errors I'm receiving in relation to the scanf_s lines as I keep getting the error 'undefined reference to scanf_s' The code is as follows:

/* This program has been altered purposefully
   so that it contains problems.
   Use the program to complete P2-Debugging a program
   Remember to take screenshots of you do the following:

   Adding a breakpoint at an appropriate point
   Stepping into functions (F11) and over each line of code (F10)
   Changing variables and altering other code, e.g. changing messages
   Watching the values of variables.
   Add comments to the code before taking screenshots.
   Fix the problems if you can. Otherwise, just add comments to the code
   indicating where you think the problems are and what the solution might be.
   Place all evidence into one Word Document and submit it.
   Can you add other improvements?
*/
#include <stdio.h>

int getOption()
{
    int option = 0, nl;
    printf("\nWelcome to the ATM\n");

    printf("\nMenu\n");
    printf("\n1. Withdraw Cash\n");
    printf("\n2. Show Balance\n");
    printf("\n3. Exit\n");
    printf("\nEnter a number from 1 to 3:");
    option = scanf_s("%d%c", &option, &nl);

    return option;
}

//function to allow you to withdraw cash
int withdrawCash()
{
    float amount;
    int nl, option;

    printf("\nHow much money do you want?");
    amount = scanf_s("%d%c", &option, &nl);
    return option;
}

//function to show you your balance
int getBalance()
{
    float balance = 10000;
    int nl, option;

    printf("\nHow much money do you want?");
    balance = scanf_s("%d%c", &option, &nl);
    return balance;
}

//function to update your balance
int updateBalance(float balance, float amount)
{
    int nl, option;
    balance = balance - amount;
    return balance;
}


// main function - start here
int main(void)
{
    int ch;
    int opt = 0;
    int amount = 0;
    int balance = 0;
    float newbal = 0.0;

    opt = getOption();
    printf("\nYou chose option %d\n", opt);
    if (opt == 1)
    {
        amount = withdrawCash();
        newbal = updateBalance(10000, amount);
        printf("\nHere is your %d, your balance is:\n", amount, newbal);
    }
    if (opt == 2)
    {
        balance = getBalance();
        printf("\nHere is your balance: %d\n", balance);
    }

    printf("\nThank you. Please take your card.\n");
    ch = getchar();

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Josh Hitchcock
  • 65
  • 1
  • 1
  • 6
  • 1
    What platform are you on? `scanf_s` is a Windows function. – mafso Jan 19 '15 at 19:51
  • 1
    I assume this should be fixed by yourself to pass your course. – Einar Sundgren Jan 19 '15 at 19:53
  • 4
    Note that `scanf_s()` is a function that's in the optional Annex K of the C11 standard. The only platform that has implemented an approximation to the standard functions is Microsoft (and the approximation is not all that good — see [Do you use the TR 24731 safe functions?](http://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions)). – Jonathan Leffler Jan 19 '15 at 19:59
  • Is there an alternative function I can use in Eclipse with MinGW and GCC? – Josh Hitchcock Jan 19 '15 at 20:01
  • 1
    You can use `scanf()`, but you'll need to review the calling sequence for differences (though actually, in the context, I don't think there are any differences). And you will probably have to avoid 'fessing up to not using the Microsoft C library. – Jonathan Leffler Jan 19 '15 at 20:02
  • Replacing the `scanf_s` calls with `scanf` at least will make the code compile and link (as far as I see, I haven't tried) and leave enough to correct and improve (and as a hint: No correction of the code with `scanf` will be wrong for the `scanf_s` version). It looks quite unlikely that the linker error is the thing you're supposed to solve. After this is done (what you can do with MinGW), you can read up on Microsoft's `scanf_s` and fix the remaining issues. – mafso Jan 19 '15 at 20:24

2 Answers2

1

One of either:

  • Use a Microsoft compiler for which scanf_s() is defined.
  • Use the ISO C90/C99 standard library function scanf() instead.
  • Use a compiler with the optional ISO C11 Annex K library support.
  • Add #define scanf_s scanf.

Note however that the arguments you are passing to scanf_s not correct given the format specifiers - they are correct for scanf - that may suggest a preferred solution (and it's not the last one ;-) ).

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 3
    Using `#define scanf_s scanf` would create problems when a `"%s"`, `"%c"`, `"%["` format specifier is used as the number of arguments needed changes. – chux - Reinstate Monica Jan 19 '15 at 22:52
  • @chux : That is true, except that in the code in the question, the necessary additional arguments are not supplied where `%c` has been used, so it looks like `scanf` was intended in any case. The last suggestion was not a very a serious suggestion. – Clifford Jan 20 '15 at 19:39
0

Code like has a linker problem and is using scanf_s() incorrectly.

scanf_s() expects 2 arguments with each "%s", "%[" and "%c": a char * address and rsize_t size.

// scanf_s("%d%c", &option, &nl);
scanf_s("%d%c", &option, &nl,  (rsize_t) 1);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256