1,105,427 Community Members

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

Member Avatar
Deepo
Newbie Poster
5 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#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 :)

Member Avatar
Caligulaminus
Junior Poster
106 posts since Feb 2011
Reputation Points: 35 [?]
Q&As Helped to Solve: 30 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
ziggystarman
Junior Poster in Training
79 posts since Sep 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
-3
 

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

Member Avatar
emanfman
Newbie Poster
24 posts since Apr 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

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!

Member Avatar
bignm
Newbie Poster
2 posts since Oct 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
emanfman
Newbie Poster
24 posts since Apr 2011
Reputation Points: 17 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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.

Question Answered as of 2 Years Ago by emanfman, bignm, ziggystarman and 1 other
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: