c++,这个while怎么回事?

while (n < 4)
	{
		printf("请输入您的卡号:\n");
		scanf_s("%c", &ID);
		printf("请输入您的密码;\n");
		scanf_s("%d", &password);
		encryption(password);
		result = check_password(password, ID);
		if (result)
		{
			printf("");
		}
		else
		{
			printf("您的密码不正确,请重新输入\n");
			n += n;
		}
	}
	printf("连续三次输入错误,您已锁卡");

我先CTRL+F5,输入随便什么,回车,还没输密码他就这样显示:

请输入您的卡号:
66666
请输入您的密码;
您的密码不正确,请重新输入
请输入您的卡号:
请输入您的密码;

这怎么改哇

#include<stdio.h>
#ifndef AIM
#define AIM
int encryption(int password);
int check_password(int password,char* ID);
#endif 
#include<stdio.h>
int encryption(int password)
{
	password += password;
	return password;
}
int check_password(int password, char* ID)
{
	return 0;
}
int main()
{
	char ID[30];
	int password, result;
	int n = 1;
	while (n < 4)
	{
		printf("请输入您的卡号:\n");
		scanf("%s", &ID);
		printf("请输入您的密码:\n");
		fflush(stdin);
		scanf("%d", &password); 
		encryption(password);
		result = check_password(password, ID);
		if (result)
		{
			printf("");
		}
		else
		{
			printf("您的卡号或密码错误。");
			n += n;
		}
	}
	printf("连续三次输入错误,您已锁卡。");
	return 0;
}

你要看看你check_password的方法是怎样的逻辑

改成scanf_s("%s", &ID);     你输入的是字符串,干嘛用%c  

请在每个scanf_s前面加上fflush(stdin);语句就正常了。

可能就是你随便输入什么搞的吧。你按照输入的要求正常输入吧

scanf_s("%c", &ID);,%c只能输入一个字符,如果你打多了,就有问题。

建议改成scanf_s("%s", &ID);

而且在windows下,敲击回车后,会多一个换行符,用%c会出意想不到的went问题。

如有帮助,请采纳一下,谢谢。

#include<stdio.h>

int main()
{
	char ID;
	int password, result;
	int n = 1;
	while (n < 4)
	{
		printf("请输入您的卡号:\n");
		scanf_s("%c", &ID);
		printf("请输入您的密码:\n");
		fflush(stdin);
		scanf_s("%d", &password); 
		encryption(password);
		result = check_password(password, ID);
		if (result)
		{
			printf("");
		}
		else
		{
			printf("您的卡号或密码错误。");
			n += n;
		}
	}
	printf("连续三次输入错误,您已锁卡。");
	return 0;
}
#ifndef AIM
#define AIM
int encryption(int password);
int check_password(int password,char ID);
#endif 
#include<stdio.h>

int encryption(int password)
{
	password += password;
	return password;
}
int check_password(int password, char ID)
{
	return 0;
}

 

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

看到你在问答里面提了一个问题 ,我有对应的课程专栏。希望能帮到你。
本人简介:https://blog.csdn.net/it_xiangqiang/article/details/115873169
C和C++完整教程:    https://blog.csdn.net/it_xiangqiang/category_10581430.html
C和C++算法完整教程:    https://blog.csdn.net/it_xiangqiang/category_10768339.html

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y