compiler showing error but why? - Page 1
Forum section: electronics / beginners

author: khatus
author_rank: Regular Contributor
timestamp: April 17, 2020, 03:11:57 am
content: Code:// Program to create a simple calculator#include <stdio.h>int main() {char operator;double n1, n2;printf("Enter an operator (+, -, *, /): ");scanf("%c", &operator);printf("Enter two operands: ");scanf("%lf %lf",&n1, &n2);switch(operator){case '+':printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);break;case '-':printf("%.1lf - %.1lf = %.1lf",n1, n2, n1-n2);break;case '*':printf("%.1lf * %.1lf = %.1lf",n1, n2, n1*n2);break;case '/':printf("%.1lf / %.1lf = %.1lf",n1, n2, n1/n2);break;// operator doesn't match any case constant +, -, *, /default:printf("Error! operator is not correct");}return 0;}Tn this code if i write the operands after n1 and n2 the compiler showing error but why?Code:int main() {char operator;double n1, n2;printf("Enter two operands: ");scanf("%lf %lf",&n1, &n2);printf("Enter an operator (+, -, *, /): ");scanf("%c", &operator);

author: stafil
author_rank: Regular Contributor
timestamp: April 17, 2020, 03:18:17 am
content: scanf("%lf %lf",&n1, &n2);you can't enter the two operands with enter in between. Instead of "3", Enter, "5", Enter, try "3 5" enteredit: oh. also it's not the compiler shows the error but your application.

author: Nominal Animal
author_rank: Super Contributor
timestamp: April 17, 2020, 01:35:20 pm
content: The reason for the error is that for scanf() family of functions,%cconverts a single character, without skipping any whitespace; all other conversions (except%cand%n) skip leading whitespace. So, if you ask and input the numbers first, the newline following the second number is left in the input buffer, and your%cconsumes that. What you wanted it to do, is to convert the characterfollowing the whitespace.(Stafil, that's not true. For scanf family of functions, spaces and tabs and newlines are the the same: just whitespace. So you can definitely use a newline/Enter instead of a space there. But, you are right that it is the program, and not the compiler, that printed the error message.)A minimum fix would be to usescanf(" %c", &operator);with a space before the conversion: all whitespace characters in scanf() family of functions mean the same thing "skip whitespace, if any". (It does notrequirewhitespace there, though.)You should always check the return value of the scanf family of functions: it is the number of successful conversions, or EOF (if no more input, or the input does not match).Also, instead of%c, you can use%1[-+*/], which is at most one character string, using only characters- + * /. (I put the - first, because it can be used for ranges; for example,a-zfor all lowercase letters, and/or0-9for all digits.Consider this variation of your code:Code:#include <stdlib.h>#include <stdio.h>int main(void) {char operator;double n1, n2;printf("Enter two operands: ");if (scanf("%lf %lf",&n1, &n2) != 2) {printf("No, I don't understand that input.\n");return EXIT_FAILURE;}printf("Enter an operator (+, -, *, /): ");if (scanf("%1[-+*/]", operator) != 1) {printf("No, that's not an operator I recognize.\n");return EXIT_FAILURE;}switch(operator){case '+':printf("%.1f + %.1f = %.1f\n", n1, n2, n1+n2);break;case '-':printf("%.1f - %.1f = %.1f\n", n1, n2, n1-n2);break;case '*':printf("%.1f * %.1f = %.1f\n", n1, n2, n1*n2);break;case '/':printf("%.1f / %.lf = %.1f\n", n1, n2, n1/n2);break;// operator doesn't match any case constant +, -, *, /default:printf("Error! operator is not correct");return EXIT_FAILURE;}return EXIT_SUCCESS;}This time,operatoris a string (of at most two characters; the last character is always reserved for the end-of-string \0), so we examine its first character,operator.Also, for arrays likeoperator,operator == &operator: the array name behaves exactly like a pointer to the array. So, bothscanf("%1[-+*/]", operator)andscanf("%1[-+*/]", &operator)do the same things. (Both array names and function names behave this way.)Would you be interested in seeing the calculator written in a way that lets one write the two numbers and the operator in any order?NVM, I just remembered scanf() has only one character of pushback, so cannot deal correctly with input like"- -1 -2". One needs a custom tokenizer, or better yet, parse input line-by-line.

author: stafil
author_rank: Regular Contributor
timestamp: April 17, 2020, 06:18:40 pm
content: Quote from: Nominal Animal on April 17, 2020, 01:35:20 pmYou are absolutely right. I stand corrected!