Thread: codes keeps crashing with random (BEGINNER)

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    codes keeps crashing with random (BEGINNER)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    #define THREE 3
    int main (void)
    {
        int check = 0;
        int first_number = rand() % 101;
        int second_number = rand() % 101;
        int third_number = rand() % 101;
        
        while(check != THREE)
        {
            if (first_number % 2 == 0 || second_number % 2 == 0 || third_number % 2 == 0)
            {
                check++;
            }
            if (first_number % 2 != 0 || second_number % 2 != 0 || third_number % 2 != 0)
            {
                check++;
            }
            if (first_number > 50 || second_number > 50 || third_number > 50)
            {
                check++;
            }
            else
            {
                check = 0;
            }
        }
        if (check == THREE)
        {
            printf("%d %d %d",first_number,second_number,third_number);
        }
        else
        {
            srand (time(NULL));
            printf("NOTHING");
        }
        return 0;
    
    
    }
    Like I wrote in the title im a beginner and I NEED HELP, so my problem is the code keeps crushing and i dont understand why.

    I am thankful for any kind of help/explanation.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Crashes, no. Loops for a very long time, yes.

    What is this code supposed to do? It loops on the same numbers again and again, are you sure that's what you want?
    Last edited by GReaper; 12-09-2017 at 03:22 AM.
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    it's working to the point of it's doing exactly what you told it to. It helps you, if you put printf in your code here and there where you're working on variables with values so you can see what is really going on. then remove them after you know it is doing not what you told it to necessarily but that it is doing what you want it to do instead.
    Code:
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Hello,32 32 54
    Last edited by userxbw; 12-09-2017 at 09:50 AM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    839
    First, I agree with @GReaper. I think you need to work out what you are trying to do on paper BEFORE coding.

    You are also running srand() at the end of the run. It is meaningless at this point. You should run srand() BEFORE any call to rand().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2017, 06:51 PM
  2. Replies: 7
    Last Post: 07-31-2017, 10:14 AM
  3. Beginner: How to Reverse an array of RANDOM numbers??
    By Kaylah Hubbard in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2016, 01:14 AM
  4. Crashing while using random numbers
    By daynorb in forum C Programming
    Replies: 7
    Last Post: 10-08-2011, 07:42 PM
  5. help with array codes....plz,,,,,,beginner
    By shawndotting in forum C Programming
    Replies: 4
    Last Post: 03-15-2010, 01:41 AM

Tags for this Thread