Wo ist der Fehler

B

Benedict Ernst

Hi NG,
where is my mistake in this c code?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int wait ()
{
setvbuf(stdin,NULL,_IONBF,0);
setvbuf(stdin,NULL,_IOFBF,BUFSIZ);
getchar();
}

int main()
{
float x, y, z;

printf("Moechten sie nun\n [1Das programm beenden?\n [2]Eine Quadratwurzel berechen?\n");
scanf("Bitte 1 oder 2 wählen: %g",&z);
if (z == 1) wait();
else if (z == 2)
{
printf("Bitte eine float-Zahl:\n");
scanf("%g",&x);

if ( x >= 0 )
{
y = sqrt(x);
printf("Quadratwurzel: %g",y);
}
else
{
printf("Aus negativen Zahlen kann keine Wurzel berechnet werden.\n");
wait();
}
}
else if (z != 1 && z != 2) printf("Fehlerhafte eingabe, das programm wird beendet.");
wait();
}


i dont see my mistake but i learn c since few days

benedict e.
 
U

Ulrich Eckhardt

Benedict said:
where is my mistake in this c code?

Your code is wrongly indented making it hard to check for errors in the
control flow. Other than that, what does it do and what did you expect it
do do? Lastly, there is comp.lang.learn.c-c++...

Uli
 
M

Michael Mair

Benedict said:
Hi NG,
where is my mistake in this c code?

setbuf() and setvbuf() may be called only _once_ - as first action
after opening the stream (or, in the case of stdin, after program start)
and before the first other action on the stream.

Just as an aside: There is nearly never a good reason for usage of
float instead of double. Rounding errors and error propagation
affect the digits relevant for your purposes relatively fast when
using float. This is just a rule-of-thumb, as the C standard does
not guarantee anything about the actual size, representation etc.
of floating point types except of some vague numerical limits which
could be fulfilled by one comparatively small floating point type.
printf("Moechten sie nun\n [1Das programm beenden?\n [2]Eine
Quadratwurzel berechen?\n");
scanf("Bitte 1 oder 2 wählen: %g",&z);
if (z == 1) wait();
else if (z == 2)

BTW: Usually, one treats if-else if-else chains like
if
else if
....
else
instead of
if
else if
....
else

Check the return value of all scanf() type functions, always.
Otherwise, you do not know whether scanning succeeded.

"else" is completely sufficient and works even if you have additional
values of z marked down as special values.
i dont see my mistake but i learn c since few days

Your main mistake is mixing scanf() and getchar().
Have a look at the comp.lang.c FAQ posted regularly here or found at
http://www.eskimo.com/~scs/C-faq/top.html
(consider reading the text version -- it is more up-to-date than the
HTML version).
It is usually better to use fgets() plus specialized conversion
functions like strtod(), strtol(), ... or sscanf(). fgets() is not
the best function for the purpose; just have a look at a couple of
past threads about this within the last two months or so.


Cheers
Michael
 
E

Emmanuel Delahaye

Benedict Ernst wrote on 14/08/05 :
Hi NG,
where is my mistake in this c code?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int wait ()
{
setvbuf(stdin,NULL,_IONBF,0);
setvbuf(stdin,NULL,_IOFBF,BUFSIZ);
getchar();
}
Useless...
int main()
{
float x, y, z;
printf("Moechten sie nun\n [1Das programm beenden?\n [2]Eine
Quadratwurzel berechen?\n");
scanf("Bitte 1 oder 2 wählen: %g",&z);

"Bitte 1 oder 2 wählen: " is not doing what you think... Use a "prompt"
:

printf ("Bitte 1 oder 2 wählen: ");
fflush (stdout);

also, the test of the value returned by scanf is missing... But
beginners (well, not only) should not use scanf() or fscanf(). Too
complicated. Use fgets() to get a line and a conversion function like
sscanf(), strtol(), strtoul() or strtod()...

The '==' operator doesn't work with floating points. Use a simple int
for that... BTW, you should use a switch-case code structure instead of
these ugly if-else if...

Try this and ask for details if you need.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main (void)
{
int z;
printf ("Moechten sie nun\n [1Das programm beenden?\n [2]Eine
Quadratwurzel berechen?\n");
{
int n;
do
{
char s[4];
printf ("Bitte 1 oder 2 waehlen: ");
fflush (stdout);

fgets (s, sizeof s, stdin);
n = sscanf (s, "%d", &z);
}
while (n != 1);
}
switch (z)
{
case 1:
break;
case 2:
{
float x, y;

{
int n;
do
{
char s[16];
printf ("Bitte eine float-Zahl: ");
fflush (stdout);

fgets (s, sizeof s, stdin);
n = sscanf (s, "%g", &x);
}
while (n != 1);
}


if (x >= 0)
{
y = sqrt (x);
printf ("Quadratwurzel: %g\n", y);
}
else
{
printf ("Aus negativen Zahlen kann keine Wurzel berechnet
werden.\n");
}
}
break;
default:
printf ("Fehlerhafte eingabe, das programm wird beendet.");
}
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
 
K

Keith Thompson

Emmanuel Delahaye said:
The '==' operator doesn't work with floating points.

That's not *quite* true. Given:

float z;
scanf("%g", &z);

if the user enters "1" or "1.0", I would expect the expression (z ==
1.0) to be true. It's not absolutely guaranteed, but I'd be surprised
if any implementation didn't behave this way.

But more generally, it's true that it's rarely safe to use "==" on
floating-point values. Few people really understand floating-point.
More people *think* they understand it (I may be in that group
myself).
Use a simple int
for that... BTW, you should use a switch-case code structure instead
of these ugly if-else if...

And in the sample program that was posted, z is used only to
distinguish between two choice, 1 and 2, so using a floating-point
variable really doesn't make any sense. I'd probably prompt for a
character, perhaps 'y' or 'n'.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top