Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对“scanf#u s'”的未定义引用;_C_Scanf_C11_Tr24731 - Fatal编程技术网

对“scanf#u s'”的未定义引用;

对“scanf#u s'”的未定义引用;,c,scanf,c11,tr24731,C,Scanf,C11,Tr24731,我有一个课程作业要快速完成,这要求我能够以某种方式调试代码。为了完成作业,我必须能够运行给定的程序,并使用断点一步一步地指导程序。我们得到的程序是ATM的基本视图,并且有许多错误 请不要修复代码中的错误,但有人能告诉我,当我不断收到错误“未定义的引用scanf\u s”时,我收到的与scanf\u s行相关的错误,我能做些什么吗?代码如下: /* This program has been altered purposefully so that it contains problems.

我有一个课程作业要快速完成,这要求我能够以某种方式调试代码。为了完成作业,我必须能够运行给定的程序,并使用断点一步一步地指导程序。我们得到的程序是ATM的基本视图,并且有许多错误

请不要修复代码中的错误,但有人能告诉我,当我不断收到错误“未定义的引用
scanf\u s
”时,我收到的与
scanf\u 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;
}
/*此程序已被故意更改
这样它就包含了问题。
使用该程序完成对程序的P2调试
请记住在执行以下操作时拍摄屏幕截图:
在适当的点添加断点
进入函数(F11)并越过每行代码(F10)
更改变量和更改其他代码,例如更改消息
观察变量的值。
在截图之前向代码添加注释。
如果可以,请解决这些问题。否则,只需在代码中添加注释即可
指出您认为的问题所在以及解决方案。
将所有证据放在一个Word文档中并提交。
你能添加其他改进吗?
*/
#包括
int getOption()
{
int选项=0,nl;
printf(“\n请到自动取款机\n”);
printf(“\n菜单\n”);
printf(“\n1.提取现金\n”);
printf(“\n2.显示余额\n”);
printf(“\n3.Exit\n”);
printf(“\n输入一个从1到3的数字:”);
option=scanf_s(“%d%c”,&option,&nl);
返回选项;
}
//函数允许您提取现金
提取现金()
{
浮动金额;
int nl,选项;
printf(“\n您想要多少钱?”);
金额=scanf_s(“%d%c”、&option、&nl);
返回选项;
}
//函数显示您的平衡
int getBalance()
{
浮动余额=10000;
int nl,选项;
printf(“\n您想要多少钱?”);
余额=scanf_s(“%d%c”、&option、&nl);
收益余额;
}
//函数更新您的余额
整数更新平衡(浮动余额、浮动金额)
{
int nl,选项;
余额=余额-金额;
收益余额;
}
//主要功能-从这里开始
内部主(空)
{
int-ch;
int opt=0;
整数金额=0;
国际收支平衡=0;
浮动新巴=0.0;
opt=getOption();
printf(“\n您选择了选项%d\n”,opt);
如果(opt==1)
{
金额=提取现金();
newbal=更新平衡(10000,金额);
printf(“\n她是您的%d,您的余额是:\n”,金额,新币);
}
如果(opt==2)
{
balance=getBalance();
printf(“\n这是您的余额:%d\n”,余额);
}
printf(“\n谢谢。请拿着您的卡。\n”);
ch=getchar();
返回0;
}
以下任一选项之一:

  • 使用为其定义的Microsoft编译器
  • 改用ISO C90/C99标准库函数
  • 使用支持ISO C11库的编译器
  • 添加
    \define scanf\u s scanf

但是请注意,您传递给
scanf\u s
的参数不正确,因为格式说明符-它们对于
scanf
是正确的-这可能会建议首选解决方案(并且不是最后一个;-)。

code-like存在链接器问题,并且不正确地使用
scanf\u s()

scanf_s()
需要两个参数,每个参数分别为
“%s”
“%[”
“%c”
:一个
char*
地址和
rsize\u t
大小

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

您在哪个平台上?
scanf_s
是一个Windows函数。我想这应该由您自己来解决才能通过课程。请注意,
scanf_s()
是C11标准的可选附录K中的一个函数。唯一实现了与标准函数近似的平台是Microsoft(而且近似值也不是很好-请参见)。我可以在Eclipse中与MinGW和GCC一起使用其他函数吗?您可以使用
scanf()
,但您需要检查调用顺序是否存在差异(尽管实际上,在上下文中,我认为没有任何差异)。并且您可能必须避免“拒绝使用Microsoft C库”。使用
\define scanf\u s scanf
会在
%s“
%C”
“%[”时产生问题
格式说明符用作所需更改的参数数量。@chux:这是真的,只是在问题中的代码中,在使用了
%c
的地方没有提供必要的附加参数,因此看起来
scanf
在任何情况下都是有意的。最后的建议不是一个非常严肃的建议。