2016-10-28 3 views
-6

ContinueまたはStopのために別々の関数を作成する方法は、常にそこを参照することができます。ない適切なCが、何Cのコード内の複数の場所で同じ機能を使用

main() 
    do{ 
     request for input... 
      if found invalid, Continue or stop? 
      ... 
     After one complete run 
      ask again for Continue or stop? 
      ... 
    }while(Continue) 

を達成するだけのアイデアの下に私はこれを試してみましたが、フィードからの値、続行は、main関数に渡すことはできません。私のロジックに何か問題があるか、これを行うためのより簡単な方法があります。

struct Ans { 
    int Continue; 
    int Stop; 
    char choice; 
}; 

struct Ans feed ; 
void YesNo(){ 
    int p=1; 
    int q=2; 
    char choice; 
    feed.Continue=0; 
    feed.Stop=0; 
    while (feed.Continue==0) 
    { 
     printf("\nDo you wish to try again (Type Y to continue Q to quit:"); 
     scanf(" %c", &choice); 
     choice = toupper(choice); 
     if(choice == 'Y') 
     { 
      feed. Continue = p; 
      return; 

     } 
     else if(choice=='Q') { 

      feed.Stop = q; 
      return; 
     } 

     else 
     { 
      printf("\nError: Invalid choice\n"); 
      do 
      { 
       feed.choice = getchar(); 

      } while (feed.choice != '\n' && feed.choice != EOF); 
     } 
    } 

} 
void main{   
Loop:do{ 
    printf("Please enter(k):\n"); 
    scanf("%d",&k); 
    struct Ans feed; 
    char str[N]; 
    if (fgets(str, sizeof str, stdin)) 
    { 
     flag = 0; 

      long value; 
      char *check; 

      value = strtol(str, &check, 0); 

      if (!isspace(*check) && *check != 0) flag = 1; 
      { 
       printf("\nInvalid Input\n"); 

      } 

      while (flag == 1) 
      { 
       YesNo(); 
       flag = 0; 
       { 
        if (feed.Continue) { 
         goto Loop; 
        } 
        else if (feed.Stop) exit(0); 
       } 
      } 

     } 

     if ((k<0)||(k>N-1)) { 
      printf("Input value out of range"); 
     } 
     YesNo(); 
     { 
      if (feed.Continue) { 
       goto Loop; 
      } 
      else if (feed.Stop) exit(0); 

     } 



     //run something 

     do{ 
      printf("\nDo you wish to try again (Type Y to continue Q to quit:"); 
      scanf(" %c", &choice); 
      choice = toupper(choice); 
     }while((choice != 'Y') && (choice != 'Q')); 

    }while(choice == 'Y'); 

     return ; 
    } 

} 
+1

のように必要なときに、あなたが何をしたいか詳細を説明してメインで呼び出して、それは今 – Pavel

+0

明らかではないが、どのように地球上のこの有効なC? –

答えて

0

あなたは、main関数の外ContinueOrStop機能を作ることができ、その後、あなたが

void ContinueORStop() 
{ 
    do{ 
     request for input... 
      if found invalid, Continue or stop? 
      ... 
     After one complete run 
      ask again for Continue or stop? 
      ... 
    }while(Continue) 
} 

main() 
{ 
    ContinueOrStop(); 
} 
関連する問題