Jump to content

Calculator Fix

anagiotis567

Well , First I should state that i am very,very new to c++.I haven't even watched all the tutorials from the series i am following yet.But, I started working on a very simple calculator for addition,substraction,division (at least until i learn how to implement more) but something went very wrong with my code.Can you help me debug it to work with the features at its current state? The code is below.

 
#include<iostream>using namespace std;int add, substract, divide;double othernum1, othernum2, otherresult;int main(){cout << "Please Enter The First Number:";cin >> othernum1;cout << "Please Enter The Second Number:";cin >> othernum2;cout << "Please Enter What You Want To Do:";if(std::cin >> substract)cout << othernum1 << "-" << othernum2 << "=" << otherresult;else (std::cin >> add);    {otherresult = othernum1 - othernum2;       }       {     otherresult = othernum1 + othernum2;     cout << othernum1 << "+" << othernum2 << "=" << otherresult;     }     (std::cin >> divide);{    otherresult = othernum1 / othernum2;    cout << othernum1 << "/" << othernum2 << "=" << otherresult;}return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

You might want something more like this:

 

#include <iostream>using namespace std;char method;double othernum1, othernum2;int main(){	cout << "Please Enter The First Number:";	cin >> othernum1;	cout << "Please Enter The Second Number:";	cin >> othernum2;	cout << "Please Enter What You Want To Do:";	cin >> method;	if (method == '-') cout << endl << othernum1 << " - " << othernum2 << " = " << (othernum1 - othernum2) << endl << endl;	else if (method == '+') cout << endl << othernum1 << " + " << othernum2 << " = " << (othernum1 + othernum2) << endl << endl;	else if (method == '/') cout << endl << othernum1 << " / " << othernum2 << " = " << (othernum1 / othernum2) << endl << endl;	else if (method == '*') cout << endl << othernum1 << " * " << othernum2 << " = " << (othernum1 * othernum2) << endl << endl;	return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

You might want something more like this:

 

#include <iostream>using namespace std;char method;double othernum1, othernum2;int main(){	cout << "Please Enter The First Number:";	cin >> othernum1;	cout << "Please Enter The Second Number:";	cin >> othernum2;	cout << "Please Enter What You Want To Do:";	cin >> method;	if (method == '-') cout << endl << othernum1 << " - " << othernum2 << " = " << (othernum1 - othernum2) << endl << endl;	else if (method == '+') cout << endl << othernum1 << " + " << othernum2 << " = " << (othernum1 + othernum2) << endl << endl;	else if (method == '/') cout << endl << othernum1 << " / " << othernum2 << " = " << (othernum1 / othernum2) << endl << endl;	else if (method == '*') cout << endl << othernum1 << " * " << othernum2 << " = " << (othernum1 * othernum2) << endl << endl;	return 0

Thank You!!!!

Link to comment
Share on other sites

Link to post
Share on other sites

Or this :P A little bit longer but switch statements would be a better choice.

#include<iostream>using namespace std;char method;double othernum1, othernum2, otherresult;int main(){	cout << "Please Enter The First Number: ";	cin >> othernum1;	cout << "Please Enter The Second Number: ";	cin >> othernum2;	cout << "Please Enter What You Want To Do: ";	cin >> method;	switch(method)	{	case'-':		cout << endl << othernum1 << " - " << othernum2 << " = " << (othernum1 - othernum2) << endl;		break;	case'+':		cout << endl << othernum1 << " + " << othernum2 << " = " << (othernum1 + othernum2) << endl;		break;	case'/':		cout << endl << othernum1 << " / " << othernum2 << " = " << (othernum1 / othernum2) << endl;		break;	case'*':		cout << endl << othernum1 << " * " << othernum2 << " = " << (othernum1 * othernum2) << endl;		break;	default:		cout << "command not found!\n";	};	return 0;}

Desktop: Fractel Design R5 - i5 4670K @ stock - Asrock Z97 Extreme 4 - 16GB @ 1600MHz - Asus GTX 970 - Corsair RM650 - OCZ Vector 150 240GB - WD Blue 500GB - Noctua NH-D15

Peripherals & OS: KBC Poker 2 MX Blue - Zowie FK2 - Audio-technica ATH-M50s - Audioengine D1- Windows 10 Pro Monitor: Asus PA248Q 1920x1200

Link to comment
Share on other sites

Link to post
Share on other sites

Or, abstracted out, because why should we use a billion cout statements:

#include<iostream>#include<stdio.h>using namespace std;char method;double othernum1, othernum2, result;int main(){	cout << "Please Enter The First Number: ";	cin >> othernum1;	cout << "Please Enter The Second Number: ";	cin >> othernum2;	cout << "Please Enter What You Want To Do: ";	cin >> method;	switch(method)	{	case '-':		result = othernum1 - othernum2;		break;	case '+':		result = othernum1 + othernum2;		break;	case '/':		result = othernum1 / othernum2;		break;	case '*':		result = othernum1 * othernum2;		break;	default:		cout << "command not found!\n";		return 2;	};	printf("%g %c %g = %g", othernum1, method, othernum2, result);	return 0;}

--Neil Hanlon

Operations Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×