运行的时候,输入了“123456”,结果没办法继续运行。
#include<stdio.h>
#include<string.h>
int main()
{
char password[20] = { 0 };
printf("请输入密码:");
scanf_s("%s", password); //由于数组自带地址,不需要&符号
printf("请确认密码(Y/N):");
int imp = 0;
while ((imp = getchar()) != '\n') {
;
} //清空缓冲区
int ch = getchar();
if (ch == 'Y')
printf("确认成功\n");
else
printf("确认失败\n");
return 0;
}
在scanf那一行,报错 0x7C01E63C (ucrtbased.dll)处(位于 Project1.c.exe 中)引发的异常: 0xC0000005: 写入位置 0x00D00000 时发生访问冲突。
不知道
输入一个密码,然后再输入Y或N,输出“确认成功”或“确认失败”
scanf_s函数password后面要加字符串长度。scanf_s函数为了确保字符串安全,要求参数指定字符串的最大长度,避免溢出
改为:
scanf_s("%s", password, 20);
#include<stdio.h>
#include<string.h>
int main()
{
char password[20] = { 0 };
printf("请输入密码:");
scanf_s("%s", password,20); //由于数组自带地址,不需要&符号
printf("请确认密码(Y/N):");
int imp = 0;
while ((imp = getchar()) != '\n') {
;
} //清空缓冲区
int ch = getchar();
if (ch == 'Y')
printf("确认成功\n");
else
printf("确认失败\n");
return 0;
}
scanf_s在接受字符的时候需要一个参数代表字符串的最长长度
类似scanf_s("%s", password,19);