如何在c中从循环中传递变量的值
在这里,我要求用户输入数字:如何在c中从循环中传递变量的值,c,variables,loops,C,Variables,Loops,在这里,我要求用户输入数字: do{ printf("Enter cellphone number +63"); fflush(stdin); gets(pb[i].cellphone); ///check if there is a similar number from the database for(r=0; r<i; r++){ same = strcmp(pb[i].cellphone, pb[r].cellphone); i
do{
printf("Enter cellphone number +63");
fflush(stdin);
gets(pb[i].cellphone);
///check if there is a similar number from the database
for(r=0; r<i; r++){
same = strcmp(pb[i].cellphone, pb[r].cellphone);
if(same==0){
printf("Number is same with contact no. %d\n", r+1);
}
}
/// at this point the value of same is becoming nonzero and continues to the next code.
}while(!isdigit(*pb[i].cellphone)||same == 0);
do{
printf(“输入手机号码+63”);
fflush(stdin);
gets(pb[i].mobile);
///检查数据库中是否有类似的编号
对于(r=0;r您需要退出for
循环,或者same
将在下一次循环迭代中被覆盖:
do {
printf("Enter cellphone number +63");
fflush(stdout); // Flush stdout so that text is shown (needed because the printf doesn't end with a newline and stdout is line buffered)
gets(pb[i].cellphone);
///check if there is a similar number from the database
for (r=0; r<i; r++){
same = strcmp(pb[i].cellphone, pb[r].cellphone);
if (same==0){
printf("Number is same with contact no. %d\n", r+1);
break; // Exit the loop. Otherwise same will be overwritten in next iteration
}
}
} while(!isdigit(*pb[i].cellphone) || same == 0);
do{
printf(“输入手机号码+63”);
fflush(stdout);//刷新stdout以便显示文本(需要,因为printf不是以换行结束,并且stdout是行缓冲的)
gets(pb[i].mobile);
///检查数据库中是否有类似的编号
对于(r=0;rint find_num(char*phone,inti,phones*phones){
INTR;
对于(r=0;r您的实际问题是什么?您不允许在输入流上使用fflush
。也不要使用get
,因为您无法实现防止溢出的保护。问题是什么?您期望什么,您得到什么?任何错误?如果它没有找到唯一的数字,那么它将再次从循环中询问,并且您是单元格吗用字符表示的电话号码?我也不确定您是否真的想用这种方式使用isdigit
,因为它可能不会像您预期的那样工作。
int find_num(char *phone, int i, phones *phones) {
int r;
for(r=0; r<i; r++){
if (!strcmp(phone, pb[r].cellphone))
return r;
}
return -1;
}
while (1) {
printf("Enter cellphone number +63");
fflush(stdin);
gets(pb[i].cellphone);
same = find_num(pb[i].cellphone, i, pb);
if (same == -1) break;
printf("Number is same with contact no. %d\n", same+1);
}