What is wrong with these functions?????

I am a beginner programmer. I started using Xcode for mac instead of Dev C++ like I always do because I was bored and I made this program. The problem is that when I go to a certain function, the program will take me through all four functions inputing numbers and will only do the last function. This is my code...



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

int addition ()
{
system("clear");
cout << "Enter the first number: " << endl;
double a;
cin >> a;
cout << "Enter the second number: " << endl;
double b;
cin >> b;
system("clear");
cout << "The first number you entered was " << a << "." << endl;
cout << "The second number you entered was " << b << "." << endl;
cout << a << " + " << b << " = " << a+b << endl;
return 0;
}

int subtraction ()
{
system("clear");
cout << "Enter the first number: " << endl;
double c;
cin >> c;
cout << "Enter the second number: " << endl;
double d;
cin >> d;
system("clear");
cout << "The first number you entered was " << c << "." << endl;
cout << "The second number you entered was " << d << "." << endl;
cout << c << " - " << d << " = " << c-d << endl;
return 0;
}

int division ()
{
system("clear");
cout << "Enter the first number: " << endl;
double e;
cin >> e;
cout << "Enter the second number: " << endl;
double f;
cin >> f;
system("clear");
cout << "The first number you entered was " << e << "." << endl;
cout << "The second number you entered was " << f << "." << endl;
cout << e << " divided by " << f << " = " << e/f << endl;
return 0;
}

int multiplication ()
{
system("clear");
cout << "Enter the first number: " << endl;
double g;
cin >> g;
cout << "Enter the second number: " << endl;
double h;
cin >> h;
system("clear");
cout << "The first number you entered was " << g << "." << endl;
cout << "The second number you entered was " << h << "." << endl;
cout << g << " x " << h << " = " << g*h << endl;
return 0;

}

int exit ()
{
cout << "Do you want to do another problem? ";
int x;
cin >> x;
if (x= 1);
{
int main ();
}

if (x= 2);
{
system("clear");
cout << "Thank-you for using Mini Calculator 3.0!!!";
}
return 0;
}

int main ()
{
system("clear");
cout << "WELCOME TO MIN CALCULATOR 3.0!!!!!" << endl;
cout << "Menu Numbers: " << endl;
cout << "1 - Addition" << endl;
cout << "2 - Subtraction" << endl;
cout << "3 - Division" << endl;
cout << "4 - Multiplication" << endl;

cout << "Which equation do you want to perform? ";
int z;
cin >> z;
if ( z=1)
{
addition ();
}


if ( z=2);
{
subtraction ();
}


if (z=3);
{
division ();
}


if (z=4);
{
multiplication ();
}


exit ();

return 0;
}



PLease help me.
exit is the name of a function in the C runtime library. It's not a good name to choose for one of your own functions.
Your if statements have assignment rather than a check for equality. Surely your compiler will have warned you.

 
    if (z=2)


should be
 
    if (z==2)

Your problem is that your if statements are using a single equals sign, which is known as 'gets' in C/C++ because it assigns a value to a variable. For comparison operations, you need to use two equals signs.

For example:

1
2
3
4
if(foo == bar)
{
    //Do something
}


A few other pointers (No pun intended) to help make you a better coder:

All of your functions are of type 'int,' and they return zero when they end, even when they don't necessitate a return type. For functions of this nature, simply declare the return type as 'void' rather than 'int' and don't return anything.

1
2
3
4
void fooBar()
{
    //Do something
}


The system("clear") command is system specific. It will only work on Unix based operating system which know what the "clear" command is, so it's bad practice to use it. Such an instruction completely breaks the portability of your code. (Not a big deal for that project, but in the future you'll want to keep things like this in mind.)

Lastly, please use code tags when posting code in the forums. It makes your code a lot easier to read, which increases your chances of getting help.

Cheers.

Last edited on
Topic archived. No new replies allowed.