Boolean function returns

This is a discussion on Boolean function returns within the C Programming forums, part of the General Programming Boards category; Hello. I'm in an introductory computer science class and so far it's been okay. With our most recent project, we've ...

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

    Boolean function returns

    Hello. I'm in an introductory computer science class and so far it's been okay. With our most recent project, we've been told to come up with a program that prompts the user for four integers that we will use for a PRNG. We have to use boolean function, and if-else statements to test that the input is correct. If at any point it is not, main () must end the program. I've been using the book a lot for help but it doesn't explain how I can use the Boolean's return value.
    The teacher doesn't want us using code we haven't gone over in class yet, so I can't use anything more advanced than if-else and Boolean returns. The code I need help with is the following. When I compile it using Bloodshed Dev C++, it says I can't compare pointers and integers. If I can understand just this part, I will be able to finish rest. Thanks in advance.

    Code:
    bool getInput(int* pSeed, int* pLb, int* pUb, int* pNumToGen)
    {
       getSeed(pSeed);
       
       if (getSeed == true){
          getBounds(pLb, pUb);
          return true;
          }
      
       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;
             }
              
          while (seedResult == 1){
             if(&pSeed >= 0){
             return true;
                }
             else{
                printf("You have entered an invalid integer");
                return false;
                }
             }
    
    }
    Last edited by Jaxtomtom89; 10-17-2010 at 08:51 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       if (getSeed == true){        // wrong.you meant getSeed(pseed) == true?
    
             if(&pSeed >= 0){    // should be *pSeed
    When you use function name like getSeed, it's pointer to function.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Quote Originally Posted by Bayint Naung View Post
    Code:
       if (getSeed == true){        // wrong.you meant getSeed(pseed) == true?
    
             if(&pSeed >= 0){    // should be *pSeed
    When you use function name like getSeed, it's pointer to function.
    It's funny because I've tried this already and it does compile, but the program will crash when I enter input. "*pSeed" causes it to crash which makes no sense to me.

    Thank you for the quick reply. I'm going to continue playing around with it and see if perhaps something else I did is causing the crash.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Can you post the code how you call getInput() function altogether with variable declaration that are passed to function.?
    Btw, you should stop changing without any reason to make it work

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thanks again. This is the code. Trying to do the one subfunction correctly before I do the rest.

    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* pZero);
       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);
    
    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);
          return true;
          }
       getNumToGen(pNumToGen);
       
       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;
             }
              
          while (seedResult == 1){
             if(pSeed >= 0){
             return true;
                }
             else{
                printf("You have entered an invalid integer");
                return false;
                }
             }
    
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
          int seedResult = scanf("%d", &pSeed);    // since pSeed itself is pointer to int. just pass pSeed, not &pSeed.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Thanks, I love you (no homo)

    I would never have noticed this and I've been working on this project for many hours. I will play around with this.

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, 03:06 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  5. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM

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