Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

can sombody find my error

Options
  • 12-03-2008 11:12am
    #1
    Registered Users Posts: 7,525 ✭✭✭


    this is my code its to find the hypotenuse of the triangle, with adjacent and opposite given??
    it keeps saying illegal use of float in main, ybh im not sure what to use, float integer or char??:confused:


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

    int main(void)

    {

    /* declarations */


    float Adjacent, Opposite;

    char Hypotenuse;


    /* input data */

    printf("Welcome to Triangle calculation program!\n");
    printf("Please enter the opposite side of your rectangle:\t");
    scanf("%F",&Opposite);

    printf("Please enter the adjacent side of your rectangle:\t");
    scanf("%F", &Adjacent);

    /*calculation*/

    Hypotenuse=sqrt(Adjacent^2+Opposite^2);

    /*output*/

    printf("\nThe Hypotenuse of the triangle is:\t%f metres");

    /*exit*/

    printf("thank you for using this great program, :\n");


    }


Comments

  • Registered Users Posts: 1,990 ✭✭✭lynchie


    kona wrote: »
    this is my code its to find the hypotenuse of the triangle, with adjacent and opposite given??
    it keeps saying illegal use of float in main, ybh im not sure what to use, float integer or char??:confused:


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

    int main(void)

    {

    /* declarations */


    float Adjacent, Opposite;

    char Hypotenuse;


    /* input data */

    printf("Welcome to Triangle calculation program!\n");
    printf("Please enter the opposite side of your rectangle:\t");
    scanf("%F",&Opposite);

    printf("Please enter the adjacent side of your rectangle:\t");
    scanf("%F", &Adjacent);

    /*calculation*/

    Hypotenuse=sqrt(Adjacent^2+Opposite^2);

    /*output*/

    printf("\nThe Hypotenuse of the triangle is:\t%f metres");

    /*exit*/

    printf("thank you for using this great program, :\n");


    }

    1. Definition of sqrt() function returns a float (or double). What are you trying to store the result in?

    2. Do you know what the ^ operator does in c?

    3. Not entirely sure but I think the formatters in scanf / printf are case sensitive so you need to use %f and not %F


  • Registered Users Posts: 1,322 ✭✭✭Mad_Max


    kona wrote: »

    float Adjacent, Opposite;
    char Hypotenuse;
    .
    .
    .
    Hypotenuse=sqrt(Adjacent^2+Opposite^2);

    Why is adjacent and opposite float while hypotenuse is char? I'd imagine that is the error right there.


  • Registered Users Posts: 7,525 ✭✭✭kona


    thanks lynchie thats exactly what was wrong!

    got it to compile but the calculation doesnt work???

    i think it has somthing to do with the hyp i assigned to char, ive put it in float,which didnt work,i also tried int which didnt work either:confused:


  • Registered Users Posts: 981 ✭✭✭fasty


    ^ is not what you use to get the power of a number in C/C++

    use the pow(double base, double exponent) function to get the power of a number.

    Also, a char isn't a number as such, it represents a character.


  • Registered Users Posts: 7,525 ✭✭✭kona


    ye i just multiplied them together!! that got around that problem, it just doesnt seem to recognise hypotenuse in the main.

    the ****ing drive i was saving to has just gone kamikaze....:mad::mad:
    server seems fupped, ah well, ive 3 programs wrecked, good thing ive saved my stuff:D:D:D

    cheers lads im still just copping onto the char, float and integer stuff, im useless with this stuff:(


  • Advertisement
  • Registered Users Posts: 6,464 ✭✭✭MOH


    My C is rusty, but where are you actually outputting Hypotenuse?


  • Registered Users Posts: 7,525 ✭✭✭kona


    yup, until i got pissed off and changed it to hyp the opposite to O and adjacent to A.

    my script looks like this now

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

    int main(void)

    {

    /* declarations */
    float a, o, hyp;


    /* welcome message */
    printf("Welcome to Triangle calculation program!\n");

    /* input data */
    printf("Please enter the opposite side of your rectangle:\n");
    scanf("%f",&o);

    printf("Please enter the adjacent side of your rectangle:\n");
    scanf("%f", &a);

    /*calculation*/
    hyp= sqrt(a*a+o*o);

    /*output*/
    printf("\nThe Hypotenuse of the triangle is:%.2f metres\n");

    /*closing message*/

    printf("thank you for using this great program, copyright :\n");

    return(EXIT_SUCCESS);
    }


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    You're still not outputting the value of hypotenuse. Look carefully at the printf() function that you're trying to use for this.

    Also, while the way you're multiplying then adding the arguments for sqrt() will work correctly as used here, make sure you understand why (i.e. operator precedence). I'd recommend getting used to using parentheses rather than relying on operator precedence, so that a) you don't get bitten on the ass in future and b) it's clear to anyone reading your code (including yourself) what order the operations are performed in.


  • Registered Users Posts: 619 ✭✭✭Peadar06


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

    main()

    {

    /* declarations */


    float a,o,hyp;


    /* input data */

    printf("Welcome to Triangle calculation program!\n");

    printf("Please enter the opposite side of your rectangle:\t");

    scanf("%f",&o);

    printf("Please enter the adjacent side of your rectangle:\t");

    scanf("%f", &a);

    /*calculation*/

    hyp = (sqrt((a*a)+(o*o)));

    /*output*/

    printf("\nThe Hypotenuse of the triangle is:\t%f metres\n", hyp);

    /*exit*/

    printf("Thank you for using this great program.\n");


    }

    This should work for you now.

    Peter


  • Registered Users Posts: 7,525 ✭✭✭kona


    cheers man!:)


  • Advertisement
Advertisement