NO

Author Topic: Next problem  (Read 3440 times)

KTaschner

  • Guest
Next problem
« on: June 24, 2010, 06:07:28 PM »
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?

Code: [Select]
#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;
}

EdPellesC99

  • Guest
Re: Next problem
« Reply #1 on: June 27, 2010, 08:30:00 PM »
   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.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Next problem
« Reply #2 on: June 28, 2010, 09:33:57 AM »
You need to flush stream after scanf() and handle LF too.
For example:
Code: [Select]
#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;
}
May the source be with you

EdPellesC99

  • Guest
Re: Next problem
« Reply #3 on: June 28, 2010, 04:59:47 PM »
   Assuming I understood the original purpose, and changing the original code as little as possible I have:

Code: [Select]
#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;
}


EdPellesC99

  • Guest
Re: Next problem
« Reply #4 on: June 28, 2010, 05:15:18 PM »

   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
   

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Next problem
« Reply #5 on: June 28, 2010, 07:24:41 PM »

Quote
The 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.

Quote
Also the code " || iSelection == 0xA ". Help me here:    "0xA" is an error code?
No it's a linefeed = 10 = 0xA = LF

Code: [Select]
    while (scanf("%c", &cTmp) && cTmp != 10);   // flush stream up to LF
   } while ((iSelection <= 'C' && iSelection >= 'A') || iSelection == 10);   // between A to C or linefeed
May the source be with you

KTaschner

  • Guest
Re: Next problem
« Reply #6 on: June 28, 2010, 09:45:15 PM »
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.

EdPellesC99

  • Guest
Re: Next problem
« Reply #7 on: June 29, 2010, 02:54:21 PM »
   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