Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

seventeen

macrumors member
Original poster
Apr 9, 2009
41
0
Denton, Tx
I am creating a simple CLI calculator tool as an exercise. The complete code is included below. I need to make sure n1 and n2 are numeric in order for the functions to work; somewhat consequently, I would like to make the program quit upon coming across a non-numeric value from the user.

Can anyone give me some direction?

Check out the first if statement in the do block. (its commented out)

Thank you!

Code:
#include <iostream>
#include <new>

using namespace std;

double factorial(double n) { return (n <= 1) ? 1 : n * factorial(n - 1); }
double add(double n1, double n2) { return(n1 + n2); }
double subtract(double n1, double n2) { return(n1 - n2); }
double multiply(double n1, double n2) { return(n1 * n2); }
double divide(double n1, double n2) { return(n1 / n2); }
int modulo(int n1, int n2) { return(n1 % n2); }
double power(double n1, double n2) {
	double n = n1;
	for(int i = 1 ; i < n2 ; i++) {
		n *= n1;
	}
	return(n);
}

void print_problem(double n1, double n2, char operatr) {
	cout<<n1<<operatr<<flush;
	if(operatr != '!') {
		cout<<n2<<flush;
	}
	cout<<"="<<flush;
}

int main(void) {
	
	double* n1, * n2, * result = NULL;
	char* operatr = NULL;
	
	n1 = new (nothrow) double;
	n2 = new (nothrow) double;
	result = new (nothrow) double;
	operatr = new (nothrow) char;
		
	if(n1 == NULL || n2 == NULL || operatr == NULL || result == NULL) {
		cerr<<"\nMemory allocation failure.\n"<<endl;
	} else {
		
		do {
			
			cout<<"calculator>> "<<flush;
			cin>>*n1;
			
			// if(*n1 is not numeric) {
			// 		 break;
			// }

			cin>>*operatr;
			
			if(*operatr == '!') {
				print_problem(*n1, *n2, *operatr);
				cout<<factorial(*n1)<<endl;
			} else {
				
				cin>>*n2;
				
				switch(*operatr) {
					case '+':
						print_problem(*n1, *n2, *operatr);
						cout<<add(*n1, *n2)<<endl;
						break;
					case '-':
						print_problem(*n1, *n2, *operatr);
						cout<<subtract(*n1, *n2)<<endl;
						break;
					case '*':
						print_problem(*n1, *n2, *operatr);
						cout<<multiply(*n1, *n2)<<endl;
						break;
					case '/':
						if(*n2 > 0) {
							print_problem(*n1, *n2, *operatr);
							cout<<divide(*n1, *n2)<<endl;
						} else {
							print_problem(*n1, *n2, *operatr);
							cout<<" cannot be computed."<<endl;
						}
						break;
					case '%':
						if(*n1 >= 0 && *n2 >= 1) {
							print_problem(*n1, *n2, *operatr);
							cout<<modulo(*n1, *n2)<<endl;
						} else {
							print_problem(*n1, *n2, *operatr);
							cout<<" cannot be computed."<<endl;
						}
						break;
					case '^':
						print_problem(*n1, *n2, *operatr);
						cout<<power(*n1, *n2)<<endl;
						break;
					default:
						cout<<"Invalid Operator"<<endl;
				}
			}
		} while(true);
		
		delete n1, n2, operatr, result;
	}
	return(0);
}
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
They will always be numeric, since they're doubles. You probably want to check this before assigning the user input to the variables, by reading the user input as a string and then converting it.

Also, please get rid of the news. There is absolutely no need to do this via pointers. Did you perhaps grow up on Java or C#..?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.