# C++ Beginner

• 06-03-2009
Ronzel
C++ Beginner
I am trying to write my first program in C++. I am receiving a code that I am not sure how to fix. I would appreciate the help! The code that I am receiving is : "error: expected primary-expression before "else".

Code:

```// Math Program - provides quick calculations to basic math problems #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; // Function for factorial long factorial (long a) {   if (a > 0)   return (a * factorial (a-1));   else   return (1); } int main() { // Header to program     cout << "******************************" << endl;     cout << "******** Math Program ********" << endl;     cout << "******************************" << endl << endl;     cout << "Please choose from the following options" << endl;     cout << "1-Factorial" << endl;     cout << "2-Slope of a Line" << endl;     cout << endl << "Selection: ";     int choice;     cin >> choice; // Decide which function to run     long number;     if (choice == 1)         { // Enter a number and call factorial() to provide answer             cout << endl << "Please type a number: ";             cin >> number;                 if(number < 0)                 {                     cout << "Enter a positive integer!..." << endl <<endl;                 }                 else                 {                     cout << number << "! = " << factorial (number) << endl << endl;                 }         } long x1,x2,y1,y2; long slope;     else if (choice == 2) // Enter coordinates and calculuates slope to provide answer         {             cout << endl << "Please enter the first set of coordinates: ";             cin >> x1;             cin >> y1;             cout << endl << "Now enter the second set of coordinates: ";             cin >> x2;             cin >> y2;             slope = ((y2-y1)/(x2-x1));             cout << "The slope of the line is:" << slope;         } // Wait for user to respond to quit program system ("pause"); return 0;```
I am getting the error after the else( choice ==2) portion
• 06-03-2009
CornedBee
These declarations:
Code:

```long x1,x2,y1,y2; long slope;```
are between the then-block of the big if and its else block. You're not allowed to put anything between them. Put the declarations inside the else block.
• 06-04-2009
Elysia
And you should better your indentation, as well.
SourceForge.net: Indentation - cpwiki