Need Help With Srand

This is a discussion on Need Help With Srand within the C Programming forums, part of the General Programming Boards category; I made this program for an exercise from my book. I would like a random number be generated but it's ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    38

    Need Help With Srand

    I made this program for an exercise from my book. I would like a random number be generated but it's always 3 and only changes when i close and open the program. I used the
    Code:
    srand(time());
    statement in a different program and it worked but with this one it doesnt work it will say, "Insufficient number of arguments to 'time"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int main()
    {
    	int	iTotalQuestions, iRndQuestion, iQuestionsAsked; 
    	int iWrong, iAnswer, iCorrect;
    	
    	
    	printf("\t\t***Math Quiz***");
    	printf("\n\nHow many questions would you like to answer?");
    	scanf("%d", &iTotalQuestions);
    	printf("\n%d questions it is then", iTotalQuestions);
    
    	srand(1-5);
    
    	iRndQuestion = (rand() %5)+1;
    
    	printf("\n\nLets get started!!!!!");
    	
    	iWrong = iAnswer = iCorrect = iQuestionsAsked = 0;
    	
    	while(iTotalQuestions>iQuestionsAsked) {
    		switch(iRndQuestion)
    			case 1:{
    			 printf("\n8 divided by 2 equals:  ");
    			 scanf("%d", &iAnswer);
    				if(iAnswer==4){
    				printf("\n\t\tCorrect!!!");
    				iQuestionsAsked++;
    				iCorrect++;
    				break;
    			}
    				else {
    				printf("\n\t\tWrong :(");
    				iQuestionsAsked++;
    				iWrong++;
    				break;
    			}
    			case 2:
    			 printf("\n8 multiplied by 4 equals:  ");
    			 scanf("%d", &iAnswer);
    				if(iAnswer == 32){
    				printf("\n\t\tCorrect!!!");
    				iQuestionsAsked++;
    				iCorrect++;
    				break;
    			}
    				else {
    				printf("\n\t\tWrong :(");
    				iQuestionsAsked++;
    				iWrong++;
    				break;
    			}
    			case 3:
    			 printf("\n10 squared equals:  ");
    			 scanf("%d", &iAnswer);
    				if(iAnswer == 100){
    				printf("\n\t\tCorrect!!!");
    				iQuestionsAsked++;
    				iCorrect++;
    				break;
    			}
    				else {
    				printf("\n\t\tWrong :(");
    				iQuestionsAsked++;
    				iWrong++;
    				break;
    			}
    
    			case 4:
    			 printf("\nThe square root of 4 is:  ");
    			 scanf("%d", &iAnswer);
    				if(iAnswer == 2){
    				printf("\n\t\tCorrect!!!");
    				iQuestionsAsked++;
    				iCorrect++;
    				break;
    			}
    				else {
    				printf("\n\t\tWrong :(");
    				iQuestionsAsked++;
    				iWrong++;
    				break;
    			}
    			case 5:
    			 printf("\n9 plus 19:  ");
    			 scanf("%d", &iAnswer);
    				if(iAnswer == 28){
    				printf("\n\t\tCorrect!!!");
    				iQuestionsAsked++;
    				iCorrect++;
    				break;
    			}
    				else {
    				printf("\n\t\tWrong :(");
    				iQuestionsAsked++;
    				iWrong++;
    				break;
    			}
    		}
    } 
    
    	
    	if(iQuestionsAsked==iCorrect) {
    		printf("\n\n\t\tAMAZING YOU GOT THEM ALL RIGHT!!!!!!\n\n\n");
    		return 0;
    		}
    	printf("\n\nYou got a total of %d correct and %d wrong\n\n\n", iCorrect, iWrong);
    	
    }
    	/*Create a math quiz program that prompts the user for how many
    questions to ask. The program should congratulate the player if
    he or she gets the correct answer or alert the user of the correct
    answer in the event the question is answered incorrectly.
    The math quiz program should also keep track of how many
    questions the player has answered correctly and incorrectly and
    display these running totals at the end of the quiz.*/

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    srand(time(NULL))

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > statement in a different program and it worked but with this one it doesnt work it will say, "Insufficient number of arguments to 'time"
    Well I'm guessing you had in your other program
    srand(time(NULL));

    If you didn't, your program was broken, even if it appeared to work for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    38
    Added the change to time and one more line and everything worked fine thanks for the help everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. srand and rand
    By pobri19 in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2008, 07:09 AM
  2. srand() in .Net
    By Sad Programmer in forum C++ Programming
    Replies: 6
    Last Post: 07-28-2003, 05:01 PM
  3. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM
  4. When to srand()?
    By Imperito in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2002, 12:20 AM
  5. srand()... possible reasons for failure
    By lightatdawn in forum C++ Programming
    Replies: 3
    Last Post: 12-18-2001, 01:33 AM

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