2016-06-24 6 views
-4

Hallo ich bin neu in C und ich schrieb ein einfaches Programm. Ich möchte das Programm neu zu starten, wenn der Benutzer die falsche Wahl gepflückt, hier ist der Code:Wie kann ich das Programm in C neu starten?

#include <stdio.h> 
#include <cs50.h> 
int main(void){ 
    char choices; 
    float math, pc, svt, eng, philo; 
    do { 
     do { 
     printf("Enter your math score: "); 
     math = GetFloat(); 
    } 
    while(math>20 || math<0); 
    do { 
     printf("Enter your pc score: "); 
     pc = GetFloat(); 
    } 
    while(pc>20 || pc<0); 
    do { 
     printf("Enter your svt score: "); 
     svt = GetFloat(); 
    } 
    while(svt>20 || svt<0); 
    do { 
     printf("Enter your eng score: "); 
     eng = GetFloat(); 
    } 
    while(eng>20 || eng<0); 
    do { 
     printf("Enter your philo score: "); 
     philo = GetFloat(); 
    } 
    while(philo>20 || philo<0); 
    printf("Are you pc or sm?\n"); 
    printf("Write 1 for pc. 2 for sm\n"); 
    int choice = GetInt(); 
    if(choice == 1){ 
     float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25; 
     printf("Your score is %.2f\n", score); 
    } 
    else if(choice == 2){ 
     float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23; 
     printf("Your score is %.2f\n", score); 
    } 
    else{ 
     printf("You've picked the wrong choice \n"); 

    } 

     printf("Do you want to try it again? (Y/N) "); 
     choices = getchar(); 
     while (choices != '\n' && getchar() != '\n') {}; 
    } while (choices == 'Y' || choices == 'y'); 


} 

Also, was ich meine hier, ich möchte den Code in dem anderen Block einzufügen, um das Programm neu zu starten und den Benutzer geben einander mal. Es wäre sehr schön, wenn ich ihn einfach wieder zwischen 1 oder 2 wählen lassen würde.

Wenn Sie irgendwelche Vorschläge oder Verbesserungen haben, zögern Sie bitte nicht zu kommentieren. Thanks :)

+0

http://www.tutorialspoint.com/cprogramming/c_goto_statement.htm –

+0

* „wollen das Programm neu zu starten, wenn der Benutzer die falsche Wahl gepflückt“ * Nein, Sie nicht. Sie möchten den Benutzer für eine bessere Eingabe erneut auffordern. –

+0

@AlexandruCimpanu 'goto', wirklich? http://stackoverflow.com/questions/46586/goto-still-consided-harmful – Jezor

Antwort

2

Was Sie brauchen, ist do while Schleife Wahl Code Umgebung:

int choice; 

do { 
    choice = GetInt(); 
    if (choice == 1) { 
     float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25; 
     printf("Your score is %.2f\n", score); 
    } 
    else if (choice == 2) { 
     float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23; 
     printf("Your score is %.2f\n", score); 
    } 
    else { 
     printf("You've picked the wrong choice, try again.\n"); 
    } 
} while(choice < 1 || choice > 2) 
+0

Danke das Programm hat perfekt funktioniert! –

0

Sie haben bereits eine Schleife noch einmal zu versuchen, können Sie diese Schleife wieder verwenden die Benutzereingabe wieder für choice zu bekommen. Wenn der Benutzer also choice anders als 1 oder 2 eingibt, können Sie choices = Y festlegen und die Schleife wiederholen. ohne nach Benutzereingaben zu fragen.

Der Code ist unten.

#include <stdio.h> 
#include <cs50.h> 
int main(void) 
{ 
    char choices; 
    float math, pc, svt, eng, philo; 
    do 
    {  
    // Other code here 

    int choice = GetInt(); 
    if(choice == 1){ 
     float score = (math*7 + pc*7 + svt*7 + eng*2 + philo*2)/25; 
     printf("Your score is %.2f\n", score); 
    } 
    else if(choice == 2){ 
     float score = (math*9 + pc*7 + svt*3+ eng*2 + philo*2)/23; 
     printf("Your score is %.2f\n", score); 
    } 
    else{ 
     printf("You've picked the wrong choice \n"); 
     choices = 'Y'; 

    } 

    if ((choice == 1) || (choice == 2)) 
    { 
     printf("Do you want to try it again? (Y/N) "); 
     choices = getchar(); 
     while (choices != '\n' && getchar() != '\n') {}; 
    } 
    } while (choices == 'Y' || choices == 'y'); 
}