HelpCalculator using functions

Posts 1–4 of 4 · Page 1 of 1
Calculator using functions
I am trying to make a calculator using functions, I have a subtraction function and a addition function. I cant really get this program to work and I am in need of some help.
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int addition(int a, int b);
int subtraktion(int x, int z);

int main()
{
int number1, number2;
string type;

cout << "Insert first value: \n";
cin >> number1;
cout << "Insert type";
cin >> type;
cout << "Insert second value: \n";
cin >> number2;
if (type == +);
{
cout << addition(number1, number2);
}
else if (type == -);
{
cout << subtraktion(number1, number2);
}
else
{
cout << "Wrong type";
}

system("PAUSE");
}

int addition(int a, int b)
{
return a + b;
}

int subtraktion(int x, int z)
{
return x - z;
}
Hey, correct those and report back.
string type;----> char type;
Secondly all your if's needs to be this way

if (type=='+')

Also you aren't doing anything inside your fuctions. Read more about fuctions .


edit : there you go
#include <iostream>
using namespace std;


void sum(double a, double b)
{
cout << a + b << endl;
}

void substruct(double a, double b)
{
cout << a - b << endl;

}

void multyple(double a, double b)
{
cout << a * b << endl;

}

void divide(double a, double b)
{
cout << a / b << endl;

}




int main()
{


double x, y,result;
char type;
cout << "Please enter what you would like to perform : + ,-,*,/ followed by enter\n";
cin >> type;
cout << "Please type 2 numbers each followed by enter\n";
cin >> x >> y;
if (type == '+')
{
sum( x, y);
}
if (type == '-')
{
substruct( x, y);
}
if (type == '*')
{
multyple( x, y);
}
if (type == '/')
{
divide( x, y);
}
return 0;
}
I fixed your code. Basically I just added quotation marks to your if -statements and removed few unneeded semicolons.
Code:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int addition(int a, int b);
int subtraktion(int x, int z);

int main()
{
	int number1, number2;
	string type;

	cout << "Insert first value: \n";
	cin >> number1;
	cout << "Insert type";
	cin >> type;
	cout << "Insert second value: \n";
	cin >> number2;
	if (type == "+")
	{
		cout << addition(number1, number2);
	}
	else if (type == "-")
	{
		cout << subtraktion(number1, number2);
	}
	else
	{
		cout << "Wrong type";
	}

	system("PAUSE");
}

int addition(int a, int b)
{
	return a + b;
}

int subtraktion(int x, int z)
{
	return x - z;
}
Quote Originally Posted by PakaluPapito View Post
Also you aren't doing anything inside your fuctions. Read more about fuctions .
He is actually doing something inside his functions, he's adding/subtracting numbers and then returning the value.
Quote Originally Posted by MikeeyJJ View Post
I fixed your code. Basically I just added quotation marks to your if -statements and removed few unneeded semicolons.
Code:
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;

int addition(int a, int b);
int subtraktion(int x, int z);

int main()
{
	int number1, number2;
	string type;

	cout << "Insert first value: \n";
	cin >> number1;
	cout << "Insert type";
	cin >> type;
	cout << "Insert second value: \n";
	cin >> number2;
	if (type == "+")
	{
		cout << addition(number1, number2);
	}
	else if (type == "-")
	{
		cout << subtraktion(number1, number2);
	}
	else
	{
		cout << "Wrong type";
	}

	system("PAUSE");
}

int addition(int a, int b)
{
	return a + b;
}

int subtraktion(int x, int z)
{
	return x - z;
}

He is actually doing something inside his functions, he's adding/subtracting numbers and then returning the value.
Oh yea you are right didn't even see that .
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us