Random variable difficulties

This is a discussion on Random variable difficulties within the C Programming forums, part of the General Programming Boards category; I am having trouble running the following code. I am getting a message segmentation fault (core dumped) Code: #include<stdio.h> #include<ctype.h> ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    Random variable difficulties

    I am having trouble running the following code. I am getting a message segmentation fault (core dumped)


    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<time.h>
    
    
    main()
    
    
    {
    
    
    int iuser;
    int irand;
    srand(time(NULL));
    
    
    irand = (rand() % 10) + 1;
    
    
    
    
      printf("Guess a Number: ");
      scanf("%f", &iuser);
      
      if ( isdigit(iuser) == 0)
      {
         printf("Please enter a number next time.");
     }
      else
         {
         if ( iuser == irand )
         printf("Woot Woot you got it right.");
         
         else 
         printf("sorry you lost.");
    }
    
    
    getchar();
    getchar();
    getchar();
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,099
    Try compiling with a higher warning level. You have some problems:

    1. main needs a return type and arguments
    2. you didn't include a header file for srand and rand (hint: it is in stdlib.h)
    3. scanf is using the wrong format
    4. isdigit expects that you give it a character like 'a' 'b' '5' etc, and tells you whether that single character is a digit

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Turn on your compiler warnings, then fix errors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random variable with exponential distribution
    By momnia in forum C Programming
    Replies: 4
    Last Post: 01-07-2013, 08:42 AM
  2. random variable names
    By NeMewSys in forum C Programming
    Replies: 7
    Last Post: 06-12-2008, 11:26 AM
  3. Random Variable names
    By Milhas in forum C Programming
    Replies: 14
    Last Post: 03-26-2008, 05:03 PM
  4. assigning random value to a variable
    By shaple in forum C Programming
    Replies: 3
    Last Post: 07-17-2007, 10:14 AM
  5. random number between 0 and variable value
    By HybridM in forum C++ Programming
    Replies: 23
    Last Post: 02-06-2003, 09:14 AM

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