Thread: Error

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    Error

    Hello , i'm almost new on C++ , i've been following lessons but they seem the same with different words from another coding language i learned in school .
    I wanted to make a simple program which will ask you to choose a shape ( triangle or etc...) .
    But i get this error when i try to compile .

    Code:
    Compiler: Default compilerExecuting  g++.exe...
    g++.exe "C:\Dev-Cpp\cpp\surfaces.cpp" -o "C:\Dev-Cpp\cpp\surfaces.exe"    -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include"  -I"C:\Dev-Cpp\include\c++\3.4.2\backward"  -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32"  -I"C:\Dev-Cpp\include\c++\3.4.2"  -I"C:\Dev-Cpp\include"   -L"C:\Dev-Cpp\lib" 
    C:\Dev-Cpp\cpp\surfaces.cpp: In function `int main()':
    C:\Dev-Cpp\cpp\surfaces.cpp:21: error: expected primary-expression before "else"
    C:\Dev-Cpp\cpp\surfaces.cpp:21: error: expected `;' before "else"
    
    
    Execution terminated
    My C++ code (sry for wrong english on it but its a priv work xD) ::
    Code:
    #include <iostream>using namespace std;
    
    
    int main()
    {
        int triangle, square ;
        cout << "Choose your shape=";
        if( triangle > square );
        {
            int base, height, sum;
            cout << "Enter the base of triangle here=" ;
            cin >> base;
            cout << "Enter the height of triangle here=" ;
            cin >> height;
            sum = base * height / 2 ;
            cout << "The Surface of the triangle is=";
            cout << sum;
            system ("PAUSE");
            return 0;
        }
        else( square > triangle );
        {
              int length, width, sum;
              cout << "Enter the length of square here=" ;
              cin >> length;
              cout << "Enter the width of square here=" ;
              cin >> width;
              sum = length * width;
              cout << "The Surface of the square is=";
              cout << sum;
              system ("PAUSE");
              return 0;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if( triangle > square );
    This doesn't need a ;

    > else( square > triangle );
    This doesn't need a condition or a ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    On line 8 you are comparing 2 not-initialized variables - you need to fill th evalues before startign to compare them.
    on line 21 it should be else if
    and then you are missing case when two variables are equal...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Though you don't need a condition for the second if, the proper syntax would be

    else if ( square > triangle )

    There should be no semicolons at the end of if statements. Also square > triangle will not capture the condition where square == triangle which you seem to have missed, so it should actually have been square >= triangle.
    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. Replies: 6
    Last Post: 10-29-2012, 03:33 AM
  2. Replies: 15
    Last Post: 11-28-2011, 11:48 AM
  3. Replies: 4
    Last Post: 07-24-2011, 09:38 PM
  4. Replies: 1
    Last Post: 11-15-2010, 11:14 AM
  5. Replies: 3
    Last Post: 10-02-2007, 09:12 PM