Dilshan De Zoysa
Dilshan De Zoysa

Reputation: 27

What have I done wrong here?

I've tried this many times but I can't figure out what's wrong! Please help me to solve this error.

#include <`stdio.h>

int main(void)
{
  float accum = 0, number = 0;
  char oper;

  printf("\nHello. This is a simple 'printing' calculator. Simply enter the number followed");
  printf(" by the operator that you wish to use. ");
  printf("It is possible to use the standard \noperators ( +, -, *, / ) as well as two extra ");
  printf("operators:\n");
  printf("1) S, which sets the accumulator; and\n");
  printf("2) N, that ends  the calculation (N.B. Must place a zero before N). \n");

  do
  {
    printf("\nPlease enter a number and an operator: ");
    scanf("%f %c", &number, &oper);
    if (number == 0 && oper == 'N')
    {
      printf("Total = %f", accum);
      printf("\nEnd of calculations.");
    }
    else if (oper == '+', '-', '*', '/', 'S')
    {
      switch (oper)
      {
        case 'S':
          accum = number;
          printf("= %f", accum);
          break;
        case '+':
          accum = accum + number;
          printf("= %f", accum);
          break;
        case '-':
          accum = accum - number;
          printf("= %f", accum);
          break;
        case '*':
          accum = accum * number;
          printf("= %f", accum);
          break;
        case '/':
          if (number != 0)
          {
            accum = accum / number;
            printf("= %f", accum);
          }
          else
            printf("Cannot divide by zero.");
          break;
        default:
          printf("Error. Please ensure you enter a correct number and operator.");
          break;
      }
    }
    else
      printf("Error. Please ensure you enter a correct number and operator.");
  }
  while (oper != 'N');
  return 0;
}

When I compile this code I get the following error as in the snapshot image here. snapshot of error message

Upvotes: 0

Views: 91

Answers (2)

alk
alk

Reputation: 70893

Following the rules for the ,-operator this

if (oper == '+', '-', '*', '/', 'S')

is the same as this

if ('-', '*', '/', 'S')

is the same as this

if ('*', '/', 'S')

is the same as this

if ('/', 'S')

is the same as this

if ('S')

which is not equal 0 and though is always "true".

From the C11 Standard (draft):

6.5.17 Comma operator

[...]

Semantics

2 The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

Upvotes: 4

Benjamin Debott&#233;
Benjamin Debott&#233;

Reputation: 348

First, be cautious about your inclusion :

#include <`stdio.h> 

should be

#include <stdio.h>

Furthermore, you can't use the if-keyword the way you did :

if (oper == '+', '-', '*', '/', 'S')

should be :

if (oper == '+' || oper =='-' || oper == '*' || oper == '/' || oper ==  'S')

Upvotes: 3

Related Questions