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. 4 1738
Benedict Ernst wrote: 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
Benedict Ernst wrote: 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(); }
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.
int main() { float x, y, z;
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
{ printf("Bitte eine float-Zahl:\n"); scanf("%g",&x);
Check the return value of all scanf() type functions, always.
Otherwise, you do not know whether scanning succeeded.
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.");
"else" is completely sufficient and works even if you have additional
values of z marked down as special values.
wait(); }
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-Mail: Mine is an /at/ gmx /dot/ de address.
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()...
if (z == 1) wait();
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...
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(); }
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++
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> writes:
[...] 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'.
--
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Armin Irger |
last post by:
Hi,
i'am running a debian sarge with the delivered apache2 mysql and php4.
The file "mitarbeiter_eingabe.php" gets the data over a html <FORM>
and send it to...
|
by: mibispam |
last post by:
Hallo,
kann mir vielleicht jemand erklären, wieso ich neue Einträge in eine
Listbox nicht per MeineListbox.additem hinzufügen kann?? Ich bekomme
da immer wieder einen Laufzeitfehler. In...
|
by: Benedict Ernst |
last post by:
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);
|
by: Marko G. |
last post by:
Hallo,
ich versuche nun seit einiger Zeit, auf einem Active-Directory Server
das Passwort eines Benutzers zu setzen. Hier der verwendete Quelltext:
------------
Dim entry As DirectoryEntry =...
|
by: Martin Himmel |
last post by:
Hello Newsgroup,
I want to build xerces.lib (actually dynamical, but as there are many linker
errors using the dll, the static .lib version)
ich möchte die xercesc-lib erstellen.
eigentlich die...
|
by: Martin Himmel |
last post by:
Hello Newsgroup,
I want to build xerces.lib (actually dynamical, but as there are many linker
errors using the dll, the static .lib version)
ich möchte die xercesc-lib erstellen.
eigentlich die...
|
by: Peter Hansch |
last post by:
Hallo,
habe ein VS02 Projekt konvertiert auf VS05. Es handelt sich um eine Klasse
zur erzeugung eines Screenshots
Dabei tritt oben genannter Fehler auf an.
Und zwar bei markierter Stelle:
...
|
by: Patrick |
last post by:
Hello,
I'm currently trying the OpenSSL Library, but I got some problems. I
want to create a server and client application that communicate
through the OpenSSL API, but this code doesn't work.
I...
|
by: locy |
last post by:
i try to compile this programm useing pluto,it shows me error .where schould the fehler be?
#include <"stdio.h">
int main()
{
int counter;
int even=0;
int b= 7,int h= 10;
if h+ = b % 10;
|
by: id10t error |
last post by:
Hello,
I am going to be using a Symbol WT4090 to scan items. I need to
printer a tag from the Zebra ql320 plus. I am trying to do this is
Visual basic 2005. Does anyone know and good site to...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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...
|
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...
|
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: 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,...
|
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...
|
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...
| |