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"