Member Avatar for Raim

So I'm making a simple calculator program on the Windows Form Application of C++. Nevertheless, I'm stuck on the exponents, factorial, and square root part. In my System32 Console calculator, these were the codes I use for the exponent, factorial, and respectively, while I just used the sqrt function of math.h library for the square root:

cout<<"Ingrese su base : ";
	cin>>VAL1;
	cout<<"\n Ingrese su potencia : ";
	cin>>VAL2;
	ACUMULADO=VAL1;
	if (VAL2==1)
	{	
		RES=VAL1;
		cout<<"\n Su respuesta es : "<<RES <<endl;
	}
	else
		if (VAL2==0)
		{		
			RES=1;
		cout<<"\n Su respuesta es : "<<RES <<endl;
	}
		else
			if (VAL2>1)
			{	
				for (int CONT=2;CONT<=VAL2;CONT+=1)
				{
					ACUMULADO*=VAL1;
					
				}
			}
double VA;
	double FACT=1;
	cout<<"Ingrese factorial : ";
	cin>>VA;
	for (int CONT=1;CONT<=VA;CONT++)
	{
		FACT*=CONT;
	}

I am lost on how to implement this codes for the functions inside the Windows Form, and I have no idea how to correctly implement the #include math.h library inside the Windows Form, hence unable to use the square root function. Help?

Member Avatar for Raim

14 hours, just wanted to know if anyone might help.

I have never made a Windows Form Application before but I just made one out of the template and threw #include <cmath> (you should use cmath not math.h in C++) at the top and it compiled without a problem, since as I suspected just C++ with a big library attached to it.

So what you can do is just go to the source file with all the functions you are trying to use the cmath functions in and include cmath at the top of it.

Member Avatar for Raim

Thanks, that solves the exponent and sqrt problem, but I still need to implement the factorial, and for it I need to implement the code above shown. Do I still implement it in the template?

Member Avatar for Raim

Okay, I've placed the "cmath" library after the #pragma at the very beginning and it only gives me more errors, so I'm unable to use the function

pow (x,y)

right now. Where exactly must I place the library so I can make use of it within the Form?
Note: I double click the button on my Form, hence programming from the new window that appears after that, just an FYI.

Honestly I'd have to see some source code before I can tell you how to fix it. The code that I have from the default VC++ template is really bare bone.

So if you could zip up your project files (do not need all the .obj files and other stuff that is made when compiling) and I'll take a look at it.

When you put #included <cmath> (notice they are < brackets not " quotes) you got a bunch of errors. If you read the errors they just say that it doesn't know what datatype you are trying to pass into the ToString() function. The work around to for this is to cast the result into a double or something (I tried long double and it didnt work).

So just change the last line of your calcu_Click() function to

this->rp->Text=Convert::ToString((double)pow(ni,nii));

Edit: make sure you include cmath at the top of Form1.h but under the #pragma once

Member Avatar for Raim

Awesome! It fixed that and now it's working. All I have left is to add the factorial in it, since there isn't one on the cmath library, but that paved the way a lot. I think I'll mark this as solved, thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.