2016-10-28 1 views
-6

Wie Sie eine separate Funktion für den Fortfahren oder den Stopp erstellen, so dass Sie sich immer darauf beziehen können. unten keine richtige C aber nur eine Idee, wasVerwenden Sie die gleiche Funktion für mehrere Orte innerhalb eines Codes für C

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

Ich habe versucht, dies zu erreichen, aber der Wert aus dem Futter, weiter nicht auf die Hauptfunktion passieren kann. ist etwas falsch mit meiner Logik oder es gibt einfachere Möglichkeiten, dies zu tun.

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

genauer beschreiben, was Sie wünschen, ist es jetzt nicht klar – Pavel

+0

Wie auf der Erde ist dieses gültige C? –

Antwort

0

Sie können ContinueOrStop Funktion außerhalb der Hauptfunktion machen und dann in Haupt anrufen, wenn Sie benötigen wie

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(); 
} 
Verwandte Themen