?
Solved

Preventing buffer overload in scanf

Posted on 2003-03-23
6
Medium Priority
?
456 Views
Last Modified: 2010-04-15
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
Comment
Question by:supertw2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8193452
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8193459
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
 
LVL 8

Expert Comment

by:akshayxx
ID: 8193464
>>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.

 

Expert Comment

by:Getch
ID: 8193683
#include <stdio.h>

void main(void) {
     char switchvar;

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

the code talks.
             
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8193695

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
 

Expert Comment

by:Getch
ID: 8194193
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

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question