Creating a function - newbie

This is a discussion on Creating a function - newbie within the C Programming forums, part of the General Programming Boards category; I need to create a function that prompts a user to enter an integer between a minimum and maximum value, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    10

    Creating a function - newbie

    I need to create a function that prompts a user to enter an integer between a minimum and maximum value, lets say 0 to 100. Then the function needs to verify the value is within range and then store it. We have to make the function under a seperate c file and then call it, with the whole idea being we can use this function on other programs in the future.

    the definition would be something along the lines of:

    int GetInt (int min, int max)

    It seems simple enough, I would just store the value to a variable and do an if statement, if the variable is less than max and greater than min, store it. The part that throws me off is the function has to be universal, I need to call it 3 times in this program from a seperate c file. How do I go about that? It seems so simple if the function was only going to be used once, I could just store the input to a variable and be set, but by storing it to that set variable I can't call the function 2 more times. Any tips, help or suggestions would be greatly appreciated
    Last edited by Sholcomb1; 09-30-2006 at 04:34 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but by storing it to that set variable I can't call the function 2 more times
    Why not? Just fill a local variable and return it:
    Code:
    int GetInt (int min, int max)
    {
      int input;
    
      // Fill input
      // Error check input
    
      return input;
    }
    My best code is written with the delete key.

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,236
    If you need more help please post your function code that you've already written.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Replies: 12
    Last Post: 10-16-2008, 03:49 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21