One thing that you could do is to use
scanf( "%c %c %c", &digit1, &operator, &digit2 );
but this would only work if the user enters single digits, not a multi-digit number such as 23
.
Instead of using the function scanf
, it is generally recommended to always read an entire line of input at once, including the newline character. The function scanf
can do nasty things, such as leaving the newline character on the input stream, which can cause trouble.
In order to read a whole line of input at once, I recommend using the function fgets
.
I recommend that you treat all characters up to, but not including the operator character, as a separate string. You can then determine whether this string is a valid integer by using the function strtol
. If it is not, you can then check the length of the string, to verify that it is only a single character. If it represents neither, then your program should print an error message and exit.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
//This function will convert a string to a number. If the
//string represents an actual number, it will return this
//number. If the string contains a single letter, it will
//return the ASCII code of this letter. Otherwise, the
//function will exit the program with an error message.
long convert_string_to_number( const char *str )
{
long num;
char *p;
//attempt to convert the string into a number
num = strtol( str, &p, 10 );
if ( p == str )
{
//failed to convert string to number, so we must
//now determine whether it is a single letter
if ( strlen(str) == 1 && isalpha( (unsigned char)str[0] ) )
{
//return the ASCII code of the character
return str[0];
}
else
{
printf( "Error: Operand must be either a number or a single letter!\n" );
exit( EXIT_FAILURE );
}
}
//verify that all remaining characters are whitespace
//characters, so that input such as "6abc<23" gets
//rejected
for ( ; *p != '\0'; p++ )
{
if ( !isspace( (unsigned char)*p ) )
{
printf( "Error: Unexpected character found!\n" );
exit( EXIT_FAILURE );
}
}
return num;
}
bool perform_operation( long num1, char operator, long num2 )
{
switch ( operator )
{
case '<':
return num1 < num2;
case '>':
return num1 > num2;
case '=':
return num1 == num2;
default:
printf( "Error: Invalid operator!\n" );
exit( EXIT_FAILURE );
}
}
int main( void )
{
char line[200];
char *p;
char operator;
long num1, num2;
//attempt to read one line of input
if ( fgets( line, sizeof line, stdin ) == NULL )
{
printf( "Input error!\n" );
exit( EXIT_FAILURE );
}
//attempt to find operator
p = strpbrk( line, "<>=" );
//print error message and abort if no operator found
if ( p == NULL )
{
printf( "Error: No valid operator found!\n" );
exit( EXIT_FAILURE );
}
//remember the operator
operator = *p;
//overwrite the operator with a null character, to
//separate the input string into two strings
*p = '\0';
//make the pointer p point to the start of the second
//string
p++;
//attempt to convert both strings to a number
num1 = convert_string_to_number( line );
num2 = convert_string_to_number( p );
//perform the actual operation and print the result
if ( perform_operation( num1, operator, num2 ) )
{
printf( "True" );
}
else
{
printf( "False" );
}
}
This program has the following behavior:
abc
Error: No valid operator found!
=
Error: Operand must be either a number or a single letter!
6abc<23
Error: Unexpected character found!
6<23
True
a=98
False
a=97
True