I am trying to get a simple program to run properly. I am having problems with scanf. If I use %1C the program will output twice then quit. If I use %1s this problem is fixed but if I enter CC(CR) then the output is hosed up. Any suggestions?
#include <stdio.h>
int main(void)
{
char iSelection = 'A';
while (iSelection <='C' && iSelection >= 'A'){
printf("A\tDeposit Funds\n");
printf("B\tWithdraw Funds\n");
printf("C\tPrint Balance\n");
printf("D\tQuit\n");
printf("Enter your selection (A-D): ");
scanf("%1s",&iSelection);
}
if (iSelection !='D')
printf("\nIllegal Response\n");
else {
printf("\nThank You!\n");
}
return 0;
}
To minimize my brain use.
Can you confirm:
You want to get "Illegal response" unless a capital D in entered by the user?
I do not understand why you are using while, unless you are playing to get the while function to work for you.
You need to flush stream after scanf() and handle LF too.
For example:
#include <stdio.h>
int main(void)
{
char iSelection;
char cTmp;
do {
printf("A\tDeposit Funds\n");
printf("B\tWithdraw Funds\n");
printf("C\tPrint Balance\n");
printf("D\tQuit\n");
printf("Enter your selection (A-D): ");
scanf("%c", &iSelection);
while (scanf("%c", &cTmp) && cTmp != 10); // flush stream up to LF
} while ((iSelection <= 'C' && iSelection >= 'A') || iSelection == 0xA);
if (iSelection != 'D')
printf("\nIllegal Response\n");
else {
printf("\nThank You!\n");
}
return 0;
}
Assuming I understood the original purpose, and changing the original code as little as possible I have:
#include <stdio.h>
int main(void)
{
char iSelection = 'A';
while (iSelection <='C' && iSelection >= 'A')
{
printf("A\tDeposit Funds\n");
printf("B\tWithdraw Funds\n");
printf("C\tPrint Balance\n");
printf("D\tQuit\n");
printf("Enter your selection (A-D): ");
scanf("%1s",&iSelection);
while (getchar() != '\n') // flushing the buffer
continue; // flushing the buffer
if (iSelection !='D')
printf("\nIllegal Response\n\n");
else
{
printf("\nThank You!\n");
}
}
return 0;
}
Timo,
I am trying to understand how your code works.
Quote
while (scanf("%c", &cTmp) && cTmp != 10); // flush stream up to LF
} while ((iSelection <= 'C' && iSelection >= 'A') || iSelection == 0xA);
The buffer for iSelection is flushed because you called scanf again reading to a different char array (cTmp) ? Is this right?
Also the code " || iSelection == 0xA ". Help me here: "0xA" is an error code?
What possible input by the user would require this "|| iSelection == 0xA" addition ???
I just want to understand your reasoning.
Tx, Ed
QuoteThe buffer for iSelection is flushed because you called scanf again reading to a different char array (cTmp) ? Is this right?
Yes. Read one char at the time to variable until linefeed found. cTmp is a char variable not an array.
If you enter something like Code<LF> only C as first character is used.
QuoteAlso the code " || iSelection == 0xA ". Help me here: "0xA" is an error code?
No it's a linefeed = 10 = 0xA = LF
while (scanf("%c", &cTmp) && cTmp != 10); // flush stream up to LF
} while ((iSelection <= 'C' && iSelection >= 'A') || iSelection == 10); // between A to C or linefeed
Ed ... yes I am learning the while function. Any response other than 'D' produces an illegal response. That works fine. If you acidently enter lets say BB then the output is all hosed up.
Timo,
Thanks, I learned a couple things there!
K,
I was playing with scanf and fgets differences in post http://forum.pellesc.de/index.php?topic=3197.0, I found out about the need to flush the system buffer fr LW (see his explanation). I like scanf, it is powerful with all it's features, but will trip you up when you are trying to not pay too much attention to it (because you are experimenting mainly with a different function).
I am finding the time you can spend understanding the details of the powerful C functions at first is frustrating, but actually is what makes C very interesting, giving me (at least) a sense it is time well spent.
Salut to Pelle's for his fine IDE that "gets the IDE complexity out of the way", because the language itself is complex enough !
... Ed