Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 459
  • Last Modified:

Preventing buffer overload in scanf

Here's a snippet:
char switchvar;
errorlbl:
printf("Enter a case number");
scanf("%c",&switchvar");
switch(switchvar){
.......
default:
    goto errorlbl;
}

the problem is if i enter 2 characters it will loop 2 times etc.
how can i prevent this?
0
supertw2000
Asked:
supertw2000
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
fflush ( stdin ) ;
scanf ( "%c", &switchvar ) ; // check this line of yours - you have an inverted quote after this
fflush ( stdin ) ;

This will do your thing in DOS. Not very sure about Linux.

Mayank.
0
 
akshayxxCommented:
1. if u expect to enter 2 chars , then enter two chars.

2. if u r unsure howmany chars user may enter, then read the input as a string, and consider only first character of the string
like this

char input[32];
scanf("%s",input);
switch(input[0]){


}

in this case if user inputs "yes" for y ,
ur program will continue as normally considering only 'y' of "yes"


3. for choices mechanism ,  try integer-choice method.
like 1 ,2,3,66 as choices.

and read as integer.



0
 
akshayxxCommented:
>>1. if u expect to enter 2 chars , then enter two chars.
i meant .. enter only that much .. which ur program can handle.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
GetchCommented:
#include <stdio.h>

void main(void) {
     char switchvar;

     printf("Enter a case number :");
     getchar(switchvar);
     switch(switchvar) {
            .
            .
            .
       default :
        goto errorlbl;
     }
}

the code talks.
             
0
 
KocilCommented:

For this purpose I suggest get() instead of scanf
  char switchvar;
  errorlbl:
  printf("Enter a case number");
  switchvar = getch();
  switch(switchvar){
    .......
   default:
     goto errorlbl;

And ...
if you don't mind please dont use goto/label.
According to Knut's bible, that's evil :)
You may improve your code like this

  char switchar;
  int valid;
  do {
    printf("Enter a case number");
    switchvar = getch();
    valid = 1;
    switch(switchvar){
      .......
      default:
        valid = 0;
    }
  } while (valid);



0
 
GetchCommented:
CODE:

#include <stdio.h>

void main (void) {
     char switchvar;

     while((scanf("%c",&switchvar)) == 1) {
      switch (switchvar) {
              .
               .
                .
       default :
            goto ....;
      }
     }
}

THIS IS WAT YOU NEED ...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now