1

The program will prompt the user to enter a simple expression. After splitting the string and assigning the variables, i want to check to see if what the user entered is a an integer so it can be calculated in the switch statement. What would be the best way to validate the data inside num1 and num2 to make sure they are integers and not letters or any other character.

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

/*This function will display the user's desired expression to be calculated */
char* expressionDisplay(char* input)
{
    char *str = input;
    printf("The expression entered is: %s\n", str);
    
}

int main()
{
    char str[50];
    char operator[6] = "+-*/%";
    int num1,num2;
    char calculation;
    char *oldstr = malloc(sizeof(str));
    printf("This program will solve a simple expression in the format 'value' 'operator' 'value'\n ");
    printf("Example 2+6 or 99 * 333\n");
    printf("Enter the simple expression to be calculated: \n"); 
    scanf("%[^\n]%*c", str);  //this will scan the whole string, including white spaces
    strcpy(oldstr, str);
    for(int i = 0; i < strlen(operator); i++)
    {

        char *position_ptr = strchr(str, operator[i]);
        int position = (position_ptr == NULL ? -1 : position_ptr - str);    
        if(str[position] == operator[i])
        {
            calculation = operator[i];
            char *num1_ptr = strtok(str, operator);
            int num1 = atoi(num1_ptr);
            char * num2_ptr = strtok(NULL, operator);
            int num2 = atoi(num2_ptr);
            break;
        }else
        
        calculation = position;
    }

    switch(calculation)
    {
        case '+':
        expressionDisplay(oldstr);
        printf("Sum\n");
        break;
        case '-':
        expressionDisplay(oldstr);
        printf("Subtract\n");
        break;
        case '*':
        expressionDisplay(oldstr);
        printf("Multiply\n");
        break;
        case '/':
        expressionDisplay(oldstr);
        printf("Division\n");
        break;
        case '%':
        expressionDisplay(oldstr);
        printf("Modulus\n");
        break;
        default:
        printf("Sorry unable to calculate the expression entered. Try again.\n");
        printf("Enter a simple expression - number operator number - ");
        break;
    }
}
  • 1
    Does this answer your question? [Input validation of an Integer using atoi()](https://stackoverflow.com/questions/15229411/input-validation-of-an-integer-using-atoi) – kaylum Sep 28 '20 at 05:39
  • Not sure,so i cannot ask the user for multiple inputs, has to be one entry. I need to check for the operator to see if there is one, which is why i have the for loop using strchr() because i will use that later to break up the string and retrieve the two variables. I noticed i can enter letters as long as i have one of the operators it will work. Where i am confused is how to check or cast the pointer into a char for a boolean When letters are inserted, and the atoi() is used i am given a 0 and it is stored in num1, however if a user enters 0 it should be valid as well, 0 - 22 is valid entry. – Jhony Ortiz Sep 28 '20 at 06:21
  • `atoi` is not suitable for validation. Use `strtol` instead. It is explained in the linked post. – kaylum Sep 28 '20 at 06:28
  • using strtol will convert an entry of letters into 0, however if a user inputs 0 it can be valid......using a if statement checking for 0 could invalidate a valid entry. – Jhony Ortiz Sep 28 '20 at 22:16
  • Take the time to read completely and carefully the info available to you. You've been given the man page and posts with examples. The return value of the function is not the only thing to check. The [man page](https://linux.die.net/man/3/strtol) tells you how to check for an invalid number: `If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.` – kaylum Sep 28 '20 at 22:19
  • Thank you, took me a minute to understand it. I am still very new at this, appreciate the feedback – Jhony Ortiz Sep 29 '20 at 16:45

1 Answers1

0

In this case you can use the strtol function of standard c library. You can see more detail about it at this link : strtol - c++reference.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35