Im trying to make a console app to solve math in order to make things go faster. However, there is an error and it says expected primary-expression before "else" and also says expected ':' before "else". Here's the code:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a, b, c, d, e, f, g, h, i, j;
    int answer;
    int result1, result2, result3, result4, result5, result6;
    int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13;
    int binomialformula, herosformula, trianglearea, pythagorean;
    cout << "Type binomialformula, herosformula, trianglearea, or pythagorean:  ";
    cin >> answer;
    if (answer == binomialformula)
   
    cout << "a=";
   cin >> a;
   cout << "b=";
   cin >> b;
   cout << "c=";
   cin >> c;

   s1 = b * b;
   s2 = 4 * a * c;
   s3 = s2;
   s4 = s1 - s2;
   s5 = 2 * a;
   result1 = -b;
   result2 = s4;
   result3 = s5;
   
   cout << "Answer:  [";
   cout << result1;
   cout << " +or- square root of ";
   cout << result2;
   cout << "] / ";
   cout << result3;
   cout << ".......";
   
   else if (answer == herosformula)
   
   cout << "side 1 = ";
   cin >> d;
   cout << "side 2 = ";
   cin >> e;
   cout << "side 3 = ";
   cin >> f;
   
   s6 = d + e + f;
   s7 = s6 / 2;
   s8 = s7 - d;
   s9 = s7 - e;
   s10 = s7 - f;
   result4 = s7 * s8 * s9 * s10;
   
   cout << "Answer:   square root of ";
   cout << result4;
   cout << "........";

   
   else if (answer == trianglearea)

   cout << "base = ";
   cin >> g;
   cout << "height = ";
   cin >> h;
   
   s11 = g * h;
   result5 = s11 / 2;
   
   cout << "Area = ";
   cout << result5;
   cout << ".......";

   
   else if (answer == pythagorean)

   cout << "leg = ";
   cin >> i;
   cout << "hypotnuse = ";
   cin >> j;
   
   s12 = i * i;
   s13 = j * j;
   result6 = s13 - s12;
   
   cout << "Answer = square root of ";
   cout << result6;
   cout << ".......";

   
   else
   cout << "Please try again..........";
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

An if with more than one statement has to be wrapped in braces:

// This is OK!
if (something)
  statement;

// This is not OK!
if (something)
  statement;
  statement;
  statement;

// This is OK!
if (something) {
  statement;
  statement;
  statement;
}

What's happening is the else part of your if statement doesn't think it's really part of an if because you have more than one statement in the if without braces. Long story short, you need to add some braces:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
  int a, b, c, d, e, f, g, h, i, j;
  int answer;
  int result1, result2, result3, result4, result5, result6;
  int s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13;
  int binomialformula, herosformula, trianglearea, pythagorean;
  cout << "Type binomialformula, herosformula, trianglearea, or pythagorean:  ";
  cin >> answer;
  if (answer == binomialformula) {
    cout << "a=";
    cin >> a;
    cout << "b=";
    cin >> b;
    cout << "c=";
    cin >> c;

    s1 = b * b;
    s2 = 4 * a * c;
    s3 = s2;
    s4 = s1 - s2;
    s5 = 2 * a;
    result1 = -b;
    result2 = s4;
    result3 = s5;

    cout << "Answer:  [";
    cout << result1;
    cout << " +or- square root of ";
    cout << result2;
    cout << "] / ";
    cout << result3;
    cout << ".......";
  }
  else if (answer == herosformula) {
    cout << "side 1 = ";
    cin >> d;
    cout << "side 2 = ";
    cin >> e;
    cout << "side 3 = ";
    cin >> f;

    s6 = d + e + f;
    s7 = s6 / 2;
    s8 = s7 - d;
    s9 = s7 - e;
    s10 = s7 - f;
    result4 = s7 * s8 * s9 * s10;

    cout << "Answer:   square root of ";
    cout << result4;
    cout << "........";
  }
  else if (answer == trianglearea) {

    cout << "base = ";
    cin >> g;
    cout << "height = ";
    cin >> h;

    s11 = g * h;
    result5 = s11 / 2;

    cout << "Area = ";
    cout << result5;
    cout << ".......";
  }
  else if (answer == pythagorean) {

    cout << "leg = ";
    cin >> i;
    cout << "hypotnuse = ";
    cin >> j;

    s12 = i * i;
    s13 = j * j;
    result6 = s13 - s12;

    cout << "Answer = square root of ";
    cout << result6;
    cout << ".......";
  }
  else
    cout << "Please try again..........";

  system("PAUSE");
  return EXIT_SUCCESS;
}

thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.