在这里,我要求用户输入号码:

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);

我的目标是,如果用户输入一个非唯一的号码,它将要求获得用户输入一个新号码。

最佳答案

您需要退出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);

关于c - 如何将变量的值从c的循环中传递出去,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21577281/

10-09 18:20