Hi everyone,
I'm trying to code a calculator that performs the following operations:
+, -, /, *, %, ^, !, L

! factorial = x!
L log10= log10(x)

and im having trouble running the code because of the last three functions. I will post my code and I hope to get some help:

// Nawaf Alqahtani
// Oct 22, 2011

// Calculator Program

#include <iostream> // for cout, endl;
#include <cmath>
using namespace std;

int main()

{
char op;
int x=0;
int y=0;
int answer=0;
bool done = false;
bool error = false;


do
  { 
  int x, y; 	    
  cout << ("Welcome to Calculator")<< endl; 
  
  cout << "Enter an operand: " << endl;
  
  cin>> x;
  
  cout << "Enter an operation: " << endl;
  
  cin >> op;
  
  //Select the operation ('Q' to quit);
  //+, -, *, /, !,^, L, > !;
  cout << "Enter an operand: " << endl;
  
  cin >> y;
  
  cout << "Enter Q to quit" << endl;
  
switch (op)
{
  case '+':
  cout << " The answer is " << x + y << endl;
  break;
  case '-':
  cout << " The answer is " << x - y << endl;
  break;
  case '*':
  cout << " The answer is " << x * y << endl;
  break;
  case '/':
  cout << " The answer is " << x / y << endl;
  break;
  case '%':
  cout << " The answer is " << x % y << endl;
  break;		 
	case '^':
	cout << " The answer is " << ( pow( x, y)) << endl;
	break;
	case '!':
	cout << "The answer is " << x! << endl;
	break;
	case 'L':
	cout << " The answer is " << Log10(x) << endl;
	break;		 		 		 
	case 'Q' :
	case 'q' :
			 done = true;
			 error = true;
			 break;
			 		 
	default: cout << " Invalid operator! " << endl;
	error = true;
}
 
cout << " Thank you for using calculator program ";
cin >> op;
}
while (op == 'y');
return 0;
	   	   	   				    
    
    system ("pause");
    return EXIT_SUCCESS;
    
}

Thank in advance

Edited 5 Years Ago by WaltP: Fixed CODE Tags... You are allowed to EDIT your post if it looks wrong.

I just could not get the exponentiation function to work. The compiler keeps highlighting this line cout << " The answer is " << ( pow( x, y)) << endl;
and says there is an error. I am assuming it will do the same for the rest of the functions. I guess I am doing them wrong and I am a beginner so I dont know what to do to make the program run and function properly.

Compilers rarely say "There is an error". They usually tell you much more about the error so there's something useful to think about.

I have not declared the function because I'm not good at c++ so I just wanna know how can I declare the function so the compiler understands it and runs. I hope I made it a bit clearer now.
Thanks,

The pow function takes two doubles. x and y were declared as integers. When you put two integers in the pow function the compiler doesn't know what to do.

You have two choices. You can either declare x and y as type "float" or "double" instead of "int", or you can do this:

cout << " The answer is " << ( pow( (double)x, (double)y)) << endl;

Thank you so much for your help. I still could not figure out how to make the following functions work:
! Factorial= x!
L log10 = log10(x)

I hope you can help me with these and thanks again for your help guys.

Can someone please help me with log10 function? my code is posted above and it wont run. The compiler said " expected primary expression before double" I use this to make the compiler run: ( double Log10(double x));

Im sorry for being a little messy. I finally figured out how to make the log10 function work. Unfortunately I still couldnt do the same for the factorial function. Maybe cuz I'm doing it the wrong way or maybe cuz im mistaking the meaning of the function. I still need help. Thanks

Im sorry for being a little messy. I finally figured out how to make the log10 function work. Unfortunately I still couldnt do the same for the factorial function. Maybe cuz I'm doing it the wrong way or maybe cuz im mistaking the meaning of the function. I still need help. Thanks

So what is the function meaning? And how do you do a factorial? And what are the limits (based on the computer)?

I've found if you don't know how something works it's impossible to program.

I'm not even certain if there is a standard factorial function in <math.h>. However, if there is, x! would not be valid C++ for reasons I don't want to get into. If you need a factorial function, you can use this one which I'm writing off the top of my head:

unsigned long factorial(unsigned input)
{
    unsigned long result = 1;
    for(int i = 1; i <= input; i++)
        result *= i;

    return result;
}

From that point, you can just find the factorial of x by using factorial(x)

As others have already pointed out (and even solved for you):

I suspect you need to write your own factorial function. The likelihood is small that x! is implemented as written. And sure enough I don't see a factorial function already implemented in cmath.

So you'll need to write your own function:

int factorial(int value)
{
    int fact;
    // compute the factorial of value here
    return fact;
}

and then call it from your switch() statement.

Edited 5 Years Ago by raptr_dflo: forgot to read page 2 before replying, d'oh

This article has been dead for over six months. Start a new discussion instead.