troubleshooting Question

basic funcion call problem

Avatar of bnblazer
bnblazer asked on
C++
9 Comments2 Solutions373 ViewsLast Modified:
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;
      }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros