簡體   English   中英

對 `scanf_s' 的未定義引用

[英]undefined reference to `scanf_s'

我有一個課程作業需要快速完成,這要求我能夠以某種方式調試代碼。 為了完成任務,我必須能夠運行我得到的程序並使用斷點逐步引導程序。 我們給出的程序是 ATM 的基本視圖,並且有許多錯誤。

請不要修復代碼中的錯誤,但有人可以告訴我我可以對我收到的與scanf_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;
}

其中之一:

  • 使用定義了scanf_s()的 Microsoft 編譯器。
  • 請改用 ISO C90/C99 標准庫函數scanf()
  • 使用帶有可選 ISO C11 Annex K 庫支持的編譯器。
  • 添加#define scanf_s scanf

但是請注意,鑒於格式說明符,您傳遞給scanf_s的參數不正確 - 它們對於scanf是正確的 - 這可能會建議首選解決方案(並且它不是最后一個 ;-) )。

類似的代碼存在鏈接器問題,並且錯誤地使用了scanf_s()

scanf_s()需要 2 個參數,每個"%s""%[""%c" :一個char *地址和rsize_t大小。

// scanf_s("%d%c", &option, &nl);
scanf_s("%d%c", &option, &nl,  (rsize_t) 1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM