Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Formatting / Dividing by 0 (My first Calculator)

$
0
0
I put parameters to not allow for characters.. but formatting goes out of whack. Try putting a letter as an integer. Need help with getting it to look clean.

Also, I also put parameters so it cannot divide by 0 based on earlier feedback. It says enter a new integer then it crashes!


#include <iostream>
using namespace std;

int sum(int iValue1, int iValue2) {
	return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
	return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
	return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
	return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
	cin >> iValue1;
	do {

	cout << "Enter the next value as an integer:";
	cin >> iValue2;
	if (cin.fail())
		cout << "No integer found. Please use a number" << endl;
		cin.clear(); 
	
	cout << "Enter the operation you want to perform (-,+,/,*):";
	cin >> chOperator;
	
	if (chOperator == '+') {
	cout << "The result is " << sum(iValue1, iValue2) << endl;
	iValue1 = sum(iValue1, iValue2);
	}

	else if (chOperator == '-') {
	cout << "The result is " << subtraction(iValue1, iValue2) << endl;
	iValue1 = subtraction(iValue1, iValue2);
	}

	else if (chOperator == '*') {
	cout << "The result is " << multiply(iValue1, iValue2) << endl;
	iValue1 = multiply(iValue1, iValue2);
	}

	else if (chOperator == '/') {
		if (iValue2 == 0)
			cout << "You cannot divide by 0. Enter another integer ";
		cin.clear();
	cout << "The result is " << division(iValue1, iValue2) << endl;
	iValue1 = division(iValue1, iValue2);
	}
	
} while (iValue1 !=0);	 
return 0;
}



Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>