Howdy, I have an assignment which requires that I get user input for 3 different variables, each in it's own function. These three functions are called by another function, which is called by main to begin with. Each of these inputs have a range that the input must be in. I've got the input range fine, and the functions return their boolean output correctly. However, we also need to kick the user out the program if he/she enters an out of range input for any of the entries.
If we aren't allowed to use exit or abort, I'm assuming this is done by ending the program through main, though I'm not exactly sure how to do it. Obviously I should use an if or something, but as for how (and where for that matter) to implement it, I'm ridiculously lost. My C programming skills are lacking :(

So I guess my questions are:
Should the if/else go in main, the function that calls the input functions, or the input functions themselves?
Does the condition of the if/else use the boolean outputs of the input functions? Or does it use brand new variables or the input variables the user entered?

Any help would be GREATLY appreciated.

Could we see what you have so far?

Like I said, I don't have the greatest understanding of the subject, so there are probably some other mistakes mixed in there as well. Feel free to cite me on anything, I'd love to know what I can improve on.

void displayName ();
bool getInput (int* seed, int* lb, int* ub, int* numToGen);
bool getSeed (int* seed);
bool getBounds (int* lb, int* ub);
bool getNumToGen (int* numToGen);
void genRandom (int seed, int lb, int ub, int numToGen, int* even, int*
                   odd, int* neg, int* pos, int* zero);
void addToCounts (int randNum, int ub, int lb, int seed, int* even, int*
                   odd, int* neg, int* pos, int* zero, int numToGen);
void printResults (int seed, int lb, int ub, int numToGen, int even, int
                     odd, int neg, int pos, int zero);

int main (void)
 {
    //variables
    int seed;
    int lb;
    int ub;
    int numToGen;
    int even;
    int odd;
    int neg;
    int pos;
    int zero;

    //fn calls
    displayName ();
    getInput (&seed, &lb, &ub, &numToGen);
    genRandom (seed, lb, ub, numToGen, &even, &odd, &neg, &pos, &zero);
    printResults (seed, lb, ub, numToGen, even, odd, neg, pos, zero);

    return 0;
 }
   


void displayName (void)
 {
  printf("\nName: ");
  printf("\n\n");
 }




bool getInput (int* seed, int* lb, int* ub, int* numToGen)
 {
  getSeed (seed);
  getBounds (lb, ub);
  getNumToGen (numToGen);
 }

                   

                   
bool getSeed (int* seed)
 {
  bool pos = true;

  
  //input and read
  printf("\nPlease enter a positive seed value: ");
  scanf("%d", seed);
    
  if (*seed <= 0)
  {
     pos = false;
     printf("\nYour entry is invalid.");
     return 0;
  }

  return pos; 
 }
    
    
    
bool getBounds (int* lb, int* ub)
 {
  bool range = true;

  printf("\nPlease enter an integer in range [-25,-1] for lower bound: ");
  scanf("%d", lb);
  printf("\nPlease enter an integer in range [1,25]for upper bound: ");
  scanf("%d", ub);
  
  if (*lb < -25 || *lb > -1)
  {
     range = false;
     printf("\nYour data entry is invalid.");
  }
  else
     if (*ub < 1 || *ub > 25)
     {
        range = false;
        printf("\nYour data entry is invalid.");
     }

  return range;    
 }
                   

  
  
bool getNumToGen (int* numToGen)
 {
  bool numRange = true;
  
  printf("\nPlease enter a number in the range [10,50] for number of");
  printf("\nrandoms to generate: ");
  scanf("%d", numToGen);
   
  if (*numToGen < 10 || *numToGen > 50)
  {
     numRange = false;
     printf("\nYour data entry is invalid.");
  }
  else
  
  return numRange;
 }

You can be sure that you are expected to do the if/else inside the function that gets the data. That is fairly standard practice, as it allows you to reuse the function to do validation in other modules, and a change in the allowable values will be implemented everywhere with a single edit.

Also, it is a realistic requirement to avoid exiting from the validation routine. The function can then be reused in a variety of ways, perhaps to ignore bad data, or when that piece of data is optional.

The problem is how to tell main() that your function had trouble. If you know that the answers must be positive integers, you could return a -1, or if it is a string, maybe it can't be empty. However, in your next assignment, you are sure to be told that those a now allowed :)

A couple of ways to handle this (sloppy and untested):

Instead of "myVar = myFunc()" you could pass the address of myVar and return a code for good or bad, like "return_code = myFunc(&myVar)". The variable in main() is actually assigned a value from within the function, using a pointer. Then main() checks "return_code", exiting for a bad value. (Bad is usually a zero, which is counter-intuitive for people who write shell scripts.)

You can use structures to return a value and a return code.

For string results, you can embed some indicator that you eventuallly have to remove. Don't care for this technique.

I have seen functions that are called in a certain order, and the second tells you if the first one failed. Never liked that.

Lots of possibilities.

Okay, I understand what you said there, and I applied it to my seed function:

if (*seed <= 0)
{
    printf("\nYour entry is invalid.");
    pos  = getSeed(seed);
 }

return pos;
}

Did I do that right? If so, it gives me my invalid entry message and then repeats the entry until I enter a positive value. Is there a way to do it so that it just quits out of the program entirely?

You are correctly putting a value in seed, but your function refuses to end until you get a good value. The caller, getInput(), never checks the result (which will always be true, as presently written.)

If you are supposed to end the program when you get bad input, why do you keep looping until you recieve proper input? Instead, simply return "false" when you get bad input.

Then, from inside getInput(), do "if ( !getSeed (seed) ) {return false}"
then in main do "if ( !getInput(blah,blah) ) {exit 1}" or something similar. Maybe print a message first (from main).

I should also point out that having the function call itself repeatedly until you get proper user input is dangerous. If you really had wanted to force good input, then a while loop would be preferred. Recursive functions have their uses, this isn't one of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.