In my calculator I tried firstly to make one operation functioning to have integers be displayed properly and when someone inputted a character it would say invalid. When I input two integers it say's invalid. Not the actual sum of it.
#include <stdio.h>
#include <conio.h>
int main(){
char op;
int num1, num2;
int result;
printf("Enter ( , -, /, *): ");
scanf("%c", &op);
printf("Enter Two Integers: \n");
scanf("%d %d", &num1, &num2);
switch (op){
case ' ':
result = num1 num2;
if(!(num1 == ' ' && num2 == ' ')){
printf("Invalid");
}
else{
printf("Sum: %d ", result);
}
break;
case '-':
result = num1-num2;
printf("Difference: %d ", result);
break;
case '/':
result = num1/num2;
printf("Quotient: %d ", result);
break;
case '*':
result = num1*num2;
printf("Product: %d ", result);
break;
default:
break;
}
getch();
return 0;
}
I expected that with that new line of condition it will make characters and symbols print "Invalid"
CodePudding user response:
if(!(num1 == ' ' && num2 == ' '))
This doesn't make any sense for several reasons. First of all De Morgan's laws and boolean algebra is often considered a prerequisite before studying any form of programming. By applying De Morgan/common sense, then we can tell that the opposite to "if num1 is and num2 is " is "if num1 isn't OR num2 isn't ". That is:
if(num1 != ' ' || num2 != ' ')
or if you will if(!(num1 == ' ' || num2 == ' '))
.
With that logic flaw out of the way, this is the wrong solution to the the actual problem anyway. You simply want to prevent the user from entering a character - any character not just ' '
- when expecting a number. The easiest way of doing that is to check the result of scanf
- it returns a number corresponding to the number of arguments successfully read. For example:
int result;
do
{
result = scanf("%d %d", &num1, &num2);
if(result != 2)
{
printf("You must enter two numbers!\n");
}
}
while(result != 2);
Another option is to read the input as a string with fgets
and then parse that string afterwards.
As a side note, please note that <conio.h>
has been obsolete for well over 20 years and if someone taught you to use it, you need a more updated source of learning. The future for freshly graduated MS DOS programmers isn't very promising...
CodePudding user response:
Be sure to check the return value of scanf
to ensure it read the number of values you expected.
num1
and num2
are int
variables and you are reading into them using the %d
specifier. If you type in
or other symbols, scanf("%d %d", &num1, &num2)
will return 0
. A simple demonstration:
$ cat > testing.c
#include <stdio.h>
int main(void) {
int op;
int result = scanf("%d", &op);
printf("%d\n", result);
}
$ gcc testing.c
$ ./a.out
0
$ ./a.out
67
1
The odds that num1
and num2
uninitialized both contain the value ' '
are not impossible but highly unlikely, so the most likely outcome is that "Invalid."
is printed.
As noted in comments, 43
is the ASCII code for ' '
so if these specific numbers are input, both num1
and num2
will test as equal to 43
and their sum (86
) will be printed.