Hi again, I'm back, with another question about my calculator program (which I got to work, yay!)

Why does the following program not work (when the one after it does):

#include <iostream>

using namespace std;
float x;
float y;
char z;
    
int add()
{
    cout << x << " " << z << " " << y << " = " << x + y << endl;
}

int subtract()
{
    cout << x << " " << z << " " << y << " = " << x - y << endl;
}

int divide()
{
    cout << x << " " << z << " " << y << " = " << x / y << endl;
}

int multiply()
{
    cout << x << " " << z << " " << y << " = " << x * y << endl;
}

int main() 
{
    cout << "Please enter an expression: " << endl;
    cin >> x;
    cin >> z;
    cin >> y;
 
    if(z == '+')
    {
         int add();
    }
    if(z == '/')
    {
         int divide();
    }
    if(z == '-') 
    {
         int subtract();
    }
    if(z == '*') 
    {
         int multiply();
    }                                   
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();  
    //^^ for Dev C++ compiler users, so the program stays open    
    return 0;                  

}

when this one does:

#include <iostream>

using namespace std;
float x;
float y;
char z;
    
int main() 
{
    cout << "Please enter an expression: " << endl;
    cin >> x;
    cin >> z;
    cin >> y;
 
    if(z == '+')
    {
    cout << x << " " << z << " " << y << " = " << x + y << endl;
    }
    if(z == '/')
    {
    cout << x << " " << z << " " << y << " = " << x / y << endl;
    }
    if(z == '-') 
    {
    cout << x << " " << z << " " << y << " = " << x - y << endl;
    }
    if(z == '*') 
    {
    cout << x << " " << z << " " << y << " = " << x * y << endl;
    }                                   
    cin.clear();
    cin.ignore(255, '\n');
    cin.get();
    //^^ for Dev C++ compiler users, so the program stays open      
    return 0;                  

}

The only difference between the two is that the first used function calls to pre-stated functions, and the second has the entire function stated in the if statement, without use of function calls. The first compiles okay, but when you actually try to use the program... well nothing happens. Anyone know why?

Recommended Answers

All 5 Replies

Because you aren't calling the functions, you're just declaring them.
Get rid of the int to turn them into calls.

You should also look up function parameters. You should pass x, y and z as parameters to the functions instead of making them global variables.

Wow, thanks I feel kinda stupid for doing that. But another question then, what is so bad about global variables? I find them much easier to deal with, most of the time. There are exceptions, but in this case is it not easier to declare them once instead of over and over in each function declaration?

In little programs like your it doesn't show why its so bad. Just trust us, when
we say do not use global variables. It leads to much complication, and ugly code.
Compare your code with this non global variable one :

#include <iostream>
using namespace std;

int main(){
 char op;
 float in1,in2;
 cin >> op;
 cin >> in1 >> in2 ;
 switch(op){
   case '*' : cout << in1 * in2 << endl; break;
   case '/' : cout << in1 / in2 << endl; break;
   case '-' : cout << in1 - in2 << endl; break;
   case '+' : cout << in1 + in2 << endl; break;
   default : cout << "Invalid\n";
 }
 return 0;
}

The above program is easier and better to read than yours. One of the reason why
that is, is because I didn't use global variables

One problem is name collision :

#include <iostream>
int x = 0;
int main(){
 int x = 0;
 x = 12; //which one did you want to change?
}

if you rearrange the order 'op', 'in1', and 'in2' are stated it will work. It is much prettier than mine. For some reason I had had trouble when using float variables with switch statements

Thanks for the help! I also incorporated other functions, and using switch is much more efficient!

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
 do
 {
  cout << "Enter any expression with +, -, *, /, ^, %,\n\nor (number to be rooted) _ 0 '" <<
  "for square roots \n\n";
  char operation;
  float var1, var2;
  cin >> var1 >> operation >> var2;
  int varr1, varr2;
  varr1 = var1, varr2 = var2;
  cout << " " << endl;
  switch(operation)
  {
   default:
   cout << "Error - Operator not accepted\n" << endl; break;
   case '*':
   cout << "= " << var1 * var2 << "\n" << endl; break;
   /*case '/':
   cout << "= " << var1 / var2 << "\n" << "OR " << varr1 / varr2 <<
        " with a remainder of " << varr1 % varr2 << "\n" << endl; break;*/
   case '+':
   cout << "= " << var1 + var2 << "\n" << endl; break;
   case '-':
   cout << "= " << var1 - var2 << "\n" << endl; break;
   case '=':
        if(var1 == var2)
        {cout << "Yes, it does!\n"  << endl;}
        else
        {cout << "No, it doesn't!\n" << endl;}
   case '^':
   cout << "= " << var1 * (var2 - 1) * var1 << endl; break;
   case '%':
   cout << "= " << (var1 / 100) * var2 << "\n" << endl; break;
   case '_':
   cout << "= " << sqrt(var1) << "\n" << endl; break;
  }
  }while(1 == 1);
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
   return 0;
}
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.