not calling functions

This is a discussion on not calling functions within the C Programming forums, part of the General Programming Boards category; Hi there. Let me start by saying I have only been doing C for about 10 weeks, and I'm struggling ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    26

    not calling functions

    Hi there. Let me start by saying I have only been doing C for about 10 weeks, and I'm struggling to get the hang of it, so please be patient with me. Also, although this is part of a homework assignment, my professor is very aware of my coming to the boards for assistance and is supportive of this.
    This project is similar to a lotto game. It prompts the user for the number of games that the user would like to run this program for. Each game prints out six random numbers in ascending order for the user (quick pick), and then generates another 6 random numbers as the winning numbers.
    The problem is that the program does not want to run the two function calls. I can do the program once if I'm using an "if" statement for "do you want to play? (y/n)" with no problems, so I know the number generator and sorter works. I just can't get it to run the whole thing a given number of times. Any assistance would be appreciative. **And please, an explaination of the offered suggestions would really help me to get the hang of this.** I hate being so dependant and ignorant. Thanks in advance.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    #include<ctype.h>
    #include<time.h>
    
    void userGenerator();
    void computerGenerator();
    void printarray(int * arrpt);
    
    int main(void)
    {
       char start;
       int numPlay=0;
       int counter=numPlay;
    
       printf("                 Welcome to the lotto game!!\n\n");
       printf("This game asks for the number of times you would like\n");
       printf("to print 6 random numbers (quick pick lotto) in ascending order\n");
       printf("for you and then 6 more random numbers (winning numbers)for you to compare.\n\n");
    
       printf("How many games would you like to play? ");
       scanf("%d", &numPlay);
    
       for( counter; counter > 0;  counter--)
       {
             void userGenerator();
             void computerGenerator();
       }
    
       system("PAUSE");
       return 0;
    }   //end main
    
    void userGenerator()
    {
       int array[6]={0};
       int arrlen;
    
       srand(time(NULL));
    
       arrlen = sizeof(array)/sizeof(int);
    
       for(int x=0; x < arrlen; x++ )
       {
           array[x] = (int) rand()%53+1;
           for(int y=0; y<x; y++)
           {
              if (array[y] == array[x])
              {
                 array[x] = (int) rand()%53+1;
                 y=0;
              }  //if()
           }   //for (y)
        }  //for (x)
    
        // sort array using swap and bubbles
    
        for(int y=0; y < arrlen-1; y++ )
        {
           for(int x=0; x < arrlen-1; x++ )
           {
              if (array[x] > array[x+1])
              {
                 int temp;
                 temp = array[x];
                 array[x] = array [x+1];
                 array [x+1] =temp;
              }  //
           }   //
        }  //
        printf("The random winning numbers are: ");
        printarray(array);
    }   // end generator
    
    void computerGenerator()
    {
       int array[6]={0};
       int arrlen;
    
       srand(time(NULL));
    
       arrlen = sizeof(array)/sizeof(int);
    
       for(int x=0; x < arrlen; x++ )
       {
           array[x] = (int) rand()%53+1;
           for(int y=0; y<x; y++)
           {
              if (array[y] == array[x])
              {
                 array[x] = (int) rand()%53+1;
                 y=0;
              }  //if()
           }   //for (y)
        }  //for (x)
    
        // sort array using swap and bubbles
    
        for(int y=0; y < arrlen-1; y++ )
        {
           for(int x=0; x < arrlen-1; x++ )
           {
              if (array[x] > array[x+1])
              {
                 int temp;
                 temp = array[x];
                 array[x] = array [x+1];
                 array [x+1] =temp;
              }  //
           }   //
        }  //
        printf("The computer comparison numbers are: ");
        printarray(array);
    }   // end generator
    
    void printarray(int*arrpt)
    {
    
       for (int x=0; x<6; x++)
       {
           printf("%d ", arrpt[x]);
       }
       printf("\n\n");
    }      // end printarray

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int counter=numPlay;
    
       printf("                 Welcome to the lotto game!!\n\n");
       printf("This game asks for the number of times you would like\n");
       printf("to print 6 random numbers (quick pick lotto) in ascending order\n");
       printf("for you and then 6 more random numbers (winning numbers)for you to compare.\n\n");
    
       printf("How many games would you like to play? ");
       scanf("%d", &numPlay);
    
       for( counter; counter > 0;  counter--)
    The first bolded line has no effect really on your program. All it does is assign whatever random value is in numPlay at that time to counter. If you change numPlay later on, it has no effect on what counter stores.

    The second bolded line, the initialization phase, is where you should make the assignment:
    Code:
    for( counter = numPlay; counter > 0;  counter--)
    And if you want it to repeat, wrap the whole thing in a loop and prompt for exit or continuation.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    It makes perfect sense. I'm sure this will help my loop run properly. Thanks for clarifying that.
    Unfortunately, it still isn't calling my functions, so I can't really see the program run to check and tweak where necessary. The program will go straight from asking the user how many times they want to play to ending the program with nothing in between.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by BungleSpice
    It makes perfect sense. I'm sure this will help my loop run properly. Thanks for clarifying that.
    Unfortunately, it still isn't calling my functions, so I can't really see the program run to check and tweak where necessary. The program will go straight from asking the user how many times they want to play to ending the program with nothing in between.
    Well if you haven't fixed that in your loop, there's a very good chance the counter > 0 portion of your loop evaluates as false. As such, it will never get to your functions. On a side note, you should only be calling srand once per program, unless you need to reseed your random generator with a specific seed.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    oops...forgot about the srand. To make this program, I was tweaking and old (and incorrect) program we did in class. I fixed it in one, but not this one. Thanks.
    As for the functions, I have fixed the loop as per your direction, and I guess it would make sense that it is reading FALSE, but how can I make it read TRUE so it will call my functions?

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    8
    Fist off, I don't see how this can compile as a C program. You've used a lot of loops woth the variable declared within the for() part. In C, You just can't have a for(int x=0;x<10;x++).

    Anyway, the main problem is here:

    Code:
     for( counter; counter > 0;  counter--)
       {
             void userGenerator();
             void computerGenerator();
       }
    You don't need to give the return type in a function call. Replace it with:

    Code:
     for( counter=numPlay; counter > 0;  counter--)
       {
             userGenerator();
             computerGenerator();
       }
    Hope that helps.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Fist off, I don't see how this can compile as a C program. You've used a lot of loops woth the variable declared within the for() part. In C, You just can't have a for(int x=0;x<10;x++).
    Ah. You caught the problem. I didn't even see it. The thing is, it is valid code. It just doesn't do what they expect.

    For starters, the loop is fine. Or rather, the loop is valid C. With the change I mentioned, and that you restated, the loop will funciton as they want. However, without changing it to have the assignment, the loop is still valid C. The loop is not incorrect in the sense of the language. It was incorrect only because they didn't have counter store a valid value before the loop.

    The actual problem was in the loop itself, as you pointed out. Here's why:
    Code:
       {
             void userGenerator();
             void computerGenerator();
       }
    The keyword void turns these from function calls into function prototypes. However, it is valid C. It is valid to prototype the functions there, because they're at the beginning of a code/scope block. Thus, it's valid to add any new variables and prototypes you feel like there.

    The problem is that because they're considered prototypes (due to the void prefixing), they don't call the functions, rather, it jus says "There are two functions some place that are like this...". It doesn't call. It prototypes.

    However, it is still valid C code no matter what way you have it. It just doesn't do what you exepct it to.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    c99
    c99 is offline
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Tethys

    In C, You just can't have a for(int x=0;x<10;x++).
    Incorrect.

    The current standard, C99, allows it, however it must be noted that any variable
    declared within a for loop only has block (local) scope.
    Last edited by c99; 03-06-2004 at 12:35 AM.
    R.I.P C89

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    8
    Hmm. Sorry, I didn't know that.

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    26
    Gotcha! It should be void of the word "void". Ha!
    And thanks so much for the explaination. I now know that not only does it work, I know why it works, which is so important for the beginner like me. I really do appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  2. Replies: 9
    Last Post: 01-26-2008, 02:12 AM
  3. calling functions: exit and return
    By 911help in forum C Programming
    Replies: 3
    Last Post: 12-28-2007, 12:24 PM
  4. I Need Help Defining and Calling Functions
    By jonbuckets in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2007, 09:46 AM
  5. Calling functions help
    By ForlornOdium in forum C++ Programming
    Replies: 14
    Last Post: 09-29-2003, 08:40 PM

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