Thread: No errors but problems??

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    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. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This :
    >scanf("%f",a);
    should be
    >scanf("%f",&a);

    Do the same for the other scanf()'s.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    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. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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.

    [edit]Doh! beat again.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    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. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    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. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    Angry

    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. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    31
    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 subscribe to a feed

Similar Threads

  1. linker errors
    By Wick in forum C++ Programming
    Replies: 4
    Last Post: 09-06-2003, 12:21 AM
  2. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM
  3. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  4. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM