Thread: Incremental program

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    Incremental program

    Hello, I am building the following program. It compiles but I am not getting exactly what I want. I know I must be using the if statements illogically. I have stared and worked on this project for many hours, and I just can't see the logical error and come to the solution.

    I want getInput to call getSeed, and depending on getSeeds success, call getBounds, then getNumToGen. If any input is not correct, then main is to end the program. Thank you in advance for your time and comments.

    Code:
      /*This program will prompt the user for a seed value, a lower bound value, 
       an upper bound value, and a quantity of pseudorandom numbers to generate.
       If an input doesn't follow the criteria specified, an error message appears.
       While generating the pseudorandom numbers it counts the number of even 
       numbers, the number of odd numbers, the number of negatives, the number of 
       positives, and the number of times 0 occurs.  Finally it displays the 
       original user inputs and the counts. 
    ******************************************************************************/
    
    /* preprocessing directives */
    #include <stdio.h>
    #include <stdbool.h>
    #include <time.h>
    #include <stdlib.h>
    
    // Function Declarations
       void displayName();
       bool getInput(int* pSeed, int* pLb, int* pUb, int* pNumToGen);
       bool getSeed(int* pSeed);
       bool getBounds(int* pLb, int* pUb);
       bool getNumToGen(int* pNumToGen);
       void genRandom(int seed, int lb, int ub, int numToGen,
                   int* pEven, int* pOdd, int* pNeg, int* pPos, int* );
       void addToCounts(int randNum, int* pEven, int* pOdd,
                     int* pNeg, int* pPos, int* pZero);
       void printResults(int seed, int lb, int ub, int numToGen,
                      int even, int odd, int neg, int pos, int zero);
    
    // function main () calls displayName() then getInput().  Depending on the 
    // values inputted by the user, main() will decide whether to end the program
    // or continue with the other functions.
    int main (void)
    {
       bool testInput;
       int aSeed, aLb, aUb, aNumToGen;
       int aEven, aOdd, aNeg, aPos, aZero;
       
       displayName ();
       getInput(&aSeed, &aLb, &aUb, &aNumToGen);
    
       if ((getInput(&aSeed, &aLb, &aUb, &aNumToGen))== true)
          printf("Thank you");        
       else
          printf("\nTry again later\n");
          
    return 0;
    }
    
    // function displayName() displays the programmer's name
    void displayName()
    {
       printf("\nThomas Ciocco\n");
       
       return; 
    }
    
    bool getInput(int* pSeed, int* pLb, int* pUb, int* pNumToGen)
    {
       getSeed(pSeed);
       
       if ((getSeed(pSeed)) == true)
          getBounds(pLb, pUb); 
       else 
          return false;
             
       if ((getBounds(pLb, pUb)) == true)
             getNumToGen(pNumToGen);
       else 
          return false;
                
       if ((getNumToGen(pNumToGen)) == true) 
          return true; 
       else 
          return false;  
    } 
    
    // function getSeed() prompts the user for a nonnegative integer
    // and tests whether the input is valid.  If it is valid, the bool is set 
    // to true, if not, bool is set to false.  Finally it returns the bool value 
    // and sends the input as an output parameter.
    bool getSeed(int* pSeed)
    {
          printf("Please enter a non-negative integer value: \n");
          int seedResult = scanf("%d", pSeed);
          
          if(seedResult != 1){
             printf("You have entered invalid data");
             return false;
             }
              
          if (seedResult == 1){
             if(*pSeed >= 0)
                return true; 
             else{
                printf("You have entered an invalid integer");
                return false;
                }
             }
    
    }           
    
    
    // function getBound () prompts the user for a lower bound and  an upper bound
    // integer.  Next, it will test whether the inputs are valid and set the bool
    // to true if it is, false if not.  The integers are sent as output parameter,
    // and the bool value is returned. 
    bool getBounds(int* pLb, int* pUb)
    {
       
       printf("Please enter a lower bound [-25,-1] and an upper bound [1,25]: ");
       int boundsResult = scanf("%d %d", pLb, pUb);
       
       if (boundsResult != 2)
          printf("You have entered invalid data");
          
       if (boundsResult == 2){
          if (!(*pLb >= -25 && *pLb <= -1)){
             printf("%d is not in range [-25,-1]", *pLb);
             return false;
          }
          if (!(*pUb >= 1 && *pUb <= 25)){
             printf("%d is not in range [1,25]", *pUb);
             return false;
             }
    }
          else 
            return true;
          
          
       return false;
       
    } 
    
    // function getNumToGen() prompts the user for a non-negative integer to set
    // as the quanitity of pseudorandom numbers to generate.  Next, the function
    // tests whether the input is valid or not.  If valid, bool value is returned 
    // true; if invalid, bool set to false.  Input sent as output parameter.
    bool getNumToGen(int* pNumToGen)
    {
       printf("Please enter the number of randoms to generate [10,50]:");
       int numToGenResult = scanf("%d", pNumToGen);
                   
          if (numToGenResult != 1){
             printf("You have entered invalid data");
             return false;
             }
             
          if (numToGenResult == 1){
             if (!(*pNumToGen >= 10 && *pNumToGen <= 50)){
                printf("%d is not in range [10,50]", *pNumToGen);
                return false;
                }
    }
          else 
             return true;
    
    
    }
    Last edited by Jaxtomtom89; 10-17-2010 at 11:48 AM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    So what goes wrong?

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    The program will ask for inputs regardless what I input. The error messages do come up if input is incorrect though. So what I think is happening is that the subfunctions are being called multiple times. The way I used the if statements may be creating a loop but I can't see exactly how.

    The program needs to validate the input then either decide to call the next function or terminate, rather than just recalling the same/past function.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Well you are calling functions multiple times:
    Code:
    getInput(&aSeed, &aLb, &aUb, &aNumToGen);
    if ((getInput(&aSeed, &aLb, &aUb, &aNumToGen))== true)
    ...
    getSeed(pSeed);
    if ((getSeed(pSeed)) == true)
    And so on. You seem to be calling each function twice. The first call you don't check the return value, the second you do. This happens for all calls inside of getInput().

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    O haha! You are a savior! Our teacher never taught us about using 'if statements' and 'functions calls' together. I didn't realize putting the function in the 'if' actually sent a call too. This explains why there were always four repetitions of each subfunction. I called getInput twice in main then called each subfunction twice. So it was compounded. I feel so much better for your knowledge and brain.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    Quote Originally Posted by Jaxtomtom89 View Post
    O haha! You are a savior! Our teacher never taught us about using 'if statements' and 'functions calls' together. I didn't realize putting the function in the 'if' actually sent a call too. This explains why there were always four repetitions of each subfunction. I called getInput twice in main then called each subfunction twice. So it was compounded. I feel so much better for your knowledge and brain.
    Ya, any time you put "function_name()", it gets called. Also, it would HAVE to be called, because when you have an if statement, it has to get evaluated, and in order to do that, it has to check the return value for your function, which means it gets called.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thank you for all the help. I was able to finish this part of the program with the help.

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM