Thread: getValue function, very confused

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    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;
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    First of all, fix the indentation.

    Code:
    return (lowerBound-upperBound);
    What's lowerBound? What's upperBound? What do you think it will return?

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    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.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    So if they are 2 and 15, what does
    Code:
    return (lowerBound-upperBound);
    do?

  5. #5
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM