Wo ist der Fehler

B

Benedict Ernst

Hallo NG,
wo ist der Fehler in diesem 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();
}


finde ihn nicht^^ hab aber auch gerade erst angefangen C zu lernen.

mfg benedict e
 
M

Michael Mair

Benedict said:
Hallo NG,
wo ist der Fehler in diesem 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();
}



finde ihn nicht^^ hab aber auch gerade erst angefangen C zu lernen.

Note: There also is
Apart from that: Without a proper description of expected and erroneous
behaviour it is hard to help you.

General remarks see my other reply (<[email protected]>).


Cheers
Michael
 
M

Martin Ambuhl

Benedict said:
Hallo NG,
wo ist der Fehler in diesem C code?

This is not a well-formed question. You should tell us what's going
wrong, so we don't have to guess what your question really is.

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

int wait()
{
/* just tell the user to hit the enter key instead of this silliness */
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IOFBF, BUFSIZ);
#if 0
/* mha: This fuction does not return the value it claims to. I have
replaced the following line. */
getchar();
#endif
return getchar(); /* mha */
}

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

/* mha: the broken call to scanf is replaced, with the prompt moved
where it belongs and the output stream appropriately flushed. */
printf("Moechten sie nun\n"
" [1] Das programm beenden?\n"
" [2]Eine Quadratwurzel berechen?\n"
"Bitte 1 oder 2 wählen: "); /* mha */
fflush(stdout); /* mha */
scanf("%g", &z); /* mha: note than scanf isn't a
particularly good idea here */
/* mha: also note that comparing floating point values for equality
is not a good idea. This is covered in the FAQ. There is no
excuse for declaring z as a float, anyway. */
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 /* mha: pointless 'if (z != 1 && z != 2)' removed */
printf("Fehlerhafte eingabe, das programm wird beendet.");
wait();
/* This function does not return the value it claims to. I have
added the following line. */
return 0;
}
 
K

Kenny McCormack

This is not a well-formed question. You should tell us what's going
wrong, so we don't have to guess what your question really is.

True. The "question" is gibberish.
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top