initializer element is not constant error - help

This is a discussion on initializer element is not constant error - help within the C Programming forums, part of the General Programming Boards category; I am working on a program that is to call many different functions from a choice menu. I been able ...

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

    initializer element is not constant error - help

    I am working on a program that is to call many different functions from a choice menu. I been able to get many of the functions done and call from the menu except this one function. When I compile it, it gives me a initializer element is not constant. This function was actually a program we had done earlier but now using it in a choice menu program. It works and compiles just fine on it's own but when I insert in as a function it gives me this error. The entire programs is long so I am only posting part of the function I am having problems with since this is a school assignment. I get the error right at the random number generator.

    Code:
    int multgame();  //function name
    
    srand(time(0));
      
    int i;
    int num1 = rand()%50;
    int num2 = rand()%50;
    int answer, guess, problems;
    
    
    //Answer to the random numbers generated
    answer = num1*num2; 
    
    //Get user input for how many problems they want to do   
     printf("How many problems do you want?\n");
     scanf("%d", &problems);

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    There is no array in this code. Post the line where the error is occurring.

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    979
    In the future, please indicate the line that is giving you the error. "Right at the random number generator" is not as clear as simply pointing out the offending line.

    I can only guess at your problem because you have not posted a complete program. My guess is that you have
    Code:
    int num1 = rand()%50;
    outside of any function. If this is the case, then that's the source of your error. Global variables cannot be initialized by a function call. They have to be initialized with a value that can be computed at compile time.

    You should avoid global variables in the first place, if it's feasible to do so; but at any rate you'll need to call rand() inside of a function.

    If this was not the issue, you'll have to be more clear.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    Sorry, the error is occuring where you stated cas. I see where I put the int when I prob shouldn't. Here is the previous program I am copying into the new one and making this into a function for the choice menu

    Code:
    int main() {
     
     
    //Set the random number
    srand(time(0)); 
      
    int i, num1, num2;      
    int answer, guess, problems;
    
    num1 = rand()%50+1;
    num2 = rand()%50+1;
    
    //Answer to the random numbers generated
    answer = num1*num2; 
    
    //Get user input for how many problems they want to do   
     printf("How many problems do you want?\n");
     scanf("%d", &problems); 
    
    //Starts the time for working on problems  
    int start = time(0);
    I still get the error, I suck so much when it comes to dealing with rand and srand. I have even read the "Eternally confused - using rand()" because even with this it would keep generating the same 2 numbers to multiply by.
    Last edited by jay32m; 11-17-2010 at 12:55 PM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You get the same problem everytime because you pick your random number outside the loop. You need to move the two rand() calls into your loop so that you get new numbers each time. That should also clear up the intializer not constant error you're getting.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Whoops, having never seen this error I was figuring this was an attempt to initialize an array with a non-constant size. My bad!

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    ohhh ok thanks commontater, that fixed it.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 05:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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