my problem is that i have to let the user enter a number then choose the function i.e. (Cube,Cube Root etc.) from a menu and then i need to call on my function in (int main() )
Thanks Any Ways
switch(usersChoice)
{
case 1:
// Square Root function call
break;
case 2:
// Square function call
break;
case 3:
// Cube Root function call
break;
case 4:
// cube function call
break;
default:
break;
}
You don't need the function tpes and parameter types when you call a function.
Do:
Cube(input);
Instead of:
float Cube(float input);
Also, you aren't displaying the return value of the functions that you are calling. That is why you aren't seeing anything.
Just put the function call in cout.
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
usingnamespace std;
float input;
float Answer;
float SqRt (float input)
{
while(input<0)
{
cout<<"This is an invalid entry you cannot have a negative square root.\n";
cin>>input;
}
Answer=SqRt(input);
return(Answer);
}
float Square (float input)
{
pow(input,2);
Answer=pow(input,2);
return(Answer);
}
float CbRt (float input)
{
if(input<0)
{
pow(input,1./3.);
Answer=pow(input,1./3.);
return(Answer);
}
else
{
pow(input,1./3.);
Answer=pow(input,1./3.);
return(Answer);
}
}
float Cube (float input)
{
pow(input,3);
Answer=pow(input,3);
return(Answer);
}
int main()
{
int choice;
cout<<"Enter a number and I will find the answer for the esired function.\n";
cin>>input;
cout<<"Please select a function.\n";
cout<<"1.Square Root.\n";
cout<<"2.Square.\n";
cout<<"3.Cube Root.\n";
cout<<"4.Cube.\n";
cin>>choice;
while(choice>4||choice<1)
{
cout<<"Please enter a valid choice.\n";
cin>>choice;
}
switch(choice)
{
case 1:
cout<<SqRt(input);
break;
case 2:
cout<<Square(input);
break;
case 3:
cout<<CbRt(input);
break;
case 4:
cout<<Cube(input);
break;
}
system("pause");
}
Okay it works but then when i try to fnd the square root windows tells me i have a problem by the way do you know a good way to find the cube root of a negative number because mine doesnt work
First of all, you have conflicting variable names. For example, you have a global float named input, and function parameters named input. Also, with your SqRt function, you have infinite recursion going on. This will cause a stack overflow and Windows will close it. On line 15, it calls itself. I think you meant to call the sqrt (all lower) function from math.h.
OH Thanks it works now but do you know a way for a negative cube root like on line 25-38 because it doesnt work.
you know like the cube root of -8 and you get -2 ?
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
usingnamespace std;
float input;
float Answer;
float SqRt (float input)
{
while (input<0)
{
cout<<"This is an invalid entry you cannot have a negative square root.\n";
cin>>input;
}
Answer=SqRt(input);
return(Answer);
}
float Square (float input)
{
pow(input,2);
Answer=pow(input,2);
return(Answer);
}
float CbRt (float input)
{
if(input<0)
{
pow(input,1./3.);
Answer=-pow(-input,1./3.);
return(Answer);
}
else
{
pow(input,1./3.);
Answer=pow(input,1./3.);
return(Answer);
}
}
float Cube (float input)
{
pow(input,3);
Answer=pow(input,3);
return(Answer);
}
int main()
{
int choice;
cout<<"Enter a number and I will find the answer for the desired function.\n";
cin>>input;
cout<<"Please select a function.\n";
cout<<"1.Square Root.\n";
cout<<"2.Square.\n";
cout<<"3.Cube Root.\n";
cout<<"4.Cube.\n";
cin>>choice;
while(choice>4||choice<1)
{
cout<<"Please enter a valid choice.\n";
cin>>choice;
}
switch(choice)
{
case 1:
cout<<"The square root of "<<input<<" is "<<sqrt(input)<<" .\n";
break;
case 2:
cout<<"The square of "<<input<< " is "<<Square(input)<<" .\n";
break;
case 3:
cout<<"The cube root of " <<input<< " is "<<CbRt(input)<<" .\n";
break;
case 4:
cout<<"The cube of " <<input<< " is "<<Cube(input)<<".\n";
break;
}
system("pause");
}
This is my code now but now it doesn't send the error message when a negative input for square root you should try t because this is what i get[-1#IND]