Thread: Simple function programme

  1. #1
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215

    Simple function programme

    Im writing a function that asks someone to put in a range from one to five and the function is to be passed through a string. I;d likke the function to return the input value if in range, or minus one if out of range. to do this, i'd like it to do something like this...

    Code:
     value=getval("Please enter a value in the range 1 to 5")
    Code:
     
    #include <iostream>
    using namespace std;
    
    void getval ()
    {
      char string[1];
      
      cout << "please enter a value between 1-5: ";
      cin >> string;
      cout << endl <<string <<endl;
    }
    
    int main ()
    {
      getval ();
      
      system("pause");
    }

    Can anyone help?

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    void getval ()
    Not really sure what it is that you are trying to achieve, but you need to change the line above to both take and return an argument if you intend to use it like

    Code:
    value=getval("Please enter a value in the range 1 to 5")

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Code:
    int getval ()
    {
      char string[1];
      
      cout << "please enter a value between 1-5: ";
      cin >> string;
      cout << endl <<string <<endl;
    
      if(string<1 || string >5)
      return -1;
      else
      return string;
    
    }

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char string[1];
    That is a one character character array. When you read into it from cin, cin assumes it is a C style string. C style strings have a null character that tells you where they end, so you have to leave room for that. That means that your string doesn't have any room for any real characters.

    If you want to read in a single character, use a char variable, not a char array. If you want to read in a string, use the C++ string class. If you want to read in a number between 1 and 5, you might want just use an int, which is meant for integer numbers.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You might not want to call a variable string if you intend to use the C++ string class.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    This is my program so far that i have started on

    Code:
    #include <iostream>
    
    using namespace std;
    
    
    int GetVal()
    {
    int Value;
    
    Value=GetVal("Please enter a value in the range 1 to 5");
    
    
     if (Value = 1>5)
    {
    
    
    }
    
    else
    {
     cout<< "Enter a number within the range 1-5";
     cin >> Value;
    }
    
    
    }
    void main ()
    {
    
    GetVal();
    
    
    system("pause");
    }
    the only problem at the moment is that i cant get
    Code:
     Value=GetVal("Please enter a value in the range 1 to 5");
    to work also i dont think it is being passed through a string.

    anyone know what the problem with it is?
    Last edited by peckitt99; 10-24-2006 at 05:21 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are looking for something along the lines of:
    Code:
    int getVal()
    {
        int number = 0;
        while (/* number is not between 1 and 5 */)
        {
            // request for input using std::cout
            // read input to number using std::cin
        }
        return number;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    yeah sort of but we have to use the
    Code:
     Value=Getval("Please enter number");
    and input is a string.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that case, modify it to:
    Code:
    int getVal(const std::string& input_request_message)
    {
        int number;
        do
        {
            // request for input using std::cout and input_request_message
            // read input to number using std::cin
        } while (/* number is not between 1 and 5 */);
        return number;
    }
    anyone know what the problem with it is?
    On one hand, it looks like you are trying to write a recursive function. On the other hand, function overloading means that you are trying to call a function that does not exist (i.e. calling GetVal(const char*) from GetVal() ).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    hey thanx i got it working in the end. this is the code i used

    Code:
    #include <iostream>
    using namespace std;
    
    int GetVal(char prompt[256]);
    
    void main()
    {
    int Value;
    Value=GetVal("Please Enter a number in the range of 1-5");
    
    system("pause");
    }
    int GetVal(char information[256])
    {
       int value;
    
       cout<<information<<"\n";
       cin>>value;
    
       if (value < 1 || value > 5)
       {
          return -1;
       }
       else
       {
          return value;
       }
    }

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Brazil
    Posts
    13
    One good suggestion is to replace char prompt[256] with string prompt and char information[256] to string prompt so both use the C++ string classes which are much more efficient than a 256 chars character array and both the declaration and definition have the same arguments name.
    Or you could remove the argument name in the declaration as it's not really checked by the compiler, only string would be enough.
    I also removed the '{'s as they're not really necessary if the body of the loop doesn't have more than one line and before I forget, void main is WRONG, compilers may accept it but it is a very bad practise.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int GetVal(string);
    
    int main()
    {
    int Value=GetVal("Please Enter a number in the range of 1-5");
    
    system("pause");
    return 0;
    }
    
    int GetVal(string information)
    {
       int value;
    
       cout << information << "\n";
       cin>>value;
    
       if (value < 1 || value > 5)
          return -1;
    
       else   
          return value;
    }
    Last edited by Etinin; 10-24-2006 at 12:45 PM.

  12. #12
    Is Trying to Learn
    Join Date
    Mar 2006
    Location
    Hutton, Preston
    Posts
    215
    now i have managed to get they layout of the function i have another small problem.

    i need to change the function that i have got and put in Lower, Upper and Outstr in as perameters, once that bit is done i then need to output the outstr (which would be the text you send to the function) and lower and upper are the range
    e.g. lower = 1, upper = 5 so the number has to be within 1 and 5.

    At the moment i have (i think) got the Outsrt working but am unsure about the rest.

    this is what i have got soo far

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int GetVal(string);
    
    int main()
    {
    int Value=GetVal("Please Enter a number in the range of 1-5");
    
    system("pause");
    return 0;
    }
    
    int GetVal(string Outstr)
    {
       int value;
    
       cout << Outstr <<endl;
       cin>>value;
    
       if (value < 1 || value > 3)
       {
       cout << "Lower";
       return -1;
       }
       
       else if (value <3 || value >5)
       {
       cout << "upper";
       return -1;
       }
       
       
       else if (value >5)
       {
       return 0;
       }
    
       else   
          return value;
    }
    if anyone can help it would be great.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Step one is to add Lower and Upper as arguments to your function and make sure to pass a value for lower and upper when you call the function from inside main. You don't have to use the alues yet, just get the parameter passing compiling first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  3. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM