basic funcion call problem

I am trying to learn C++ on my own and have come to a stumbling block that I think is just purely syntax related but I am not sure.  Essentially I am trying to call a function that is outside of main().  When I do I get the following error:

error: initializer expression list treated as compound expression

According to the error message is has something to do with the way that I am calling menuOp() in the 'while' loop.

Thanks for your help,
Brian


Here is the code:

#include <iostream>
using namespace std;

int main ()
{
      char option;
      float num1;
      float num2;
      
      cout << "***Welcome to the Mini-Calc!***\n\n";
      cout << "This program takes 2 numbers and an operation choice and gives you a ";
      cout << "result. Just follow the onscreen propts and menu.\n";
      
      while (option != '0')
      {
            cout << "<----------MENU---------->\n";
            cout << "1) '+' Add both numbers\n";
            cout << "2) '-' Subtract second number from first\n";
            cout << "3) '*' Multiply both numbers\n";
            cout << "4) '/' Divide Second number into first\n";
            cout << "5) '<a>' Average the two numbers.\n";
            cout << "0) QUIT";
            cout << "Please enter your operation choice: ";
            cin >> option;
            cout << "Please enter the first number: ";
            cin >> num1;
            cout << "Please enter the second number: ";
            cin >> num2;
            float menuOp(option, num1, num2);
            
            /*Clear the screen. This is actually VERY OS dependant to do as C++ has no
            * funtion in the STL to do this since it really doesn't even know about a
            * terminal or console much less that it has to be cleared. So it has to be
            * written as a terminal control command and as such requires 'ptintf'.
            */
            printf( "\33[2J");
      }
      
      cout << "Thank you for playing!\n" << endl;
      return 0;
}

float menuOp(char op, float firstNum, float secondNum)
{
      float result;
      
      switch (op)
      {
            case '1':
                  result = firstNum + secondNum;
                  cout << firstNum << " + " << secondNum << " = " << result << endl;
            case '2':
                  result = firstNum - secondNum;
                  cout << firstNum << " - " << secondNum << " = " << result << endl;
            case '3':
                  result = firstNum * secondNum;
                  cout << firstNum << " * " << secondNum << " = " << result << endl;
            case '4':
                  result = firstNum / secondNum;
                  cout << firstNum << " / " << secondNum << " = " << result << endl;
            case '5':
                  result = (firstNum + secondNum) / 2;
                  cout << "The <a> of " << firstNum << " and " << secondNum << " is " << result << endl;
            default:
                  cout << "You entered an incorrect menu choice! \n";
                  
            return 0.0;
      }
}
LVL 1
bnblazerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You need to declare and use your function properly, i.e.

#include <iostream>
using namespace std;

// declaration
float menuOp(char op, float firstNum, float secondNum);

int main ()
{
     char option;
     float num1;
     float num2;
     
     cout << "***Welcome to the Mini-Calc!***\n\n";
     cout << "This program takes 2 numbers and an operation choice and gives you a ";
     cout << "result. Just follow the onscreen propts and menu.\n";
     
     while (option != '0')
     {
          cout << "<----------MENU---------->\n";
          cout << "1) '+' Add both numbers\n";
          cout << "2) '-' Subtract second number from first\n";
          cout << "3) '*' Multiply both numbers\n";
          cout << "4) '/' Divide Second number into first\n";
          cout << "5) '<a>' Average the two numbers.\n";
          cout << "0) QUIT";
          cout << "Please enter your operation choice: ";
          cin >> option;
          cout << "Please enter the first number: ";
          cin >> num1;
          cout << "Please enter the second number: ";
          cin >> num2;
          menuOp(option, num1, num2); // call it - 'float' is wrong here
         
          /*Clear the screen. This is actually VERY OS dependant to do as C++ has no
          * funtion in the STL to do this since it really doesn't even know about a
          * terminal or console much less that it has to be cleared. So it has to be
          * written as a terminal control command and as such requires 'ptintf'.
          */
          printf( "\33[2J");
     }
     
     cout << "Thank you for playing!\n" << endl;
     return 0;
}

float menuOp(char op, float firstNum, float secondNum)
{
     float result;
     
     switch (op)
     {
          case '1':
               result = firstNum + secondNum;
               cout << firstNum << " + " << secondNum << " = " << result << endl;
          case '2':
               result = firstNum - secondNum;
               cout << firstNum << " - " << secondNum << " = " << result << endl;
          case '3':
               result = firstNum * secondNum;
               cout << firstNum << " * " << secondNum << " = " << result << endl;
          case '4':
               result = firstNum / secondNum;
               cout << firstNum << " / " << secondNum << " = " << result << endl;
          case '5':
               result = (firstNum + secondNum) / 2;
               cout << "The <a> of " << firstNum << " and " << secondNum << " is " << result << endl;
          default:
               cout << "You entered an incorrect menu choice! \n";
               
          return 0.0;
     }
}
jkrCommented:
Or, did you mean to

float response = menuOp(option, num1, num2);

?

The above would declare a variable called 'response' and assign it the return value of 'menuOp()'.
Harisha M GEngineerCommented:
jkr is basically right...

(Tested in Dev C++)

#include <iostream>
using namespace std;

float menuOp(char op, float firstNum, float secondNum);

int main ()
{
     char option;
     float num1;
     float num2;
     
     cout << "***Welcome to the Mini-Calc!***\n\n";
     cout << "This program takes 2 numbers and an operation choice and gives you a ";
     cout << "result. Just follow the onscreen propts and menu.\n";
     
     while (option != '0')
     {
          cout << "<----------MENU---------->\n";
          cout << "1) '+' Add both numbers\n";
          cout << "2) '-' Subtract second number from first\n";
          cout << "3) '*' Multiply both numbers\n";
          cout << "4) '/' Divide Second number into first\n";
          cout << "5) '<a>' Average the two numbers.\n";
          cout << "0) QUIT";
          cout << "Please enter your operation choice: ";
          cin >> option;
          cout << "Please enter the first number: ";
          cin >> num1;
          cout << "Please enter the second number: ";
          cin >> num2;
          option = menuOp(option, num1, num2);
         
          /*Clear the screen. This is actually VERY OS dependant to do as C++ has no
          * funtion in the STL to do this since it really doesn't even know about a
          * terminal or console much less that it has to be cleared. So it has to be
          * written as a terminal control command and as such requires 'ptintf'.
          */
          printf( "\33[2J");
     }
     
     cout << "Thank you for playing!\n" << endl;
     return 0;
}

float menuOp(char op, float firstNum, float secondNum)
{
     float result;
     
     switch (op)
     {
          case '1':
               result = firstNum + secondNum;
               cout << firstNum << " + " << secondNum << " = " << result << endl;
          case '2':
               result = firstNum - secondNum;
               cout << firstNum << " - " << secondNum << " = " << result << endl;
          case '3':
               result = firstNum * secondNum;
               cout << firstNum << " * " << secondNum << " = " << result << endl;
          case '4':
               result = firstNum / secondNum;
               cout << firstNum << " / " << secondNum << " = " << result << endl;
          case '5':
               result = (firstNum + secondNum) / 2;
               cout << "The <a> of " << firstNum << " and " << secondNum << " is " << result << endl;
          default:
               cout << "You entered an incorrect menu choice! \n";
               
          return 0.0;
     }
}
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jkrCommented:
>>jkr is basically right...

Then, why did you repost the whole code? ;o)
Harisha M GEngineerCommented:
jkr,
> float response = menuOp(option, num1, num2);
>>           option = menuOp(option, num1, num2);
jkrCommented:
'menuOp()' returns a float. 'option' is the 1st argument to 'menuOp()' and is read via

        cin >> option;

in 'main()'.
Harisha M GEngineerCommented:
It has more bugs than I thought....

There were no break; statements for the switch block, and I also changed while to do.. while

_______________

#include <iostream>
using namespace std;

float menuOp(char op, float firstNum, float secondNum);

int main ()
{
    char option;
    float num1;
    float num2;
   
    cout << "***Welcome to the Mini-Calc!***\n\n";
    cout << "This program takes 2 numbers and an operation choice and gives you a ";
    cout << "result. Just follow the onscreen propts and menu.\n";
   
    do
    {
        cout << "<----------MENU---------->\n";
        cout << "1) '+' Add both numbers\n";
        cout << "2) '-' Subtract second number from first\n";
        cout << "3) '*' Multiply both numbers\n";
        cout << "4) '/' Divide Second number into first\n";
        cout << "5) '<a>' Average the two numbers.\n";
        cout << "0) QUIT\n";
        cout << "\nPlease enter your operation choice: ";
        cin >> option;
        if(option != '0')
        {
                cout << "Please enter the first number: ";
                cin >> num1;
                cout << "Please enter the second number: ";
                cin >> num2;
                cout << menuOp(option, num1, num2);
        }
        /*Clear the screen. This is actually VERY OS dependant to do as C++ has no
        * funtion in the STL to do this since it really doesn't even know about a
        * terminal or console much less that it has to be cleared. So it has to be
        * written as a terminal control command and as such requires 'ptintf'.
        */
        printf( "\33[2J");
    }while (option != '0');
   
    cout << "Thank you for playing!\n" << endl;
    return 0;
}

float menuOp(char op, float firstNum, float secondNum)
{
    float result;
   
    switch (op)
    {
    case '1':
       result = firstNum + secondNum;
       cout << firstNum << " + " << secondNum << " = " << result << endl;
       break;
    case '2':
       result = firstNum - secondNum;
       cout << firstNum << " - " << secondNum << " = " << result << endl;
       break;
    case '3':
       result = firstNum * secondNum;
       cout << firstNum << " * " << secondNum << " = " << result << endl;
       break;
    case '4':
       result = firstNum / secondNum;
       cout << firstNum << " / " << secondNum << " = " << result << endl;
       break;
    case '5':
       result = (firstNum + secondNum) / 2;
       cout << "The <a> of " << firstNum << " and " << secondNum << " is " << result << endl;
       break;
    default:
       cout << "You entered an incorrect menu choice! \n";
       return 0.0;
    }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bnblazerAuthor Commented:
Thanks guys!  I had caught the 'break;' issue just after I got it to compile using the help from the first answer and it failed my tests.

As a point of learning though.  Why the do...while instead of just the while?  And more broadly, what are the general rules on selecting one over the other?

Thanks again.  It is the polite responses that are aimed at helping guys like me learn instead of feeling stupid that make this place a pleasure for me.

Brian
Harisha M GEngineerCommented:
> Why the do...while instead of just the while?  And more broadly, what are the general rules on selecting one over the other?

When a code needs to run atleast once, it is a good practice to use do..while().
When the number of times it runs is known before in hand, for() loop is better. When neither of these hold, use while()

However, it is just a direction, and you are free to use anyone anywhere
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.