You are here:

C++/syntax error before "else"

Advertisement


Question
#include<stdio.h>
#include<conio.h>

main()
{
     int rFp, lFp, rRp, lRp;
     
     printf("Input Right Front Pressure: ");
     scanf("%d",&rFp);
     printf("Input Left Front Pressure: ");
     scanf("%d",&lFp);
     printf("Input Right Rear Pressure: ");
     scanf("%d",&rRp);
     printf("Input Left Rear Pressure: ");
     scanf("%d",&lRp);      
     {
     if (rFp==lFp && rRp==lRp);
     printf("Inflation is OK!");
     else (rFp != lFp && rRp != lRp);
     printf("Inflation is NOT OK!!");
     }
     getch();
     }

Thanks!!

Answer
Hi,

The syntax error is the ";" after the if clause - you don't use one there. You have a problem with the else clause too; in order to use another condition there (rFp != lFp && rRp != lRp) you need to have another if. Also, I think you want an "or" condition in the else clause, so you would have a problem if either the front tires were unequal or the rear tires were. So

  if (rFp==lFp && rRp==lRp);
  printf("Inflation is OK!");
  else (rFp != lFp && rRp != lRp);
  printf("Inflation is NOT OK!!");

becomes

  if (rFp==lFp && rRp==lRp)
     printf("Inflation is OK!");
  else  if(rFp != lFp && rRp != lRp)  //I think you mean || instead of &&
     printf("Inflation is NOT OK!!");

You could also leave off the second conditional statement and get the same result:

  if (rFp==lFp && rRp==lRp)
     printf("Inflation is OK!");
  else
     printf("Inflation is NOT OK!!");

Happy programming!

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Oshi Jager

Expertise

I can: - help debug code, or make it more efficient - help with concepts, key terms and commands, and object design/inheritance - give suggestions for subroutines or object calls - suggest overall organization techniques and/or data organization I have a fairly good concept of program flow, so if you have ideas, but don't know how to get started... I've had to do a lot of that, and I can help.

Experience

Primarily, 36 units in Computer Science at university. Also, I started programming games in C and C++ back when the Commodore 64 was the best game machine out there, and that makes me old... for those of you who don't know the reference. I have intermittently kept up with my programming, so try me out.

Organizations
As of 2 years ago: Association for Computing Machinery (ACM)

Education/Credentials
Related classes at Cal State University: - Object Oriented Programming (3 units) [basically C/C++] - Database Design and Implementation (3) [all theory and SQL oriented] - Discrete Mathematics (3) [pure theory] - Computer Organization (3) [cpu functionality and programming] - Data Structures (3) [in C++, but also in general theory] - Computer Architecture (3) [the PC from top to bottom] - Java Programming (3) - Programming Languages (3) [history] - Telecommunications and Networking (3)

©2016 About.com. All rights reserved.