gpt4 book ai didi

c++ - 尝试扫描到全局 int 时出现奇怪的错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:06:14 25 4
gpt4 key购买 nike

这是代码

#include "stdafx.h"
#include <string>
#include <clocale>
#include <stdio.h>
#include <cstdlib>

using namespace std;

int souls;

void userInput(char situation[20]) {
if (situation == "souls") {
scanf("%i", souls);
printf("%i", souls);
}
}

void main() {
setlocale(LC_CTYPE, "rus");

userInput("souls");

system("pause");
}

当我通过控制台(例如 int 数字)在我的 scanf() 中输入一些东西(试图改变一个全局 int)后它停止了,并且让我进入了一个“未处理的异常”

enter image description here

为什么会这样?我正在使用 MS Visual Studio 2005。

最佳答案

在你的代码中

scanf("%i", souls);

应该是

scanf("%i", &souls);
^

scanf()需要一个指向类型的指针作为参数来存储与提供的格式说明符相对应的扫描值。

也就是说,if (situation="souls") 也是错误的。您不能使用 == 运算符比较字符串的内容。您需要使用 strcmp()为此。

关于c++ - 尝试扫描到全局 int 时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34799333/

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