473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

scanf() problem

I'm just starting C language classes, and I have searched around to understand why that part of my code behaves the way it does and come to find this site, a page titled "Warning against Scanf" where it says scanf() is less prone to errors when it's used along with fgets().

Now, we haven't studied fgets() yet and I don't know the correct syntax for it. This is the part of my code that causes a problem.

===========================================

/*asks a user to enter a number less than 25 and gives a warning if a number greater than 25 is entered*/

int x=26;

printf("enter a number");

while(x>25)
{
scanf("%d",&x);

if(x>25)
printf("Enter a number less or equal to 25");
else
printf("the number you entered is %d",x);
}
==============================================

Now this segment works fine as long as the user enters a number, but it starts acting weird if a character or an array of characters is entered.

how do I use fget() in this case? and is there a way to handle this issue without using fget()? like tweaking scanf() or something?
Jul 15 '07 #1
8 2222
JosAH
11,448 Expert 8TB
I'm just starting C language classes, and I have searched around to understand why that part of my code behaves the way it does and come to find this site, a page titled "Warning against Scanf" where it says scanf() is less prone to errors when it's used along with fgets().

Now, we haven't studied fgets() yet and I don't know the correct syntax for it. This is the part of my code that causes a problem.

===========================================

/*asks a user to enter a number less than 25 and gives a warning if a number greater than 25 is entered*/

int x=26;

printf("enter a number");

while(x>25)
{
scanf("%d",&x);

if(x>25)
printf("Enter a number less or equal to 25");
else
printf("the number you entered is %d",x);
}
==============================================

Now this segment works fine as long as the user enters a number, but it starts acting weird if a character or an array of characters is entered.

how do I use fget() in this case? and is there a way to handle this issue without using fget()? like tweaking scanf() or something?
You're not supposed to tweak anything that is part of the Standard ;-) Note that
scanf() returns the number of actually scanned items. So if the user types anything
else but a number scanf() will return 0 (zero) given your "%d" scan format string.
Your program should handle that situation.

kind regards,

Jos
Jul 15 '07 #2
You're not supposed to tweak anything that is part of the Standard ;-) Note that
scanf() returns the number of actually scanned items. So if the user types anything
else but a number scanf() will return 0 (zero) given your "%d" scan format string.
Your program should handle that situation.

kind regards,

Jos
When I try and enter a character, it goes into an infinite loop. Why does that happen?
Jul 15 '07 #3
JosAH
11,448 Expert 8TB
When I try and enter a character, it goes into an infinite loop. Why does that happen?
As I wrote: scanf() doesn't 'eat' the faulty character. Suppose I enter "a123" and
scanf should scan a "%d" (i.e. an integer number); it 'sees' the 'a' and simply
bails out, leaving that 'a' in the input stream. Then you try again: same result;
and again and again.

According to the scanf() return value you should discard that offending character
or even the entire line or whatever suits you fine. Simply retrying things again
and again in a loop doesn't work: i.e. scanf() would reject that same character
again and again.

kind regards,

Jos
Jul 15 '07 #4
and an fgets() would flush out the garbage character? so what is the correct syntax?
Jul 15 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Use getch() to eat the offending byte. fgets() is for extracting an entire C string from the input buffer.
Jul 15 '07 #6
This is out of my current knowledge, i'm tied to the total basics. maybe you could show me an example of how getch() works? i mean , what to white inside brackets?
Jul 15 '07 #7
JosAH
11,448 Expert 8TB
[center]
This is out of my current knowledge
No it is not; when/if your scanf() call doesn't return 1 (one), eat that offending
character and see how your program behaves.

kind regards,

Jos
Jul 15 '07 #8
Thank you! I got it. you were very helpful. It took some brain storming but I finally got it.
Jul 15 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double'...
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
5
by: Eduardo Olivarez | last post by:
The following code does not work correctly on my machine. Either one of the scanf()'s alone work perfectly. However, when they are combined, the second scanf() call just reads what the first one...
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
6
by: obdict | last post by:
Hello, I used scanf() in a while loop, which ensures that user input is valid (must be an integer no greater than 21 or less than 3). If user enters a number out of the range, or enters...
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
14
by: main() | last post by:
I know this is the problem that most newbies get into. #include<stdio.h> int main(void) { char a; scanf("%c",&a); /*1st scanf */ printf("%c\n",a); scanf("%c",&a); /*2nd scanf*/...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
13
by: FerrisUML | last post by:
Hello everyone! I new to C and am having the following problem. In the below program, the last scanf is being ignored and the program exits. Can anyone see anything that im doing wrong? Thanks...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.