I wrote this code as a practice assignment and cannot seem to get my opening question to loop until the correct key is entered, correctly. I would like the question to be asked until the Y,y,N or n key is entered. If any other key is entered (ex. t or %) i would like it to ask the question again. i have tried many different loop combinations and there always seems to be one condition in which the question will loop indefinitly. If anyone knows how to loop the opening question until the correct key is entered and still make the program execute the subsequent "if" statements i would greatly appreciate your input.
#include <stdio.h>
int main()
{
int a;
char noun_one[20];
char adjective[20];
char verb[20];
char noun_two[20];
do{
printf ("Would you like to do a MadLib?(Y/N):");
getchar(); } while (a!='Y'||a!='y'||a!='N'||a!='n');
if (a=='y'|| a=='Y'){
printf ("Type in a noun:");
scanf ("%s", &noun_one);
printf ("type an adjective:");
scanf ("%s", &adjective);
printf ("type a verb:");
scanf ("%s", &verb);
printf ("type another noun:");
scanf ("%s", &noun_two);
printf ("Compiling Madlib...");
printf ("This is what i came up with =)\n The %s ribbon was wrapped around the %s.", adjective, noun_one);
printf ("When the %s came to the house and saw he %s as fast as he could to the door",noun_two, verb);
}
else if (a=='N'||a=='n')
printf ("Well maybe another time than!");
getch();
return 0;
}



LinkBack URL
About LinkBacks



