-1

When I execute below code it gives me this message

Error - At least one public class is required in main file

Here is the link of the program.

    #include <iostream.h>
    #include <conio.h>
    
    void main() {
        int a, b, c;
        int sum = 0;
        int sub;
        float prod = 1;
        float div;
        if (12 < a < 20)
            sum = a + b + c;
        else if (b > 30)
            sub = b - c;
        else if (10 < c < 18)
            prod = a * b;
        else
            div = c / a;
        cout <<” The values of sum, sub, prod and div are :”<< sum << sub << prod << div;
        getch();
    }

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34

1 Answers1

0

I'm not sure what does your code do, but there are lots of logical & syntactical errors:

  1. Seemingly you're using a deprecated compiler because iostream.h is no longer used.

  2. The values of the variables a, b & c are kept uninitialized, thus, it contains some garbage values.

  3. The syntax:

    if (10 < c < 18)
    

    is incorrect. The correct idea should be:

    if (10 < c && c > 18)
    
  4. The token is unknown and cannot be used.

  5. The declaration of the main function:

    void main() {}
    

    is incorrect as well. The right way to do it is:

    int main(void) {}
    

Assuming the value of a is initialized to 1, because if the values of b & c are kept zero (probably you want it), the else condition will take place, i.e. division of c with a, which yields illegal instruction error.

After solving these issues, the correct code would look like:

#include <iostream>

int main(void)
{
  // All the variables must be initialized if they are going to be used
  int a = 1, b = 0, c = 0;

  int sum = 0;
  int sub = 0;
  float prod = 1;
  float div = 0;

  if (12 < a && a > 20)
    sum = a + b + c;
  else if (b > 30)
    sub = b - c;
  else if (10 < c && c > 18)
    prod = a * b;
  else
    div = c / a;

  std::cout << "The values of sum, sub, prod and div are :"
            << sum << sub << prod << div << std::endl;

  return 0;
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34