468,484 Members | 1,559 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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 3798
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by clusardi2k | last post: by
5 posts views Thread by Eduardo Olivarez | last post: by
20 posts views Thread by Sivarn | last post: by
33 posts views Thread by Lalatendu Das | last post: by
2 posts views Thread by Bernard Liang | last post: by
8 posts views Thread by Neil | last post: by
reply views Thread by FerrisUML | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.