Results 1 to 6 of 6

Thread: Getting the Error "Expression Syntax" in C++

  1. #1
    Join Date
    Aug 2006
    Posts
    139

    Getting the Error "Expression Syntax" in C++

    Hello Friends,
    I have started doing the programming in C++ few weeks ago. I am writing a code for 3 numbers and the program should display the smallest value. I have used the following code :
    if (num1 < num2) && (num1 < num3)
    Is this coding wrong.?? I am getting an error as "expression syntax". Please help me immediately.!!
    Last edited by MarceloQuad; 16-01-2010 at 06:06 PM.
    The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little.
    -Joe Martin

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Getting the Error "Expression Syntax" in C++

    I think that you should use the parentheses. According to the rule, you should use the open and closing parentheses around the if statements. Here is an example :
    if(condition)
    {
    ...
    }
    In your program the coding should be
    if((num1 < num2) && (num1 < num3))
    {
    ...
    }
    Hope that doing this will remove an Error of the 'Expression Syntax'.

  3. #3
    Join Date
    Aug 2006
    Posts
    139

    Re: Getting the Error "Expression Syntax" in C++

    Thanks a lot 'absolute55'. Your solution worked perfectly for me. I am having one more doubt. Here is my code :
    cout<< "(A)dd two numbers: "<<endl;
    cout<< "(S)ubtract two numbers: "<<endl;
    cout<< "(M)ultiply two numbers: "<<endl;
    cout<< "(D)ivide two numbers: "<<endl;
    Can I make a condition so that User can select the first option it will prompt.?? Hope that you can help me now also.!
    The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little.
    -Joe Martin

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Getting the Error "Expression Syntax" in C++

    If you want to do the following actions :
    (A)dd two numbers
    (S)ubtract two numbers
    (M)ultiply two numbers
    (D)ivide two numbers
    then you would have to make a program that should read two numbers from the user, and display the respective answers. Their sum if the user typed a or A as a choice to the above menu etc. You will have to use the Switch Case.

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Getting the Error "Expression Syntax" in C++

    You can use this code for the taking input and doing the respective operations :

    #include <iostream.h>

    float input1, input2, ans
    char input;
    float add();
    float subtract();
    float multiply();
    float divide();

    int main()
    {
    cout << "(A)dd two numbers: \n";
    cout << "(S)ubtract two numbers: \n";
    cout << "(M)ultiply two numbers: \n";
    cout << "(D)ivide two numbers: \n";
    cin >> input;
    if ((input == 'a') || (input == 'A'))
    add();
    if ((input == 's') || (input == 's'))
    subtract();
    if ((input == 'm') || (input == 'M'))
    multiply();
    if ((input == 'd') || (input == 'D'))
    divide();
    cout << "The answer is " << ans << ".\n";
    cin.ignore(1,'\n'); //to view result
    return 0;
    }
    In this code even the user inputs a or A the program will do the addition. So I think that's much better.

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: Taking Input and performing respective operations in C++

    You can also use the following code that can take Input from the user and can perform the operation as desired :

    #include <iostream>
    using namespace std;
    int main()
    {
    int choice, a, b, c;
    cout << "1. Add two numbers\n";
    cout << "2. Subtract two numbers\n";
    cout << "3. Multiply two numbers\n";
    cout << "4. Divide two numbers\n\n";
    cout << "Enter your selection: ";
    cin >> choice;
    cout << "Enter first number: ";
    cin >> a;
    cout << "Enter second number: ";
    cin >> b;
    if(choice == 1)
    c = a + b;
    if(choice == 2)
    c = a - b;
    if(choice == 3)
    c = a * b;
    if(choice == 4)
    c = a / b;
    cout << "The result is: " << c << endl;
    return 0;
    }

Similar Threads

  1. Replies: 3
    Last Post: 10-01-2014, 10:40 AM
  2. Replies: 4
    Last Post: 12-02-2011, 07:10 AM
  3. Getting Error "invalid syntax" in Python
    By kyosang in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 10:53 PM
  4. Syntax/Semantics for a C++ "Function Template"?
    By Juan-Carlos in forum Software Development
    Replies: 3
    Last Post: 11-11-2009, 06:44 PM
  5. Replies: 1
    Last Post: 11-08-2008, 03:22 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,680,350.68168 seconds with 16 queries