Thread: C++ Beginner

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    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

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And you should better your indentation, as well.
    SourceForge.net: Indentation - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM