#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 :)

Dani AI

Generated

Quick diagnosis (why the compiler complained "expected '(' before '{' token"): the if condition must be enclosed in parentheses, not braces; the relational operator is >= not =>; input uses >> (not commas); output uses << (not commas); return is lowercase; and expressions like num1 + num2; do nothing unless their result is stored or printed. already corrected the compilation syntax, and / covered the if grammar — the remaining gap is a clear, reliable way to compute the value the thread name suggests: the maximum sum of any two of three integers.

A compact, easy-to-read approach is: compute the total of all three and subtract the smallest value. This avoids many branch mistakes and is efficient.

#include <iostream>
#include <algorithm>

int main() {
    int a, b, c;
    if (!(std::cin >> a >> b >> c)) return 1;
    int sum_two = a + b + c - std::min({a, b, c}); // requires C++11
    std::cout << "Maximum sum of two numbers: " << sum_two << '\n';
    return 0;
}

Notes and alternatives: on pre-C++11 compilers use std::min(std::min(a,b), c) instead of std::min({a,b,c}). Another clear option is to put the three values into an array and std::sort it, then add the last two elements. Avoid system("pause") for portability and rely on the debugger/compiler messages instead.

Quick debugging checklist (ties to earlier replies): compile with warnings enabled (for example -Wall), inspect the exact error line, ensure if (condition) uses parentheses, use >= not =>, chain cin >> and cout <<, and remember return is case-sensitive. This addresses both the syntax errors called out by and the example fixes from .

Recommended Answers

All 5 Replies

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

commented: Don't fix peoples code. Guide them to their own solution. -3

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!

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.