Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:05:37+00:00 2026-06-13T23:05:37+00:00

I’m having issues with the code pasted below where it just hangs during runtime.

  • 0

I’m having issues with the code pasted below where it just hangs during runtime. VS2010 doesn’t give me any warnings or errors.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


void clear_buffer(void)
{
    while(getchar() != '\n');
}

int validate(int low, int high) {
    int num;

    scanf("%d", &num);
    while(num < low || num > high) 
    {
        clear_buffer();
        printf("INVALID! Must enter value between %d and %d: ", low, high);
        scanf("%d", &num);
    }
    return num;
}

int getRand(int max) {
    int number;
    number = rand() % max + 1;
    return number;
}

int validatePick(int pick, int one, int two, int three, int four, int five) {
    int valid = 0;

    if (pick != one && pick != two && pick != three && pick != four && pick != five) {
        valid = 1;
    } else {
        valid = 0;
    }
    return valid;
}
void prnt(int qty, int one, int two, int three, int four, int five, int six) {
    int i = 0, flag = 0;
    while (flag != 2) {
        flag = 0;
        if (sort2(&one, &two) == 0 && sort2(&three, &four) == 0 && sort2(&five, &six) == 0)
            flag = 1;
        if (sort2(&two, &three) == 0 && sort2(&four, &five) == 0)
            flag = 1;
        flag += flag;
    }

    printf("Picks: %d, ", one);
    while (i <= qty){
        if (i== 2 && qty == 2)
            printf("%d\n", two);
        else if (i == 2 && qty != 2) 
            printf("%d, ", two);

        if (i == 3 && qty == 3) 
            printf("%d\n", three);
        else if (i == 3 && qty != 3) 
            printf("%d, ", three);

        if (i == 4 && qty == 4)
            printf("%d\n", four);
        else if (i == 4  && qty != 4)
            printf("%d, ", four);

        if (i == 5 && qty == 5)
            printf("%d\n", five);
        else if (i == 5  && qty != 5) 
            printf("%d, ", five);

        if (i == 6 && qty == 6) 
            printf("%d\n", six);
        i++;
    }
}



int sort2(int *n1, int *n2) {
    int tmp, valid = 0;

    if (*n1 > *n2)
    {
        tmp = *n2;
        *n2 = *n1;
        *n1 = tmp;
        valid = 1;
    }
    return valid;
}

int main () {
    int num1, num2;
    int pick, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;

    srand(time(NULL));

    printf("LOTTERY GENERATOR\n");
    printf("Enter the maximum value between 1 and 100: ");
    num1 = validate(2,100);
    printf("Enter quantity of numbers to pick, between 1 and 6: ");
    num2 = validate(1, 6);

    one = getRand(num1);
    while (two == 0 || three == 0 || four == 0 || five == 0 || six == 0) {
        pick = getRand(num1);
        if (validatePick(pick, one, two, three, four, five) == 1 && two == 0)
            two = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && three == 0)
            three = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && four == 0)
            four = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && five == 0)
            five = pick;
        else  if (validatePick(pick, one, two, three, four, five) == 1)
            six = pick;
    }
    prnt(num2, one, two, three, four, five, six);
}

If I enter say 3 for Enter the maximum value between 1 and 100 and then 2 the program just hangs. I don’t understand why it does such. I don’t see an error in the code. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:05:38+00:00Added an answer on June 13, 2026 at 11:05 pm

    I think the problem is in your logic. If I understand the program correctly, you are trying to pick 6 different numbers, randomly generated in the range from 1 to num1.

    Now here’s the problem: Your while loop only terminates when all six variables (one, two.. six) are not 0. The only way that one of these variables can be set to a non-zero value is if validatePick returns 1, and that can only happen if the random number generated has not already been assigned to one of one, two… six.

    This boils down to something like a reverse pigeon-hole problem; you are trying to fill six pigeon-holes with less than six pigeons, an impossible task.

    If num1 is less than 6, then its impossible for you to satisfy the termination condition of your while loop, and your program will seemingly hang.

    You can verify this by putting an else case in your while loop and printing what random number you generated, and also printing what the value for each variable is at each iteration.

    Note that your second input, num2, is not referenced in any way until after your while loop, so the value you enter there will not be able to limit the number of unique random values your program tries to generate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.