Thread: expected ";" before "else" problem

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    expected ";" before "else" problem

    Hello to everyone
    I´m a beginner and trying to write a code that displays the smallest of three integers. The only mistake I get is "expected ";" before "else" mistake in lines 7.2 and 8.2. But if I put ; before the two "elses" I cut my if statement in the middle, as in this case I get "no "if" before "else"" problem- What's my mistake?

    Code:
    #include <iostream>
    using namespace std;
    int main(){
    int x, y, z, m;
    cin >>x>>y>>z>>m;
    if (x<y and x<z) m=x; cout << x << endl
     else if (z>y) m=y; cout << y << endl
     else m=z; cout << z << endl;
     }
    Thanks in advance to everyone.

  2. #2
    Guest
    Guest
    You need to format your code properly, then these mistakes become apparent:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
        int x, y, z, m;
        cin >> x >> y >> z >> m;
        if (x < y && x < z)
            m = x;
            cout << x << endl // missing ";"
            // also: you shouldn't have multiple statements after an if without placing them inside a { block }
        else if (z > y)
            m = y; cout << y << endl // ";"
        else
            m = z;
            cout << z << endl;
     }
    So remember to terminate each statement with a semi-colon, and use braces to pack multiple statements together:
    Code:
    if (condition) {
       statement;
       statement;
       statement;
    } else if (condition) {
       statement;
    } else {
       statement;
       statement;
    }
    Last edited by Guest; 09-20-2016 at 08:03 AM.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where exactly are you trying to place the semicolons?

    By the way here is your code properly formatted:

    Code:
    #include <iostream>
    
    using namespace std;
     
    int main() {
        int x, y, z, m;
        cin >> x >> y >> z >> m;
    
        if(x < y and x < z) 
            m = x;
    
        cout << x << endl
        
        else if(z > y) 
            m = y;
    
        cout << y << endl
        
        else 
            m = z;
    
        cout << z << endl;
    }
    You should avoid putting multiple statements on one line until you are much more familiar with the language. Even then I recommend never putting multiple statements on one line. You should always use braces with your control statements. Remember without the braces control statements only encompass one line.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A "Simple" Array/String Problem. "Help" ASAP needed
    By AirulFmy in forum C Programming
    Replies: 10
    Last Post: 08-19-2015, 04:30 AM
  2. Replies: 2
    Last Post: 12-08-2014, 08:12 PM
  3. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread