3

私は、特定の方法でコードをデバッグできるようにする必要がある、すぐに終わらせなければならないコースワークを持っています。課題を完了するには、与えられたプログラムを実行し、ブレークポイントを使用してプログラムを段階的にガイドできる必要があります。与えられたプログラムは、ATM の基本的なビューであり、多くのエラーがあります。

コード内のエラーを修正しないでください。しかし、「 undefined reference toscanf_s 」というエラーが発生し続けるため、行に関して受信しているエラーについて何ができるか教えてください。コードは次のとおりです。scanf_s

/* 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;
}
4

2 に答える 2

0

次のいずれか:

  • scanf_s()が定義されている Microsoft コンパイラを使用します。
  • scanf()代わりに、ISO C90/C99 標準ライブラリ関数を使用してください。
  • オプションの ISO C11 Annex K ライブラリ サポートを備えたコンパイラを使用します。
  • を追加し#define scanf_s scanfます。

ただしscanf_s、フォーマット指定子が指定されている場合、渡す引数は正しくないことに注意してください-それらは正しいですscanf-それは好ましい解決策を示唆している可能性があります(そしてそれは最後のものではありません;-))。

于 2015-01-19T20:04:42.487 に答える
0

次のようなコードにはリンカーの問題があり、scanf_s()正しく使用されていません。

scanf_s()それぞれ"%s","%[""%c":char *アドレスとrsize_tサイズの 2 つの引数が必要です。

// scanf_s("%d%c", &option, &nl);
scanf_s("%d%c", &option, &nl,  (rsize_t) 1);
于 2015-01-19T22:48:32.703 に答える