473,320 Members | 1,846 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 for char input getting skipped

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 in
advance.

#include <stdio.h>

/*
HOMEWORK: Assignment 1
Name: Dennis McQuilken
Class: C Programming
Date: 9.19.2008
Description: This is a C program that will calculate an employees
gross pay based on hours work and hourly rate.
*/
int main()
{

int clock_number = 0;
float hours_worked = 0;
float hourly_rate = 0;
float total = 0;
char quitVal;

goto start;

start:

printf("Please enter the employee's clock number:\n");
scanf("%i", &clock_number);
printf("Please enter the employee's hourly rate:\n");
scanf("%f", &hourly_rate);
printf("Please enter the hours worked:\n");
scanf("%f", &hours_worked);
total = hourly_rate * hours_worked;
printf("Employee Clock Number\tHourly Rate\tHours Worked\tGross\n");
printf("%i\t\t\t%0.2f\t\t%0.2f\t\t%0.2f\n", clock_number,
hours_worked, hourly_rate, total);
printf("Do you want to quit:");
scanf("%c", &quitVal);
goto done;

done:

return 0;
}
Sep 18 '08 #1
13 4293
FerrisUML said:
Hello everyone! I new to C and am having the following problem. In
the below program, the last scanf is being ignored
No, it isn't being ignored.
and the program
exits. Can anyone see anything that im doing wrong?
Yes. I have removed from your program everything that is not relevant to
this specific problem.
#include <stdio.h>

int main()
{
float hours_worked = 0;
char quitVal;

scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);

return 0;
}
This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let's say your hours_worked value is 4.2 - so what do you type? It's no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you're about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you've
finished this line of data. The characters '4', '.', '2', '\n' are now
made available to your C program on its standard input stream. The scanf
munches the '4', '.', and '2' to make 4.2, but '\n' isn't part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more input
from the user.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 18 '08 #2
On Sep 17, 10:38*pm, Richard Heathfield <r...@see.sig.invalidwrote:
FerrisUML said:
Hello everyone! *I new to C and am having the following problem. *In
the below program, the last scanf is being ignored

No, it isn't being ignored.
and the program
exits. *Can anyone see anything that im doing wrong?

Yes. I have removed from your program everything that is not relevant to
this specific problem.
#include <stdio.h>
int main()
{
float hours_worked = 0;
char quitVal;
scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);
return 0;
}

This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let's say your hours_worked value is 4.2 - so what do you type? It's no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you're about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you've
finished this line of data. The characters '4', '.', '2', '\n' are now
made available to your C program on its standard input stream. The scanf
munches the '4', '.', and '2' to make 4.2, but '\n' isn't part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more input
from the user.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
I had a feeling whomever responded would say that. Whats the best way
to clear the input stream buffer before peforming a scanf? I've
researched and found the following, but im not sure if its the best
approach seeing as I have to swap scanf for getchar.
printf("Would you like to calculate for another employee? (y/n)\n");
while(getchar() != '\n') continue;
if(getchar() == 'y')
{
goto start;
}
else
{
goto done;
}
Sep 18 '08 #3
FerrisUML said:

<snip>
I had a feeling whomever responded would say that. Whats the best way
to clear the input stream buffer before peforming a scanf?
int fpclear(FILE *fp)
{
int ch;
while((ch = getc(fp)) != '\n' && ch != EOF)
{
continue;
}
return ch == EOF;
}

Simplest possible usage:

fpclear(stdin);
Oh, and if you want to make much use of comp.lang.c, I suggest you learn
how to use loops. If you keep posting programs with goto in them, enough
clc subscribers will barf to fill an entire tub.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 18 '08 #4
On 2008-09-18, FerrisUML <dm********@gmail.comwrote:
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 in
advance.

#include <stdio.h>

/*
HOMEWORK: Assignment 1
Name: Dennis McQuilken
Class: C Programming
Date: 9.19.2008
Description: This is a C program that will calculate an employees
gross pay based on hours work and hourly rate.
*/
Thanks! This is a first that I've seen, anyway - it's clear you
know how to ask for help with homework here, the intelligent way.
>
int main()
int main(void) is better style, FWIW.
{

int clock_number = 0;
float hours_worked = 0;
float hourly_rate = 0;
float total = 0;
char quitVal;

goto start;

start:
What is the point of this? You do it again below, but goto is
(almost) never a good idea, and certainly isn't needed here.
printf("Please enter the employee's clock number:\n");
scanf("%i", &clock_number);
printf("Please enter the employee's hourly rate:\n");
scanf("%f", &hourly_rate);
printf("Please enter the hours worked:\n");
scanf("%f", &hours_worked);
total = hourly_rate * hours_worked;
printf("Employee Clock Number\tHourly Rate\tHours Worked\tGross\n");
printf("%i\t\t\t%0.2f\t\t%0.2f\t\t%0.2f\n", clock_number,
hours_worked, hourly_rate, total);
printf("Do you want to quit:");
scanf("%c", &quitVal);
goto done;

done:

return 0;
}
Your problem is the way that scanf() works. The first time it
encounters %i, it skips whitespace until it finds something
integer-looking, and then reads up until it finds something
not integer-looking.

In each case, the first non-integer-looking something will be
the newline character you enter when you hit enter after typing
a number. It puts this newline back so that the next input
function can get it if it needs.

This doesn't matter for next two %f specifiers, because like
%i, they skip whitespace until they find something number-
looking.

BUT, this is not true for %c - it takes that newline there
and happily accepts it as input. Since that's enough to satisfy
it, it doesn't bother requesting the user for something more.
This causes it to "ignore" the last scanf().

I don't remember how to work around this using scanf(), but
I personally would use fgets() to read input a line-at-a-time
and then parse it myself. That way, if my boss comes in and
says "users are trying to type in the word 'one' and expecting
your program to still work" it's a little easier to adapt the
parsing logic.

--
Andrew Poelstra ap*******@wpsoftware.net
That was a joke. Jokes in mathematics, are sometimes not funny.
-Veselin Jungic
Sep 18 '08 #5
FerrisUML <dm********@gmail.comwrites:
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 in
advance.
[...]

The comp.lang.c FAQ is an excellent resource, available at
<http://www.c-faq.com/>. You've just asked question 12.18b.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 18 '08 #6
Richard Heathfield wrote:
FerrisUML said:
>Hello everyone! I new to C and am having the following problem.
In the below program, the last scanf is being ignored

No, it isn't being ignored.
>and the program
exits. Can anyone see anything that im doing wrong?

Yes. I have removed from your program everything that is not relevant to
this specific problem.
>#include <stdio.h>

int main()
{
float hours_worked = 0;
char quitVal;

scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);

return 0;
}

This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let's say your hours_worked value is 4.2 - so what do you type? It's no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you're about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you've
finished this line of data. The characters '4', '.', '2', '\n' are now
made available to your C program on its standard input stream. The scanf
munches the '4', '.', and '2' to make 4.2, but '\n' isn't part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more input
from the user.
That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE. This
allows discovering failures in the scanf call.

All calls should be of the form

if (NUMBER == scanf(...)) { /* accepted */ }
else { /* rejected */ )

with suitable values for NUMBER, and possible retention of the
returned value.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 18 '08 #7
CBFalconer <cb********@yahoo.comwrites:
Richard Heathfield wrote:
>FerrisUML said:
>>Hello everyone! I new to C and am having the following problem.
In the below program, the last scanf is being ignored

No, it isn't being ignored.
>>and the program
exits. Can anyone see anything that im doing wrong?

Yes. I have removed from your program everything that is not relevant to
this specific problem.
>>#include <stdio.h>

int main()
{
float hours_worked = 0;
char quitVal;

scanf("%f", &hours_worked);
printf("Do you want to quit:");
scanf("%c", &quitVal);

return 0;
}

This shorter program will give you the same problem - it appears to ignore
the second scanf. But in fact it is not doing so.

Let's say your hours_worked value is 4.2 - so what do you type? It's no
good just typing 4.2 because the cursor will sit there blinking at you, in
case you're about to add a 5 to make it 4.25, yes?

So you press ENTER to signify to your command processor that you've
finished this line of data. The characters '4', '.', '2', '\n' are now
made available to your C program on its standard input stream. The scanf
munches the '4', '.', and '2' to make 4.2, but '\n' isn't part of a
floating point value, so scanf leaves it in the stream.

Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more input
from the user.

That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE. This
allows discovering failures in the scanf call.

All calls should be of the form

if (NUMBER == scanf(...)) { /* accepted */ }
else { /* rejected */ )

with suitable values for NUMBER, and possible retention of the
returned value.
There's only one type of retention in that needless post and its not
return code retention.
Sep 18 '08 #8
CBFalconer said:
Richard Heathfield wrote:
<snip>
>Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more
input from the user.

That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE.
For the sake of your students, I sincerely hope you're not a teacher.

One thing at a time, dude. Give the guy time to get over the shock of
discovering that scanf isn't as clever as he thought. There's plenty of
time later on to reveal that it's cleverer than he thought.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 18 '08 #9
Richard Heathfield <rj*@see.sig.invalidwrites:
CBFalconer said:
>Richard Heathfield wrote:

<snip>
>>Your next scanf reads a single character from stdin. Oh look, there's a
'\n' waiting - that'll do nicely, and there's no need to gather more
input from the user.

That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE.

For the sake of your students, I sincerely hope you're not a teacher.

One thing at a time, dude. Give the guy time to get over the shock of
discovering that scanf isn't as clever as he thought. There's plenty of
time later on to reveal that it's cleverer than he thought.
Please see threads on pointers for further "how to confuse students" by
denying reality and living in a dusty standard.
Sep 18 '08 #10
Richard Heathfield wrote:
CBFalconer said:
>Richard Heathfield wrote:

<snip>
>>Your next scanf reads a single character from stdin. Oh look,
there's a '\n' waiting - that'll do nicely, and there's no
need to gather more input from the user.

That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE.

For the sake of your students, I sincerely hope you're not a teacher.

One thing at a time, dude. Give the guy time to get over the shock
of discovering that scanf isn't as clever as he thought. There's
plenty of time later on to reveal that it's cleverer than he thought.
I'm not, but that doesn't remove the fact that the first thing to
do is check that returned value. If the OP realized that scanf was
reporting an error he might have figured it all out on his own.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Sep 18 '08 #11
CBFalconer said:
Richard Heathfield wrote:
>CBFalconer said:
>>Richard Heathfield wrote:

<snip>
>>>Your next scanf reads a single character from stdin. Oh look,
there's a '\n' waiting - that'll do nicely, and there's no
need to gather more input from the user.

That's all very well, but you neglect the fundamental requirement
for using scanf interactively. CHECK THE RETURNED VALUE.

For the sake of your students, I sincerely hope you're not a teacher.

One thing at a time, dude. Give the guy time to get over the shock
of discovering that scanf isn't as clever as he thought. There's
plenty of time later on to reveal that it's cleverer than he thought.

I'm not, but that doesn't remove the fact that the first thing to
do is check that returned value.
No, the first thing to do is get him to realise that scanf isn't magical,
and that the seemingly puzzling behaviour he is experiencing (an apparent
skipped call) has a rational explanation.
If the OP realized that scanf was
reporting an error he might have figured it all out on his own.
What error? Let's add the checks to the OP's code, and count the errors.
The following consists of the OP's code, with some mods to bomb out on
error (also, one comment block has been removed and one string literal has
been split to avoid unwieldy line wrap):

#include <stdio.h>
/* original comments snipped for brevity */
#include <stdlib.h/* rjh */
void bomb(const char *emsg) /* rjh */
{ fputs(emsg, stderr); exit(EXIT_FAILURE); } /* rjh */
int main()
{

int clock_number = 0;
float hours_worked = 0;
float hourly_rate = 0;
float total = 0;
char quitVal;

goto start;

start:

printf("Please enter the employee's clock number:\n");
if(1 != scanf("%i", &clock_number)) bomb("Error A\n"); /* rjh */
printf("Please enter the employee's hourly rate:\n");
if(1 != scanf("%f", &hourly_rate)) bomb("Error B\n"); /* rjh */
printf("Please enter the hours worked:\n");
if(1 != scanf("%f", &hours_worked)) bomb("Error C\n"); /* rjh */
total = hourly_rate * hours_worked;
printf("Employee Clock Number\tHourly Rate\tHours"
" Worked\tGross\n");
printf("%i\t\t\t%0.2f\t\t%0.2f\t\t%0.2f\n", clock_number,
hours_worked, hourly_rate, total);
printf("Do you want to quit:");
if(1 != scanf("%c", &quitVal)) bomb("Error D\n"); /* rjh */
goto done;

done:
fputs("No errors encountered.\n", stderr); /* rjh */
return 0;
}

me@here./foo
Please enter the employee's clock number:
123
Please enter the employee's hourly rate:
123.54
Please enter the hours worked:
42.5
Employee Clock Number Hourly Rate Hours Worked Gross
123 42.50 123.54 5250.45
No errors encountered.
So what was this error, then?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 18 '08 #12
FerrisUML wrote:
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 in
advance.
You posted an almost identical message to comp.lang.c.moderated. In
general, cross-posting should be avoided. However, if you must
cross-post, you should send a single message to both groups, rather than
a separate but identical message to each group. That way any discussion
of the issue that appears on either group can be seen by people who
monitor either group.

See my reply to your message on clcm, when it gets through the
moderation queue. The slow pace of the moderation queue is one key
reason why clcm is less popular than clc. The absence of a moderator is
the key reason so many obnoxious and off-topic messages clog up clc.

As far as I can tell, this is the only part of the program that was
missing from your clcm post:
#include <stdio.h>

/*
HOMEWORK: Assignment 1
Name: Dennis McQuilken
Class: C Programming
Date: 9.19.2008
Description: This is a C program that will calculate an employees
gross pay based on hours work and hourly rate.
*/
I should have told you that <stdio.hwas missing from you clcm message;
but I'm not as good as I should be at noticing that particular kind of
defect in other people's code.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Sep 29 '08 #13
James Kuyper wrote:
....
You posted an almost identical message to comp.lang.c.moderated. In
....
moderation queue. The slow pace of the moderation queue is one key
reason why clcm is less popular than clc. ...
I hadn't intended to provide such dramatic evidence of "the slow
pace". I posted that message on 2008-09-17, before anyone else's
responses had yet shown up, on my newsserver, though you couldn't tell
that by looking at the message headers. 11 days to get through the
moderation queue?!

Sep 29 '08 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: clusardi2k | last post by:
/* The below code on SGI will wait for you to enter 2 things, but on Linux it will only wait the first time. I can make the code work by replacing the scanf with: char data ; fgets...
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...
20
by: Sivarn | last post by:
I'm writing a program for that takes dates as input using scanf. I want to verify that the user is inputting a full 4 digits for the year. How do I do this? I know that the return value on printf...
6
by: Dawn Minnis | last post by:
Hi (running Win xp and developing using Miracle C. Running applications in windows command prompt) I'm new to the group so be gentle with me. I am currently writing a C program to perform...
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...
2
by: Bernard Liang | last post by:
In response, I have another question about the scanf family. After reading in a %d value, for instance, do they immediately wade through all subsequent whitespace until a non-whitespace character...
8
by: Neil | last post by:
Hello Just to let you know this not homework, I'm learning the language of C on my own time.. I recently tried to create a escape for user saying printf ("Do you want to continue? (y or n)");...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
0
by: FerrisUML | last post by:
Hello eveyone! Im working on the following program for an intro to C class and I seem to be having a problem with the last scanf statement, scanf("%c", &quitVal). Seems that its getting skipped...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...

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.