for example
- Code: Select all
#include<stdio.h>
#include<string.h>
main()
{
char c;
int i;
char str[]="You are correct!";
char str2[]="You are incorrect, press enter to start over";
char str3[]="yes";
char str4[]="no";
char str5[5];
printf("Welcome to the quiz!\n");
printf("There are 5 questions\n");
printf("\n");
printf("Please type yes or no to the questions\n");
printf("\n");
printf("lowercase yes and no only!\n");
printf("\n");
printf("If you get a question wrong the program will start over!\n");
printf("\n");
printf("Type enter to start\n");
c = getc(stdin);
next:
do{
printf("Magnum opus means great work. yes or no?\n");//question 1
gets(str5);
} while (strcmp (str3, str5) != 0);//correct answer is yes
printf("\n");
puts ("Correct answer!");
printf("\n");
printf("A paragon is a self contradictory statement. yes or no?\n");//question 2
gets(str5);
if (strcmp (str4, str5) != 0){ //correct answer is no
printf("\n");
printf("%s\n", str2);
printf("\n");
c = getc(stdin);
goto next;
}
else{
printf("A dystopia is a perfect world. yes or no?\n");
}
gets(str5);
if (strcmp (str4, str5) != 0){ //correct answer is no
printf("\n");
printf("%s\n", str2);
printf("\n");
c = getc(stdin);
goto next;
}
else{
printf("Ephemeral means lasting for a short time. yes or no?\n");
}
gets(str5);
if (strcmp (str3, str5) != 0){ //correct answer is yes
printf("\n");
printf("%s\n", str2);
printf("\n");
c = getc(stdin);
goto next;
}
else{
printf("Colonoscopy refers to a successful colony. yes or no? \n");
}
gets(str5);
if (strcmp (str4, str5) != 0){ //correct answer is no
printf("\n");
printf("%s\n", str2);
printf("\n");
c = getc(stdin);
goto next;
}
else{
printf("You won! Congratulations!\n");
printf("\n");
printf("If you won on your first try you are smart \n");
printf("\n");
printf("If you took a lot of tries to win, you are smart but need to try harder\n");
}
printf("Press enter to exit\n");
c = getc(stdin);
return 0;
}
What is an alternative to the goto? I seem to be using it for all my programs now....but its a bad habit.
Preferably you can show me with this code.
Thanks


