# getValue function, very confused

• 02-23-2010
joeman
getValue function, very confused
i'm trying to run a program that calculates the surface area of various shapes. right now i only have it set it up through a sphere, but i can't go further until i figure this out. part of the assignment is that i must use the function "int getValue (string prompt, lowerBound, upperBound". so when this function is used, it will enter some words, in this case, something like "enter the radius of the sphere, and then there are limits as to how small and how large the radius can be. 2-15, let's say. anything outside that range is invalid and the user needs to be asked to try again. i've tried implementing this function a few different ways but it keeps being skipped completely. here is my code (i'm focusing on this function, not worried about the neatness of the rest of the code right now)
Code:

``` #include <iostream> #include <iomanip> #include <string> #include <math.h> using namespace std; const double PI = 3.14159; int value,choice, radius; float surface_area_sphere; int menu(int choice) { cout<<"\nSurface Area Calculator" <<"\n1) Sphere" <<"\n2) Cone" <<"\n3) Rectangular Prism" <<"\n4) Pyramid" <<"\n\n5) Quit" <<"\n\nEnter Your Choice: "; cin>>choice; while (choice<1 || choice>5) { cout<<"\nInvalid choice, please try again"; cin>>choice; } return(choice); }; float calcSphere (int radius) { cin>>radius; surface_area_sphere=(4*PI*radius*radius); cout<<"The surface area of the sphere is"<<surface_area_sphere; return (surface_area_sphere); }; int getValue (string prompt, int lowerBound, int upperBound) { return (lowerBound-upperBound); }; int main() {   menu(choice); { if (choice=1) getValue ("Enter the radius of the sphere", 2,15);  calcSphere (radius) ; }  }```
so i want the getValue function, when the user enters 1 for choice, to prompt them to enter the radius, and then have to enter one between 2 and 15. but when i run this, it runs the menu function fine, then skips the get value, and goes to calcSphere, where i can enter a number (without any prompting though) and it calculates it fine. i don't understand the purpose of the getValue function and it seems like it's unnecessary to me, but i have to use it. any advice on how to get it to work?

return 0;
}
• 02-23-2010
cyberfish
First of all, fix the indentation.

Code:

`return (lowerBound-upperBound);`
What's lowerBound? What's upperBound? What do you think it will return?
• 02-23-2010
joeman
lowerbound and upperbound are the limits of what can be inputted for the radius. lowerbound is 2, upperbound is 15. but i need to reuse this functions for different shapes with different limits, so i can't set them to stay at 2 and 15.
• 02-23-2010
cyberfish
So if they are 2 and 15, what does
Code:

`return (lowerBound-upperBound);`
do?
• 02-23-2010
jeffcobb
You are calling your menu function but not doing anything with the selection...menu() is designed to return an int but you are just calling menu() w/o doing anything with the result. In main() you need to do something like:
Code:

`choice = menu();`
And there is no point in passing the choice integer *to* menu....