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 8774281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:31:30+00:00 2026-06-13T18:31:30+00:00

In the program below, when the program goes into the while loop, it stops

  • 0

In the program below, when the program goes into the while loop, it stops working. When I tried to assisgn values without the loop, it woked fine.
Can anybody tell me what the problem is?
I am using Visual Studio 2010.

#include <stdio.h>
#include<iostream.h>
#include <conio.h>

using namespace std;
int random(int min,int max);

struct pts
{
    int x;
    int y;
};

int main()
{
    struct pts *p;
    int w = 600,h=400;
    int nmax,kmax,k=0,n=0;

    while(k<5)
    { 
        p[0].x = random(0,h-1);
        p[1].y = random(0,w-1);
        cout << p[0].x << "   " << p[1].y << "\n";
        k++;
     }
     getch();
     return 0;
}

int random(int min,int max)
{
    int n=0;
    n=(rand()%(max-min+1))+min;
    return n;
}
  • 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-13T18:31:31+00:00Added an answer on June 13, 2026 at 6:31 pm

    The while loop itself is working fine. It’s just that you’re corrupting memory. Your problem is with:

    struct pts *p;
    p[0].x = whatever;
    

    with no intervening setting of p to a valid memory block. In other words, you are using an unitialised pointer, hence undefined behaviour, hence all bets are off.

    Since you (currently) only seem to be using p[0] and p[1], you can probably change:

    struct pts *p;
    

    to:

    struct pts p[2];
    

    Of course, if you want a variable structure, you can use:

    struct pts *p = new pts[500];
    

    replacing 500 with whatever size you want, of course.


    You might also want to consider upgrading to a more recent compiler, iostream.h and conio.h are anachronisms.

    Here’s a complete program that shows how to do it, at least until you decide to store things a little more “sanely” inside the loop:

    #include <iostream>
    #include <stdlib.h>
    
    struct pts
    {
        int x;
        int y;
    };
    
    int random(int min,int max)
    {
        int n=0;
        n=(rand()%(max-min+1))+min;
        return n;
    }
    
    int main()
    {
        struct pts p[2];
        int w = 600,h=400;
        int k=0;
    
        while(k<5)
        {
            p[0].x = random(0,h-1);
            p[1].y = random(0,w-1);
            std::cout << p[0].x << "   " << p[1].y << "\n";
            k++;
         }
         return 0;
    }
    

    A sample run of that gives:

    183   286
    377   115
    193   535
    186   492
    249   421
    

    (and, in fact, will probably always give that sequence since you don’t call srand to set the seed – your numbers may well be different to mine but they’ll give you the same sequence every time).


    If you want a better baseline to start with, see:

    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    struct pts { int x; int y; };
    
    int random (int min, int max) {
        return (rand() % (max - min + 1)) + min;
    }
    
    int main (void) {
        pts *p = new pts[5];
        int w = 600, h = 400;
        int k = 0;
    
        srand (time (0));
        while (k < 5) {
            p[k].x = random (0, h - 1);
            p[k].y = random (0, w - 1);
            std::cout << p[k].x << "   " << p[k].y << "\n";
            k++;
        }
        delete[] p;
        return 0;
    }
    

    This shortens the code a bit by removing unnecessary stuff, and gets rid of your undefined-behaviour problem.

    It also initialises the random number generator and populates the array properly.

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

Sidebar

Related Questions

The below program calculates 2 raised to the power n without using any loop,runtime
In the below program,i am not getting values from printf . #include<stdio.h> int main()
The program below prints the following data: Wed,Jun,13,10:37:34,2012,759,41,0,30,10,0,0,1 Wed,Jun,13,10:38:34,2012,767,33,0,25,6,0,0,2 Wed,Jun,13,10:39:34,2012,758,42,0,32,10,0,0,0 Wed,Jun,13,10:40:35,2012,758,42,0,29,11,0,0,2 Wed,Jun,13,10:41:35,2012,761,39,0,34,5,0,0,0 Wed,Jun,13,10:42:35,2012,769,31,0,22,6,0,0,3 Wed,Jun,13,10:43:35,2012,754,46,0,29,17,0,0,0
The program below prints: my name is:null my name is:null Someclass static init AFAIK
This program below moves the last (junior) and the penultimate bytes variable i type
(In short: main()'s WaitForSingleObject hangs in the program below). I'm trying to write a
Below program contains two show() functions in parent and child classes, but first show()
In this below program i am printing the contents of the div when i
I have the below program that will move files from one directory to other.
I have a program shown below: Socket receiveSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); EndPoint

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.