April Meiddle
April Meiddle

Reputation: 33

C if-statement issues?

I'm trying to practice making if-statements and am having very little luck with this. Right now I'm trying to make a trigonometric calculator, a very simple one, using if statements, but I can't get it working. The actual problem occurs after input the trig function (sine, cosine, tangent). This is what happens.
1. I compile it
2. It outputs the user prompt
3. I input the function and hit enter
4. The program jumps to a new blank line
5. Nothing happens and the program closes if I press enter

Here is the code itself. Please be kind if I've done something monumentally stupid, I'm pretty new to C.

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

int main(void)
{
    float x;
    float a, o, h;
    float sine, cosine, tangent;

    printf("Enter the trig function you wish to calculate: ");
    scanf("%f", &x);

    if (x == sine)
    {    printf("Enter the value of the opposite leg: ");
         scanf("%f", &o);
         printf("Enter the value of the hypotenuse: ");
         scanf("%f", &h);

         sine = o / h;
         printf("The sine is equal to %f", sine);
    }

    if (x == cosine)
    {    printf("Enter the value of the adjacent leg: ");
         scanf("%f", &a);
         printf("Enter the value of the hypotenuse: ");
         scanf("%f", &h);

         cosine = a / h;
         printf("The cosine is equal to %f", cosine);
    }

    if (x == tangent)
    {    printf("Enter the value of the opposite leg: ");
         scanf("%f", &o);
         printf("Enter the value of the adjacent leg: ");
         scanf("%f", &a);

         tangent = o / a;
         printf("The tangent is equal to %f", tangent);
    }

    getch();
}

Thank you to everyone who who was actually helpful and wasn't rude about my lack of understanding, I didn't realize I had to add a numerical character instead of just a character.

Upvotes: 0

Views: 216

Answers (4)

Jerry Coffin
Jerry Coffin

Reputation: 490713

Right now, this code:

float x;
float a, o, h;
float sine, cosine, tangent;

printf("Enter the trig function you wish to calculate: ");
scanf("%f", &x);

if(x == sine)

...reads a value into x, but then compares it to the current value of sine. Unfortunately, you haven't initialized sine, so it's comparing to some unknown, semi-random value.

When you compare to cosine and tangent, you're doing more of the same.

None of these comparisons has a meaningful result (e.g., they might easily all be true).

At a guess, you probably want to have the user enter a string, and compare that to the values "sine", "cosine", and "tangent", using strcmp.

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 755064

Simple (minimal) fixes

Yes, you're on the verge of doing various things that an experienced programmer would call silly, but they're the sorts of mistakes that novices make (you're neither the first nor the last to make them).

int main(void)
{
    float x;
    float a, o, h;
    float sine, cosine, tangent;

    printf("Enter the trig function you wish to calculate: ");
    scanf("%f", &x);

    if (x == sine)

The primary problem is that you've not given sine, cosine or tangent values, so you've no idea what to enter to make the equality work.

The secondary problem is that comparing floating point numbers for equality is not a good idea.

You'd probably do best with something like:

int main(void)
{
    int x;
    float a, o, h;
    enum { sine, cosine, tangent };

    printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
    scanf("%d", &x);

    if (x == sine)

This is more or less orthodox, and reading and comparing integers for equality is reliable. You would have to change the actions since I've pre-empted the names sine, cosine, and tangent as enumeration (integer) constants. You could work around that by using upper-case names for the constants (that's pretty orthodox), or using a prefix for the names, or ...

int main(void)
{
    int x;
    float a, o, h;
    float sine, cosine, tangent;
    enum { SINE, COSINE, TANGENT };

    printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
    scanf("%d", &x);

    if (x == SINE)

Friendlier input

As you might gather from the comments below, it would be better to allow the user to enter the name of the function they'd like to enter instead of making them enter a coded number. That's a little trickier to code up reliably, which is the main reason why I left the answer above using numbers.

#include <stdio.h>
#include <string.h>

int main(void)
{
    char line[4096];

    printf("Enter trig function you wish to calculate: ");
    if (fgets(line, sizeof(line), stdin) != 0)
    {
        char *nl = strchr(line, '\n');
        if (nl != 0)
            *nl = '\0';
        if (strcmp(line, "sine") == 0)
        {
            /* Process sine */
        }
        else if (strcmp(line, "cosine") == 0)
        {
            /* Process cosine */
        }
        else if (strcmp(line, "tangent") == 0)
        {
            /* Process tangent */
        }
        else
        {
            fprintf(stderr, "Unrecognized trig function (%s)\n", line);
        }
    }
}

The 4096 is simply a big round number that's so long that it is very unlikely that anyone will ever enter a line that is longer than that. If they do enter such a line, then GIGO and they get what they deserve (which will be a polite error message that the name they entered was not recognized).

This is still not wonderful code. It might be reasonable to strip leading and trailing white space, and maybe case-convert the input to lower case, and one of the messages should probably identify the valid function names. It would be possible to have the code loop repeatedly, but then you'd need a function to prompt and read the response, etc. All of which adds usability at the expense of more code which complicates things unnecessarily for a beginning programmer.

Upvotes: 7

syb0rg
syb0rg

Reputation: 8247

I'm not sure what you are entering into the program to begin with, but this is where your error is. If you are entering a character array (a "string"), and that is being passed to x, you can't compare that with a floating point value. Also, your sine, cosine, and tangent variables have no value/have not been assigned anything. To solve your issue, assign your variables a number, such as float sine = 1; and make sure what you enter into the command line to pass to x is a number. If you want to enter "cosine", and pass that value to x, then you will have to change your x variable to a char array, such as char[] x = "", and then change your sine, cosine, and tangent variables to be character arrays as well. If you do change your variables to arrays, remember to remove the & from the scanf statement, like this -> scanf("%s", x);.

Upvotes: 1

Jim Mischel
Jim Mischel

Reputation: 134125

Your if statements aren't working because you're comparing the input value, x, with a floating point value that hasn't been set. I think what you want to do is this:

int x;
printf("Enter the trig function you wish to calculate\n");
printf("1=sine, 2=cosine, 3=tangent: ");
scanf("%d", &x);

if (x == 1)
{
    // do sine
}
else if (x == 2)
{
    // do cosine
}
else if (x == 3)
{
    // do tangent
}
else
{
    printf("I don't know that function.\n");
}

Monumentally stupid? Naw. It's an easy kind of mistake to make when you first start programming. Stick with it. We've all been there.

Upvotes: 2

Related Questions