gpt4 book ai didi

c - 对 `scanf_s' 的 undefined reference

转载 作者:太空宇宙 更新时间:2023-11-04 04:59:54 27 4
gpt4 key购买 nike

我有一项类(class)作业需要快速完成,这要求我能够以某种方式调试代码。为了完成作业,我必须能够运行我得到的程序并使用断点逐步 Bootstrap 。我们得到的程序是 ATM 的基本 View ,存在一些错误。

请不要修复代码中的错误,但是有人可以告诉我我能做些什么来处理我收到的与 scanf_s 行相关的错误,因为我一直收到错误'undefined reference to 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;
}

最佳答案

其中之一:

  • 使用 Microsoft 编译器 scanf_s()已定义。
  • 使用ISO C90/C99标准库函数scanf()相反。
  • 使用支持可选 ISO C11 Annex K 库的编译器。
  • 添加#define scanf_s scanf

但是请注意,您传递给 scanf_s 的参数在给定格式说明符的情况下不正确 - 它们对于 scanf 是正确的 - 这可能会建议一个首选解决方案(但它不是最后一个 ;-))。

关于c - 对 `scanf_s' 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28032459/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com