We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,752 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

what seems to be wrong with this ?c++ if else

#include <iostream>
using namespace std;

int main( )
{
    int num1 , num2 , num3;
  cout << "Please Enter Three Valid Integers Using The Keyboard";
  cin >>  num1 , num2 , num3;
  if {num1 => num2} && {num2 => num3} else num1 + num2;
  if {num2 => num1} && {num1 => num3} else num2 + num1;
  if {num3 => num1} && {num1 => num2} else num3 + num1;
  if {num3 => num2} && {num2 => num1} else num3 + num2;
  cout << " The Maximum Sum Of These Numbers Is: "<< num1 , num2, num3 endl;
  
  system("pause");
  Return 0 ;
}

can anyone tell me what seems to be wrong with it ?

i get the error - expected `(' before '{' token

can someone please run me through it and how i can fix it thanks :)

5
Contributors
5
Replies
1 Day
Discussion Span
2 Years Ago
Last Updated
6
Views
Question
Answered
Deepo
Newbie Poster
5 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 72
Solved Threads: 30
Skill Endorsements: 0

Hi

Your code will now compile, so you now can see your programs logic...

#include <iostream>
using namespace std;

int main()
{
  int num1 , num2 , num3;
  cout << "Please Enter Three Valid Integers Using The Keyboard\n";
  cout << "enter interger 1\n";
  cin >>  num1;
  cout << "enter interger 2\n";
  cin >>  num2;
  cout << "enter interger 3\n";
  cin >>  num3;

  //First IF statement
  if ((num1 >= num2) && (num2 >= num3))
  {
      // We only come if above condition is met
      // You need to read about logical operators

      // We need to do something here...
      std::cout << "First IF statement is met...\n" << std::endl;

  }
  else
  {
      // We end up here if the above condition is not met
      int sum1;
      sum1 = num1 + num2;
      std::cout << "First IF statement is not met" << std::endl;
  }

  //Second IF statement
  if ((num2 >= num1)  && (num1 >= num3))
  {

  }
  else
  {
      num2 + num1;
  }

  //Third IF statement
  if ((num3 >= num1)  &&  (num1 >= num2))
  {

  }
  else
  {
     num3 + num1;
  }
  //Four IF statement
  if ((num3 >= num2) && (num2 >= num1))
  {

  }
  else
  {
     num3 + num2;
  }

  cout << " The Maximum Sum Of These Numbers Is: "<< num1 << num2 << num3 << endl;

  system("pause");
  return 0 ;
}

OK now work through the program & there are still some corrections to do

ziggystarman
Junior Poster in Training
79 posts since Sep 2010
Reputation Points: 23
Solved Threads: 5
Skill Endorsements: 0

I saw a major syntax error. Maybe you were thinking Java?

The proper syntax for an if statement is:

if (condition1, condition2, condition3...) {statement1; statement2;}

If it's simpler, here it is on several lines:

if (condition1, condition2, condition3...)
{
statement1;
statement2;
}

If you wanted something to be done if it were evaluated false, just add an else at the end, followed by braced code:

if (condition1, condition2, condition3...)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}

Notice that the if...else statement doesn't require a semicolon (;) after the braces... it acts like a function.

Try fixing up your code with the right syntax.

Remember - you need the conditions in parentheses (()) and the function in braces({}).

Also, something I just realized is that you have no function for the "if" and the else has no condition.

Hope that helps!

emanfman
Newbie Poster
24 posts since Apr 2011
Reputation Points: 27
Solved Threads: 1
Skill Endorsements: 0

Hi emanfman,

I agree with MOST of your post, but the syntax you give for the "if" statement is, while it will probably compile, misleading at best.

There is no multiple conditions separated by commas in an "if" statement. The syntax of the multiple types of "if" statement is as follows:

if ( condition ) statement1;

if ( condition ) statement1; else statement2;

if ( condition )
{
   statement1;
   statement2;
}

if ( condition )
{
   statement1;
   statement2;
}
else
{
   statement3;
   statement4;
}

Only the "for" statement has three parts to it, and they are separated by semicolons and not commas.

Tom

bignm
Newbie Poster
2 posts since Oct 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0

Hi emanfman,

I agree with MOST of your post, but the syntax you give for the "if" statement is, while it will probably compile, misleading at best.

There is no multiple conditions separated by commas in an "if" statement. The syntax of the multiple types of "if" statement is as follows:

if ( condition ) statement1;

if ( condition ) statement1; else statement2;

if ( condition )
{
   statement1;
   statement2;
}

if ( condition )
{
   statement1;
   statement2;
}
else
{
   statement3;
   statement4;
}

Only the "for" statement has three parts to it, and they are separated by semicolons and not commas.

Tom

From the near JavaScript example he gave, with the conditions separated by &&, I use a comma because it makes the "if" statement true only if ALL the conditions are true. Technically, it is still correct to use the && though.

emanfman
Newbie Poster
24 posts since Apr 2011
Reputation Points: 27
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 2 Years Ago by emanfman, bignm, ziggystarman and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0719 seconds using 2.75MB