problem with ; at end of if statement

This is a discussion on problem with ; at end of if statement within the C++ Programming forums, part of the General Programming Boards category; if (q==s); cout<<fixed<<setprecision(5)<<a*z<<endl; else cout<<fixed<<setprecision(5)<<a*x<<endl; error: else without a previous if Thanks in advance...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    1

    problem with ; at end of if statement

    if (q==s);
    cout<<fixed<<setprecision(5)<<a*z<<endl;
    else cout<<fixed<<setprecision(5)<<a*x<<endl;


    error: else without a previous if

    Thanks in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    if (q==s);
    That semi-colon shouldn't be there.
    If you understand what you're doing, you're not learning anything.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    I fixed your thread title
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Compiler must generate an error in this case as else is being used without any if.
    What compiler reading is;
    It reads your if statement and as you didn't put any opening, closing bracket, so it reads ; as a statement and ignores remaining... It then takes cout object as a separate line and when it looks at else, it generates an error as there is no preceding if....
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 02:05 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 04-27-2005, 11:50 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21