-1

I kept getting Illegal use of character on the line that checks if a variable is empty or null.

Does anyone know why this happened?

the error points here:

if((fa == "" && fp == "") || (fb == "" && fp == "")){

though it worked before i just dont know whats causing this error.

Edit:

changed the code now the errors are too few parameters for function sinfa.

i only want to pass one parameter at a time to this function, is it possible or do i have to create another function to do that so that i can cater to them individually?

thank you. :)

#include<stdio.h>

#include<conio.h>
#include<math.h>
#define PI 3.14159265359

float sinfa(float num1,float num2)
{
    float fc;
    float powers;
    float rad_angle;

    if(num1 != 0){
    rad_angle = num1 * (PI / 180.0);
    powers = pow(rad_angle,4);
    fc = sin(rad_angle)-powers+1;
    }else{
    rad_angle = num2 * (PI / 180.0);
    powers = pow(rad_angle,4);
    fc = sin(rad_angle)-powers+1;
    }
    return (fc);
}

float sinp(float p1)
{
    float fop;
    float ppowers;
    printf("%f",p1);
    ppowers = pow(p1,4);
    fop = sin(p1)-ppowers+1;
    return (fop);
}

float tp(float fa,float fb,int num1,int num2)
{
    float p;
    float fm2 = fa*num2;
    float fm1 = fb*num1;
    p = fm2-fm1/fa-fb;
    return (p);
}

float main()
{
    float num1;
    float num2;
    float fa;
    float fb;
    float p1;
    float fp;

    clrscr();
    printf("Enter number 1: \n");
    scanf("%f", &num1);
    getch();
    printf("Enter number 2: \n");
    scanf("%f", &num2);
    getch();
    clrscr();

    if((fa == 0 && fp == 0) || (fb == 0 && fp == 0)){
        fa = sinfa(num1);
        fb = sinfa(num2);
        p1 = tp(fa,fb,num1,num2);
        fp = sinp(p1);
    }else{
        if((fa*fp) < 0){
            num1 = num1;
            num2 = p1;
            fa = sinfa(num1);
            fb = sinfa(num2);
            p1 = tp(fa,fb,num1,num2);
            fp = sinp(p1);
        }
        if((fb*fp)< 0){
            printf("\n 2");
            getch();
        }
    }
}
magicianiam
  • 1,474
  • 7
  • 33
  • 70

4 Answers4

2

"" is not a number. You cannot compare a number to it.

2

The error appears because you try to compare a literal string to a float value.

Your test for empty strings is also wrong. To check if a string is null or empty you should use the following code (assuming that type of fp is char *):

if(fp == NULL || *fp == '\0')

The sinfa function doesn't have a valid prototype. Add type specifiers to the parameters num1, num2 in the function definition.

And according to the Standard the prototype of the main function should be either

int main(void)

or

int main(int argc, char *argv[])

Yours is wrong.

I suppose you're trying to check if there were errors during scanf input conversion. In order to do that you should inspect the result of scanf and check the gloval error state using the ferror function (or check the errno value directly).

nameless
  • 1,969
  • 11
  • 34
  • FP is float so how do i compare it? – magicianiam Jan 30 '13 at 00:40
  • to check if it has any values. – magicianiam Jan 30 '13 at 00:44
  • 1
    @magicianIam: It always has precisely one value. That's the nature of a `float`. Perhaps what you really wanted to do was read in a string, and do one thing if it was empty and, otherwise, convert that string to a float (maybe with `atof`). – David Schwartz Jan 30 '13 at 00:44
  • @magicianIam, comparing a number to an empty string doesn't make sense for me. You should convert your floating value to string before comparison (though the result of conversion will always be non-empty) or compare the a float to another float. – nameless Jan 30 '13 at 00:45
  • @magicianIam: The above commenters are correct, a float has a single value. But you need to initialize it to one first, or your program has undefined behavior. Just wanted to make that clear. – GManNickG Jan 30 '13 at 00:52
  • @GManNickG so i need to set float to initially hold a value? like float test = 0; also i edited the question and when i tried to change main() to int main(void), i still get too few parameters for my sinfa function. – magicianiam Jan 30 '13 at 00:55
  • @magicianlam, `main` is irrelevant to this issue. You've defined the `sinfa` function to take two variables, but pass only one. – nameless Jan 30 '13 at 01:00
  • @magicianIam: Yes. And once you say `float test = 0;` then it will have a value of zero until you say otherwise. And if the compiler is saying you're passing too few parameters to `sinfa`, why would changing `main`'s parameter list make a difference? Change your calls to `sinfa`. According to the declaration you wrote: `float sinfa(float num1,float num2)`, it takes two arguments, yet you're only passing one: `sinfa(num1);`. – GManNickG Jan 30 '13 at 01:01
1

Error appears because you're comparing float with string literal, ie:

if((fa == "")

You can only do a comparison of float with a numeric/float literal, ie:

if((fa == 0.0

Recall that there's no such concept as NULL / uninitialized for primitive type such as float. Instead they typically default to 0.

Hence having the following means f value will be literal 0

float f;
gerrytan
  • 40,313
  • 9
  • 84
  • 99
  • i changed it to check if fa == 0. will that be ok? – magicianiam Jan 30 '13 at 00:48
  • Yes although it has slightly different semantics. 0 is an integer literal while 0.0 is a floating value literal. If you compare it to 0 there will be some implicit type casting happening -- but it wouldn't matter in your case – gerrytan Jan 30 '13 at 03:33
  • They don't "default to zero". If you didn't initialize your float variable, and scanf didn't write it (because the conversion failed), then reading it is actually undefined behaviour since it's still uninitialized. Use `float num1 = 0;` if you want its initial value to be known. – Peter Cordes Jan 07 '22 at 11:30
0
float fa;
float fb;
float p1;
float fp;

clrscr();
printf("Enter number 1: \n");
scanf("%f", &num1);
getch();
printf("Enter number 2: \n");
scanf("%f", &num2);
getch();
clrscr();


if((fa == 0 && fp == 0) || (fb == 0 && fp == 0)){

Where are you assigning a value to fa, fb or fp? Nowhere. This is undefined behaviour.

autistic
  • 1
  • 3
  • 35
  • 80