473,383 Members | 1,739 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,383 software developers and data experts.

Trouble with scanf("%c", &answer);

Hello

I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and
ends the program. Does scanf have problems with "%c" or is it the operating
system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == 'Y');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com

Nov 14 '05 #1
14 2690

"Peter Mount" <in**@petermount.au.com> wrote in message
news:40**********@news.iprimus.com.au...
Hello

I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and ends the program. Does scanf have problems with "%c" or is it the operating system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == 'Y');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com


Have a look at the FAQ - this is answered there nicely.
HINT: Your previous scanf() hasnt cleared the input buffer.
Allan
Nov 14 '05 #2
Hello

I got it to work by changing line 20 to read:

scanf("%s", &answer);

Is this because I'm using Windows 98SE? I would have thought "scanf("%c",
&answer);" would have been correct as "answer" was declared as a char
variable.

Also, where is the FAQ? I'm using Outlook Express for my newsgroups so I
don't see the FAQ and I would like to read the FAQ if I could

Thanks

Peter Mount
in**@petermount.au.com

"Allan Bruce" <al*****@TAKEAWAYf2s.com> wrote in message
news:ca**********@news.freedom2surf.net...

"Peter Mount" <in**@petermount.au.com> wrote in message
news:40**********@news.iprimus.com.au...
Hello

I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I run the program in cygwin on Windows 98SE it skips that line completely

and
ends the program. Does scanf have problems with "%c" or is it the

operating
system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == 'Y');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com


Have a look at the FAQ - this is answered there nicely.
HINT: Your previous scanf() hasnt cleared the input buffer.
Allan

Nov 14 '05 #3
Peter Mount wrote:

I'm having trouble with " scanf("%c", &answer);" on line 20 below.
When I run the program in cygwin on Windows 98SE it skips that
line completely and ends the program. Does scanf have problems
with "%c" or is it the operating system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do {
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
scanf(" %c", &answer); 21
22 } while(answer == 'Y');
23
24 return 0;
25 }
26


However you will have problems using scanf in interactive files.
You have also committed the fatal mistake of not checking the
scanf return value. The printf()s should be followed by
fflush(stdout);

You can handle most of these things with a couple of wrapper
functions:

#include <stdio.h>

void prompt(char *line)
{
printf(line);
fflush(stdout);
}

void getfloat(float *fv, char *promptln)
{
do {
prompt(promptln);
} while (1 != scanf("%f", fv))'
}

int getonechar(char *promptln)
{
int ch;

if (1 != scanf(" %c", ch)) return 0;
return (unsigned char) ch;
}

/* after which your routine reduces to: */

int main()
{
float width, length, area;

do {
getfloat(&width, "\nEnter width of the rectangle ");
getfloat(&length, "\nEnter length of the rectangle ");
area = length * width;
printf("\nArea of the rectangle is %.2f", area);
} while('Y' == toupper(getonechar(
"\nContinue? - Enter Y for yes, N for no ")));
return 0;
}

Notice how subroutines simplify and clarify. However that still
has all sorts of nasty problems for interactive use, because of
the use of scanf. Try responding to the first prompt with "2 3 Y"
(without the quotes) for example.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #4
Hello

Thanks for that

Peter Mount
in**@petermount.au.com

"CBFalconer" <cb********@yahoo.com> wrote in message
news:40***************@yahoo.com...
Peter Mount wrote:

I'm having trouble with " scanf("%c", &answer);" on line 20 below.
When I run the program in cygwin on Windows 98SE it skips that
line completely and ends the program. Does scanf have problems
with "%c" or is it the operating system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do {
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);


scanf(" %c", &answer);
21
22 } while(answer == 'Y');
23
24 return 0;
25 }
26


However you will have problems using scanf in interactive files.
You have also committed the fatal mistake of not checking the
scanf return value. The printf()s should be followed by
fflush(stdout);

You can handle most of these things with a couple of wrapper
functions:

#include <stdio.h>

void prompt(char *line)
{
printf(line);
fflush(stdout);
}

void getfloat(float *fv, char *promptln)
{
do {
prompt(promptln);
} while (1 != scanf("%f", fv))'
}

int getonechar(char *promptln)
{
int ch;

if (1 != scanf(" %c", ch)) return 0;
return (unsigned char) ch;
}

/* after which your routine reduces to: */

int main()
{
float width, length, area;

do {
getfloat(&width, "\nEnter width of the rectangle ");
getfloat(&length, "\nEnter length of the rectangle ");
area = length * width;
printf("\nArea of the rectangle is %.2f", area);
} while('Y' == toupper(getonechar(
"\nContinue? - Enter Y for yes, N for no ")));
return 0;
}

Notice how subroutines simplify and clarify. However that still
has all sorts of nasty problems for interactive use, because of
the use of scanf. Try responding to the first prompt with "2 3 Y"
(without the quotes) for example.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #5
Hi Peter,
Just put in fflush(stdin) before line 20.This ensures that the
memory used for answer is initialized before using it again.I think
that will work.

Thanks.
Shuvodeep.

"Peter Mount" <in**@petermount.au.com> wrote in message news:<40**********@news.iprimus.com.au>...
Hello

I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and
ends the program. Does scanf have problems with "%c" or is it the operating
system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == 'Y');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com

Nov 14 '05 #6

On Mon, 14 Jun 2004, "Shuvodeep" wrote:

Hi Peter,
Just put in fflush(stdin) before line 20.This ensures that the
memory used for answer is initialized before using it again.I think
that will work.


Of course it won't; 'fflush' has nothing to do with input.
And "Shuvodeep" shouldn't be top-posting.

-Arthur
Nov 14 '05 #7
Shuvodeep wrote:
Hi Peter,
Just put in fflush(stdin) before line 20.This ensures that the
memory used for answer is initialized before using it again.I think
that will work.


If I'm not mistaken you cannot fflush an input stream. It's implementation
defined.

A better solution is to use proper input such as a fgets/sscanf combo. That
has well defined behaviour and will sync to newlines [or filled
buffers... ;-)].

Tom
Nov 14 '05 #8
Peter Mount wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message
Peter Mount wrote:

I'm having trouble with " scanf("%c", &answer);" on line 20 below.
When I run the program in cygwin on Windows 98SE it skips that
line completely and ends the program. Does scanf have problems
with "%c" or is it the operating system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do {
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);


scanf(" %c", &answer);
21
22 } while(answer == 'Y');
23
24 return 0;
25 }
26


However you will have problems using scanf in interactive files.
You have also committed the fatal mistake of not checking the
scanf return value. The printf()s should be followed by
fflush(stdout);

You can handle most of these things with a couple of wrapper
functions:

#include <stdio.h>

void prompt(char *line)
{
printf(line);
fflush(stdout);
}

void getfloat(float *fv, char *promptln)
{
do {
prompt(promptln);
} while (1 != scanf("%f", fv))'
}

int getonechar(char *promptln)
{
int ch;

if (1 != scanf(" %c", ch)) return 0;
return (unsigned char) ch;
}

/* after which your routine reduces to: */

int main()
{
float width, length, area;

do {
getfloat(&width, "\nEnter width of the rectangle ");
getfloat(&length, "\nEnter length of the rectangle ");
area = length * width;
printf("\nArea of the rectangle is %.2f", area);
} while('Y' == toupper(getonechar(
"\nContinue? - Enter Y for yes, N for no ")));
return 0;
}

Notice how subroutines simplify and clarify. However that still
has all sorts of nasty problems for interactive use, because of
the use of scanf. Try responding to the first prompt with "2 3 Y"
(without the quotes) for example.


Thanks for that


Please do not top post. Here are slightly better behaved versions
of the subroutines I posted. I actually tried these ones. They
still have nasty interactive behaviour. They require #include
<ctype.h>.

int flushln(FILE *f)
{
int ch;

while ((EOF != (ch = getc(f)) && ('\n' != ch))) continue;
return ch;
}

void getfloat(float *fv, const char *promptln)
{
do {
prompt(promptln);
} while ((1 != scanf("%f", fv)) && (EOF != flushln(stdin)));
}

int getonechar(const char *promptln)
{
char ch;

prompt(promptln);
if (1 != scanf(" %c", &ch)) return 0;
return (unsigned char) ch;
}

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #9
Shuvodeep wrote:

Just put in fflush(stdin) before line 20.This ensures that the
memory used for answer is initialized before using it again. I
think that will work.


No, don't do that. fflush is only defined for output files. And
please do not top-post, it is considered rude in this newsgroup.

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #10
On Mon, 14 Jun 2004 15:46:53 GMT, Tom St Denis <to*@securescience.net>
wrote in comp.lang.c:
Shuvodeep wrote:
Hi Peter,
Just put in fflush(stdin) before line 20.This ensures that the
memory used for answer is initialized before using it again.I think
that will work.
If I'm not mistaken you cannot fflush an input stream. It's implementation
defined.


No, it is undefined. The term "undefined behavior" has a specific
definition in the C standard. The term "implementation-defined" has a
different and incompatible definition in the C standard.

Anything which the standard claims produces "undefined behavior" does
not and cannot be "implementation-defined".
A better solution is to use proper input such as a fgets/sscanf combo. That
has well defined behaviour and will sync to newlines [or filled
buffers... ;-)].


Now that is a good suggestion.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #11
SDZ
"Peter Mount" <in**@petermount.au.com> wrote in message news:<40**********@news.iprimus.com.au>...
Hello

I'm having trouble with " scanf("%c", &answer);" on line 20 below. When I
run the program in cygwin on Windows 98SE it skips that line completely and
ends the program. Does scanf have problems with "%c" or is it the operating
system I'm using?

1 #include <stdio.h>
2
3 int main()
4 {
5 float width, length, area;
6 char answer;
7
8 do{
9 printf("\nEnter width of the rectangle ");
10 scanf("%f", &width);
11 printf("\nEnter length of the rectangle ");
12 scanf("%f", &length);
13
14 area = length * width;
15
16 printf("\nArea of the rectangle is %.2f", area);
17
18 printf("\nContinue? - Enter Y for yes, N for no ");
19
20 scanf("%c", &answer);
21
22 }while(answer == 'Y');
23
24 return 0;
25 }
26

Thanks

Peter Mount
in**@petermount.au.com

The problem is that the scanf on line 12 does not consume the last
character pressed after entering the length. That left over character
is consumed by the scanf on line 20. A simple patch would be this:

/* Declare a variable cahr dummy */
/* Dummy will consume the character and answer will have the character
typed - either Y or N */

scanf("%c %c", &dummy, &answer);

Hope this helps.

-SDZ
Nov 14 '05 #12

"Peter Mount" <in**@petermount.au.com> wrote in message
news:40**********@news.iprimus.com.au...
Hello

I got it to work by changing line 20 to read:

scanf("%s", &answer);

Is this because I'm using Windows 98SE? I would have thought "scanf("%c",
&answer);" would have been correct as "answer" was declared as a char
variable.

Also, where is the FAQ? I'm using Outlook Express for my newsgroups so I
don't see the FAQ and I would like to read the FAQ if I could

Thanks


http://www.eskimo.com/~scs/C-faq/top.html
Nov 14 '05 #13
On Mon, 14 Jun 2004, CBFalconer <cb********@yahoo.com> wrote:
Peter Mount wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message
> Peter Mount wrote:
Please do not top post. Here are slightly better behaved versions
of the subroutines I posted. I actually tried these ones. They
still have nasty interactive behaviour. They require #include
<ctype.h>.

int flushln(FILE *f)

int flushln(FILE* f, int prec){
int ch; int ch=prec;
while ((EOF != (ch = getc(f)) && ('\n' != ch))) continue; if( prec!='\n' && prec!=EOF )
while( EOF!=(ch = getc(f)) && '\n'!=ch )
return ch;
}
Is this the real flush?
void getfloat(float *fv, const char *promptln)
{ int c; do {
prompt(promptln);
} while ((1 != scanf("%f", fv)) && (EOF != flushln(stdin)));

while( (c = scanf("%f", fv))!=1 &&
c!=EOF && EOF!=flushln(stdin,'a') );

for you if c==EOF there will be the ask for input (flushln)
What do you say?
Nov 14 '05 #14
Peter Mount <in**@petermount.au.com> wrote:
Hello

I got it to work by changing line 20 to read:

scanf("%s", &answer);
which is not the correct sollution but just appears to be doing what
you're expecting right now.

Is this because I'm using Windows 98SE?
No. The problem will araise on all plattforms with a correct C
implementation.
I would have thought "scanf("%c",
&answer);" would have been correct as "answer" was declared as a char
variable.
Yes and no.

Look, the problem in not related to how scanf interprets the input, but
what is in the inputstream when your program reaches this call.

Also, where is the FAQ? I'm using Outlook Express for my newsgroups so I
don't see the FAQ and I would like to read the FAQ if I could


http://www.eskimo.com/~scs/C-faq/top.html

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #15

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

Similar topics

145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
24
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the...
46
by: TTroy | last post by:
Hi, I'm just wondering why people/books/experts say "the function returns a pointer to.." or "we have to send scanf a pointer to.." instead of "the function returns the address of.." or "we have...
3
by: news | last post by:
Hi I have problem: My program reads 3 digits from keyboard using scanf("%d", &a) then calculate it,everything is OK until I put "1,2,3,4,5,6,7,8,9,0" but if I put "char " like "a,b,c,d etc" my...
18
by: Martin Jørgensen | last post by:
Hi, Today I got a really strange problem... I've made myself a data-file and I read in data from that file.... When I read something like this line: 03 04 05, 00 04 01, 05 03 07, 08 03...
9
by: chutsu | last post by:
hi I got a simple program, and I was wondering how do you check if the string in an array = a string. For example if I put "APPLE" in array Array then how can I check it with a if statement. if...
11
by: LionelAndJen | last post by:
I have an XML file that has a comment field in which the data provider, very kindly, already uses "&quot;" when writing "doesn't", I have doesn&apos;t . it's PERFECT, because that xml is then fed to...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.