ccompiler-errors

code compiles on other compilers, but returns an error when used in visual studio


This code in C works properly when I use code::blocks or any online compilers, however if I try to use this in VSCODE it returns "expected type-specifier before ';' token on LN 5, Col 18" "expected type-specifier before ')' token on LN 13, Col 26" "expected type-specifier before ')' token on LN 20, COL 21"

this is the code that i was referring to +

#include <stdio.h>
#include <ctype.h>

int main() {
    char operator;
    double num1;
    double num2;
    double result;
    char choice;

    do {
    printf("Enter the operator you want to use: ");
    scanf("%c", &operator);

    printf("Number 1: ");
    scanf("%lf", &num1);
    printf("Number 2: ");
    scanf("%lf", &num2);

    switch (operator)
    {
    case '+':
        result = num1 + num2;
        printf("The Sum is: %.1lf\n", result);
        break;
    case '-':
        result = num1 - num2;
        printf("The Difference is: %.1lf\n", result);
        break;
    case '/':
        result = num1 / num2;
        printf("The Quotient is: %.3lf\n", result);
        break;
    case '*':
        result = num1 * num2;
        printf("The Product is: %.1lf\n", result);
        break;

    default:
    printf("That is not a valid operator! \n");
        break;
    }

    printf("Do you want to compute again?: Y/N ");
    scanf(" %c", &choice);
    choice = toupper(choice);
    while ((getchar()) != '\n');

    }while (choice == 'Y');

    return 0;
}

I tried changing the char operators; but to no avail. this problem shows up on VSC only


Solution

  • That is the exact error you get when compiling your code as a C++ program.

    It's not a valid C++ program since operator (as in char operator;) is a keyword in C++ and you can't declare variable named operator in C++. You need to save your program as a C program and compile it as such.