help

Please elp don'tknbow how tocall on functions i am stuck,i want to call my functions hhow can do this please write the code thanks. Here is the code:

#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace 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*-1);
}
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;
}
Firstly, http://www.cplusplus.com/forum/articles/42672/

What is your problem? You seem quite able to call those pow()s, so why do your own functions cause problems?
1
2
3
float value;
cin >> value;
if( choice == 1 ) cout << SqRt( value );

Though your SqRt doesn't actually calculate anything..
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
Sounds like a job for a switchyard.
http://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
}
This is going to sound like a stupid question but what would be the function call
Thank you
The same way you call any other function. func_name(params);
[code]
#include <stdlib.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace 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*-1);
}
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:
float SqRt (float input);
break;
case 2:
float Square (float input);
break;
case 3:
float CbRt (float input);
break;
case 4:
float Cube (float input);
break;

}
system("pause");
}
[/code}
I still dont get an answer it just tell me to input a number and then choose my function but no answer ??
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.
 
cout << Cube(input);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <iostream> 
#include <string>
#include <math.h>
using namespace 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 ?
I'm not sure how it could be done off of the top of my head. You could probably find a working algorithm on Google.
okay thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <iostream> 
#include <string>
#include <math.h>
using namespace 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]
You are calling math.h's square root function (sqrt, all lower) and not your own (SqRt).

Replace cout<<"The square root of "<<input<<" is "<<sqrt(input)<<" .\n"; with cout << SqRt(input);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdlib.h>
#include <iostream> 
#include <string>
#include <math.h>
using namespace 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<<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");
}

Okay but now when i enter a positive number windows shuts down the program!?
On line 15 your calling SqRt, not sqrt. This will cause infinite recursion and a stack overflow. Just call sqrt (all lower) instead.
Topic archived. No new replies allowed.