# Thread: No errors but problems??

1. ## No errors but problems??

Hello all, I'm at that lovely time in a mathematical learning experience in which you are forced to submit to memorizing the quadratic formula, which is not only despicably confusing but long and painful to work with. I decided to write a program that would do quadratic equations for me, and the final code (debugged and everything) is as follows:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float a;
float b;
float c;
float d;
float e;
float f;

int main()
{
printf("The format for a quadratic equation is ax^2+bx+c=0.\n");
printf("This should solve a quadratic equation.\n");
printf("Enter the A variable.\n");
scanf("%f",a);
printf("Enter the B variable.\n");
scanf("%f",b);
printf("Enter the C variable.\n");
scanf("%f",b);
if(2*a!=0)
{
d=sqrt(b*b-4*a*c);
e=(-b+d)/(2*a);
f=(-b-d)/(2*a);
printf("X equals %f and %f.\n",e,f);
}
else
{
printf("No real number solution.\n");
}
system("PAUSE");
return 0;
}```
All right, so after compiling and running this I percieve no warnings or errors from the compilers, but after entering the first number and pressing enter, it says that the program has performed an illegal operation. Why?
Thanks a lot
Raytro2

2. This :
>scanf("%f",a);
should be
>scanf("%f",&a);

Do the same for the other scanf()'s.

3. you forgot the address operator (&) in your scanf statements

forgeting to precede the variable in a scanf statement with an ampersand produces a run-time error not a compiling error

4. Great, thanks Hammer. That certainly helped.
Why though? Is that only a rule with integers, doubles, floats, etc.? That '&' certainly isn't necessary with 'char's.
Alas! Although that got rid of the fact that the program performs an illegal system error, it has new problems. Any numbers put into it return the same answers: 0.000000 and 0.000000. It's fine that there are two answers, that's supposed to happen, but there should be some other things than zero there! I've tested several combinations of 1, 2, and 3, and each time I see fourteen zeroes...
Raytro2

5. Originally posted by raytro2
Great, thanks Hammer. That certainly helped.
Why though? Is that only a rule with integers, doubles, floats, etc.? That '&' certainly isn't necessary with 'char's.
The function expects you to be passing it pointers. If you don't have a pointer, then you must pass the address of a variable instead (which in effect is the exact same thing as a pointer to the variable). Yes, you must do this for characters.

The only time you do not need to use the & sign is:
a) When you pass it a pointer directly.
b) When you pass it the name of an array, which is actually a pointer to the first element of the array.

The reason you have to do it for all variable types is because that is how the function works. You must pass to a function the correct parameters. Otherwise it won't work right.

Quzah.

6. >That '&' certainly isn't necessary with 'char's
Oh yes it is All parameters for scanf() must be pointers. I expect you mean to say "char arrays", which is different from a single char.

For example, this is correct:

>>char myname[10];
>>scanf("%s", myname);

and so is this:

>>char letter;
>>scanf("%c", &letter);

Note the second one is a single char, not an array of char.

Doh! beat again.

7. Actually, the ampersand is necessary with chars. I completely forgot about it, but upon going back to my older programs, I see that I actually have been using them and it is mandatory to use them in scanf statements. Sorry!
Raytro2

8. Okay, so the ampersand is necessary with single char's but not with char arrays. Will it give me an error to use the ampersand with a char array? Just a thought, thanks for the clarification Hammer.
Raytro2

9. All right, but I'm still getting problems. Whatever numbers I use as input, the two numbers I get as output are always 0.000000 and 0.000000. I'm not sure why, maybe it's my mathematical formatting? Here:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float a,b,c,d,e,f;

int main()
{

printf("The format for a quadratic equation is ax^2+bx+c=0.\n");
printf("This should solve a quadratic equation.\n");
printf("Enter the A variable.\n");
scanf("%f",&a);
printf("Enter the B variable.\n");
scanf("%f",&b);
printf("Enter the C variable.\n");
scanf("%f",&b);
if(2*a!=0)
{
d=sqrt(b*b-4*a*c);
e=(-b+d)/(2*a);
f=(-b-d)/(2*a);
printf("X equals %f and %f.\n",&e,&f);
}
else
{
printf("No real number solution.\n");
}
system("PAUSE");
return 0;
}```
If any of you know why this doesn't work please tell me why.
Thanks a lot...
Raytro2

10. here's your problem:

Code:
`printf("X equals %f and %f.\n",&e,&f);`
you're passing the address of e and f. you need to pass their values. remove the ampersand.

Popular pages Recent additions