Shoutbox

Squadratic formula in c++ - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Squadratic formula in c++ (/showthread.php?tid=65080)

Squadratic formula in c++ by somelauw on 08-18-2006 at 04:04 PM

I am a beginner c++ programmer and this is my first program.
I enter:

quote:
a=5
b=10
c=5
but it shows
discriminant=-6,9e-018
I thought 10*10-4*5*5=0?

Here my code:
code:
#include <math.h>
#include <stdio.h>

int wortelformule()
{
   float a,b,c,d,x1,x2;
   printf("ax˛+bx+c=0 \n");
   printf("The value of a is ");
   scanf("%g",&a);
   printf("The value of b is ");
   scanf("%g",&b);
   printf("The value of c is ");
   scanf("%g",&c);
   d=pow(b,2)-4*a*c;
   printf("\n \nThe discriminant is %g \n",d);
   x1=(-b+pow(d,0.5))/(2*a);
   x2=(-b-pow(d,0.5))/(2*a);
   printf("The first answer is %g \n",x1);
   printf("The second answer is %g \n \n",x2);
   wortelformule();
}

main()
{
      wortelformule();
}


RE: Squadratic formula in c++ by Adeptus on 08-19-2006 at 02:17 AM

Using your code, cut-n-pasted:

quote:
~ $ nano -w wf.c
~ $ gcc -o wf -lm wf.c
~ $ ./wf
ax˛+bx+c=0
The value of a is 5
The value of b is 10
The value of c is 5


The discriminant is 0
The first answer is -1
The second answer is -1
Looks good to me.  Your code is fine.  I doubt there could be a problem with your input, given that typing 5, 10, 5 is easy enough.  That leaves a problem with your compiler.

So, what platform and compiler are you using?
RE: Squadratic formula in c++ by somelauw on 08-19-2006 at 10:59 AM

I use "windows XP home edition"(no service pack) as platform and the program I use is "Dev-C++"

Also a minor problem the ˛ isn't shown correctly. Is there a way too show that 2 in superscript.


RE: Squadratic formula in c++ by Adeptus on 08-19-2006 at 03:03 PM

I don't know why you are getting incorrect results.  The reason I compiled your code was because I looked it over and couldn't find any problems -- and it works fine here, as expected.

I might download Dev-C++ and see if I can duplicate your problem later this evening.

As for your other question, try:

printf("ax\375+bx+c=0 \n");

375 is the octal value for the ˛ character in the PC OEM character set used by Windows console.


RE: Squadratic formula in c++ by somelauw on 08-20-2006 at 09:23 AM

Thank you for your efforts. Maybe there is a compiler bug.
It's obvious you are living in another time zone because you spoke about later this evening. In my country it was posted afternoon.
That \375 worked as replacement for ˛ by the way.