我对禁止同一学生证的要求感到沮丧。
假设我需要输入3个学生ID:
但我不知道如何扫描所有以前的数据并比较用户输入的数据。 谢谢你的帮助。
#include<stdio.h>
int main()
{
int Num;
printf("Enter a number for student:"); //enter the number of student
scanf("%d",&Num);
int i,a,CheckId;
int myArr[Num];
for(i=0;i<Num;i++)
{
printf("\n\nEnter student ID:");//type the StudentID
scanf("%d",&myArr[i]);
}
//This part i want to check whether the data is same
do
{
printf("Enter a number");
scanf("%d",&CheckId);
if(CheckId==myArr[i])//Assume the number is same
{
break;
}
}while(CheckId!=myArr[i]);//Assnume the number is not same and continue
//But how can i check the previous data
}
答案 0 :(得分:0)
您需要在for
循环内添加嵌套的do-while
循环,然后将输入的值与for
循环内的所有数组值进行比较。
此外,您可以在结尾处使用while(1)
而不是您已编写的条件,因为该条件永远不会是false
,因为如果它是break
,则false
离开循环do-while
。
编辑 :(在OP的评论中)
在您使用while(CheckId!=myArr[i])
的{{1}}中,我建议您将其替换为while(1)
,但这不是必需的。
您需要连续(无限期地)接受来自用户的输入,直到它等于数组中的某个值。
您可以在成功比较时突破内部for
循环,但在break
出局之前,您需要设置一个flag = 1;
,您需要在外部{{1}进行监控从它循环到do-while
。
你可以用两种方式做到:
1.
break
2。
do {
...
if(flag == 1)
break;
}
while(1);