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

getc/fgets

Below is a fragment from a program that calculates statistics on
x,y data. I want the user to be able to predict one or more
predicted values of y from x, given the line of best fit. I have
a procedural problem.
predict:
printf("\npredict y? (y/n): ");
if((getc(stdin)=='n')) exit(EXIT_SUCCESS);
//if((fgets(response, 1, stdin)=="n")) exit(EXIT_SUCCESS);
else
{
printf("\nenter x: ");
scanf("%lf", &xdatum);
printf("\ny = %f\n", y_int + (slope * xdatum));
fflush(stdin);
goto predict;
}

It works fine using getc, but when I use fgets execution jumps
directly into the else{} block and prompts for x without waiting
for user response to the y/n prompt. What am I doing wrong?

Richard
Jul 31 '07 #1
5 2539
santosh wrote:
Richard Weeks wrote:
>Below is a fragment from a program that calculates statistics on
x,y data. I want the user to be able to predict one or more
predicted values of y from x, given the line of best fit. I have
a procedural problem.
predict:
printf("\npredict y? (y/n): ");

Unless output is terminated with a newline, it might appear immediately on
the screen.
That should read;

.... might *not* appear immediately on the screen.

An alternative is to use fflush(stdout) just after the printf.

Jul 31 '07 #2
On Aug 1, 9:46 am, Richard Weeks <rwe...@nomail.netwrote:
//if((fgets(response, 1, stdin)=="n")) exit(EXIT_SUCCESS);

It works fine using getc, but when I use fgets execution jumps
directly into the else{} block and prompts for x without waiting
for user response to the y/n prompt. What am I doing wrong?
>From the C standard (ISO/IEC 9899:1999 7.19.7.2):
[#2] The fgets function reads at most one less than the
number of characters specified by n from the stream pointed
to by stream into the array pointed to by s.

My guess is that since you specified n=1, fgets
determines that it needs to read at most 0
characters. Since doing nothing is the same
as reading 0 characters, it does nothing.

Also, as santosh pointed out, your method of
checking the result is not correct.


Jul 31 '07 #3
Richard Weeks <rw****@nomail.netwrites:
Below is a fragment from a program that calculates statistics on x,y
data. I want the user to be able to predict one or more predicted
values of y from x, given the line of best fit. I have a procedural
problem.
predict:
printf("\npredict y? (y/n): ");
Why do you print a newline at the beginning of the prompt? If you
just want a blank line above the prompt, that's fine; if you think
it's required, it isn't.

You should add 'fflush(stdout);' here to ensure that your prompt
appears. It may not be necessary on most systems, but stdout could be
line-buffered.
if((getc(stdin)=='n')) exit(EXIT_SUCCESS);
//if((fgets(response, 1, stdin)=="n")) exit(EXIT_SUCCESS);
You can't use the "==" operator to compare strings, at least not
meaningfully.

Take a look at the definition of fgets from the standard:

The fgets function reads at most one less than the number of
characters specified by n from the stream pointed to by stream
into the array pointed to by s.

Your fgets call reads at most zero characters.
else
{
printf("\nenter x: ");
Again, 'fflush(stdout);'.
scanf("%lf", &xdatum);
printf("\ny = %f\n", y_int + (slope * xdatum));
fflush(stdin);
Just what do you think 'fflush(stdin);' is going to do?

It invokes undefined behavior.
goto predict;
Style point: this would be better written as a loop.
}
[snip]

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Please read it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 31 '07 #4
On Jul 31, 4:46 pm, Richard Weeks <rwe...@nomail.netwrote:
It works fine using getc, but when I use fgets execution jumps
directly into the else{} block and prompts for x without waiting
for user response to the y/n prompt. What am I doing wrong?
All previously mentioned points aside, I have to wonder: why do you
want to use fgets() when all you're doing at that point is reading one
character anyways?

Aug 1 '07 #5
On Wed, 01 Aug 2007 03:29:53 +0530, santosh wrote:
Richard Weeks wrote:
[snip]
> if((getc(stdin)=='n')) exit(EXIT_SUCCESS);

getc can return EOF on end-of-file or error. EOF is not a valid character
value. You must test for EOF and *then* test against 'n' and 'N'.
What the hell? If getc(stdin) return EOF, the program will do
exactly the same thing as if it returned '\n', ' ', or anything
else other than 'n'.
A better thing to do would be islower(getchar()) != 'y', so the
default would be "no".
But (k = getc(stdin)) != EOF && k == 'n' will always do the exact
same thing as getc(stdin) == 'n', except that it needs an
otherwise useless temporary variable.
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 2 '07 #6

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

Similar topics

4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
8
by: Bill Cunningham | last post by:
Would getc and ungetc be the best and most simple what to parse expressions for a parser? Bill
11
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an...
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
19
by: mailursubbu | last post by:
HI, Below is my program. I compiled it through g++. Now strange thing is, getc is not reading the data instead its printing the previously read data ! Please some one let me know whats wrong. ...
7
by: hzmonte | last post by:
My C program has the following: static int skip_space(FILE *fp, int *line, int c) { int i = 0; if(feof(fp)) { printf("in skip feof ...\n"); } printf("in skip start fp=%p line=%d c=%d\n", fp,...
1
momotaro
by: momotaro | last post by:
Please someone to explain me the defference between the two functions fgets and getc in very long detail :) thx!
32
by: vippstar | last post by:
Assuming all the values of int are in the range of unsigned char, what happends if getc returns EOF? Is it possible that EOF was the value of the byte read? Does that mean that code aiming for...
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:
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.