Hello, in my program, i ask them to enter data, then i ask if they want to do it again (Y) - which repeats it again; or not (N) in which case it exits the program.
My problem is: It works fine the first time, but when i type Y, it repeats ''Enter Text:'' and also prints ''Again? (Y/N):'', so that i can't answer the first part.
Anyone know why it's doing this? It is having the same error as if i was using scanf and needed to 'flush' it (yes i know fflush(stdin) doesn't work)
Here's the code:
Code:#include <stdio.h> void fConvert(char[]); int main(void) { char cText[100], cInputYN[5], cAnsYN; int xAgain, iInputError; do { printf("Enter text:"); fgets(cText, 100, stdin); fConvert(cText); do { printf("\n\nAgain? [Y/N]"); fgets(cInputYN, 2, stdin); cAnsYN = cInputYN[0]; if (cInputYN[0] != '\n') { if (cAnsYN == 'Y' || cAnsYN == 'y') { xAgain = 1; iInputError = 0; } else if (cAnsYN == 'N' || cAnsYN == 'n') { xAgain = 0; iInputError = 0; } else { printf("\n\tInvalid letter. Please try again.\n"); iInputError = 1; } } else { printf("\n\tInvalid Input. Please try again.\n"); iInputError = 1; } }while (iInputError == 1); }while (xAgain == 1); printf("\n\tThanks for using B-Cipher V0.1.\n\n"); return 0; } void fConvert(char b[100]) { int a = 0; for(a; b[a]!='\0'; a++) { switch(b[a]) { case 'a': printf("t"); break; case 'b': printf("e"); break; case 'c': printf("s"); break; case 'd': printf("l"); break; case 'e': printf("a"); break; case 'f': printf("b"); break; case 'g': printf("c"); break; case 'h': printf("d"); break; case 'i': printf("f"); break; case 'j': printf("g"); break; case 'k': printf("h"); break; case 'l': printf("i"); break; case 'm': printf("j"); break; case 'n': printf("k"); break; case 'o': printf("m"); break; case 'p': printf("n"); break; case 'q': printf("o"); break; case 'r': printf("p"); break; case 's': printf("q"); break; case 't': printf("r"); break; case 'u': printf("u"); break; case 'v': printf("v"); break; case 'w': printf("w"); break; case 'x': printf("x"); break; case 'y': printf("y"); break; case 'z': printf("z"); break; case ' ': printf(" "); break; case '\n': break; default: printf("\nError, was not a letter\n"); } } }



LinkBack URL
About LinkBacks
)


