Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I code the program to accept not only uppercase but also lowercase first letter?

For example, if I run the program and type in sin, it shows error. When I write down Sin, it’s ok. For example, if I run the program and type in sin, it shows error. When I write down Sin, it’s ok. For example, if I run the program and type in sin, it shows error. When I write down Sin, it’s ok. For example, if I run the program and type in sin, it shows error. When I write down Sin, it’s ok.

/*************************************************************
                            Pavel Kivilša
                               EKF-21
                        1 laboratorinis darbas
                               2021-11
*************************************************************/

#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

enum Functions { Floor, Round, Ceil, Sin, Cos, Cosh, Exp, Tan, Tanh, Sinh, Log, Log10, Sqrt, Pow, Trunc, Help };
enum Functions function_name;

int main(int argc, char* argv[]) {
    float number, number2;
    char* function[16] = { "Floor", "Round", "Ceil", "Sin", "Cos", "Cosh", "Exp", "Tan", "Tanh", "Sinh", "Log", "Log10", "Sqrt", "Pow", "Trunc", "Help" };

    char name[6];
    printf("Enter a math function: ");
    scanf("%s", name);

    for (function_name = 0; function_name <= 15; function_name++) {
        if (strcmp(function[function_name], name) == 0) {
            break;
        }
    }


    switch (function_name) {
    case Floor:
        printf("Enter the value of floor: ");
        scanf("%f", &number);
        printf("Floor of %.2f is equal to %.2f\n", number, floor(number));
        break;
    case Round:
        printf("Enter the value of round: ");
        scanf("%f", &number);
        printf("Round of %.2f is equal to %.2f\n", number, round(number));
        break;
    case Ceil:
        printf("Enter the value of ceil: ");
        scanf("%f", &number);
        printf("Ceil of %.2f is equal to %.2f\n", number, ceil(number));
        break;
    case Sin:
        printf("Enter the value of sin: ");
        scanf("%f", &number);
        printf("Sin of %.2f is equal to %.2f\n", number, sin(number));
        break;
    case Cos:
        printf("Enter the value of cos: ");
        scanf("%f", &number);
        printf("Cos of %.2f is equal to %.2f\n", number, cos(number));
        break;
    case Cosh:
        printf("Enter the value of cosh: ");
        scanf("%f", &number);
        printf("Cosh of %.2f is equal to %.2f\n", number, cosh(number));
        break;
    case Exp:
        printf("Enter the value of exp: ");
        scanf("%f", &number);
        printf("Exp of %.2f is equal to %.2f\n", number, exp(number));
        break;
    case Tan:
        printf("Enter the value of tan: ");
        scanf("%f", &number);
        printf("Tan of %.2f is equal to %.2f\n", number, tan(number));
        break;
    case Tanh:
        printf("Enter the value of tanh: ");
        scanf("%f", &number);
        printf("Tanh of %.2f is equal to %.2f\n", number, tanh(number));
        break;
    case Sinh:
        printf("Enter the value of sinh: ");
        scanf("%f", &number);
        printf("Sinh of %.2f is equal to %.2f\n", number, sinh(number));
        break;
    case Log:
        printf("Enter the value of log: ");
        scanf("%f", &number);
        printf("Log of %.2f is equal to %.2f\n", number, log(number));
        break;
    case Log10:
        printf("Enter the value of log10: ");
        scanf("%f", &number);
        printf("Log10 of %.2f is equal to %.2f\n", number, log10(number));
        break;
    case Sqrt:
        printf("Enter the value of sqrt: ");
        scanf("%f", &number);
        printf("Sqrt of %.2f is equal to %.2f\n", number, sqrt(number));
        break;
    case Pow:
        printf("Enter the value of pow: ");
        scanf("%f", &number);
        printf("Enter the second value of pow: ");
        scanf("%f", &number2);
        printf("%.2f to the power of %.2f is equal to %.2f\n", number, number2, pow(number, number2));
        break;
    case Trunc:
        printf("Enter the value of trunc: ");
        scanf("%f", &number);
        printf("Trunc of %.2f is equal to %.2f\n", number, trunc(number));
        break;
    case Help:
        printf("The following functions can be used: Floor, Round, Ceil, Sin, Cos, Cosh, Exp, Tan, Tanh, Sinh, Log, Log10, Sqrt, Pow, Trunc.\n");
        break;
    default:
        printf("Function not found, try again by typing in Help.\n");
        break;
    }
    return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You are assigning to function_name

for (function_name = 0; function_name <= 15; function_name++) {

Which is not allowed in the C++ compiler you are using.

error C2440: '=': cannot convert from 'int' to 'Functions' note: Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

error C2676: binary '++': 'Functions' does not define this operator or a conversion to a type acceptable to the predefined operator

You could turn it into a C compiler by using the /TC switch and then the above will work.

If you wish to continue using C++, you need to cast. Here’s how you’d do it with C-style casts:

for (function_name = (Functions)0; function_name <= 15;
     function_name = (Functions)((int)function_name + 1))
{
    if (strcmp(function[function_name], name) == 0) {
        break;
    }
}

Using C++ style casts:

for (function_name = static_cast<Functions>(0); function_name <= 15; 
     function_name = static_cast<Functions>(static_cast<int>(function_name) + 1))
{
    if (strcmp(function[function_name], name) == 0) {
        break;
    }
}

Other notes:

You are missing #include <string.h> for strcmp and scanf("%s", &name); should be scanf("%s", name);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading