Please correct my code

Hi, I am new to this forum and this is my first post and I have been learning c++ by myself online as well as from a refernce book but I have encountered a problem in this book.
The question is :-
Define a class named Admission in C++ with following description?
Private members:
admno integer (Ranges 10-1500)
name string of 20 characters
cls integer
fees float
Public members:
A constructor which initialized admno with 10, name with “NULL”, cls with 0 & fees with 0
Function getdata() to read the object of Admission type.
Function putdata() to print the details of object of admission type.
Function draw_nos() to generate the admission no. randomly to match with admno and display the detail of object.
and here is my code but it is not giving expected results, I think that the problem is in draw_nos()function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>

class admission
{
int admno;
char name[20];
char cls;
float fees;
public :
void readdata();
void display();
int drawnos();
int adnos(){
return admno;
}
};
void admission::readdata()
{
cout<<"Enter the admission no"<<endl;
cin>>admno;
cout<<"Enter the name"<<endl;
gets(name);
cout<<"Enter the class and fees"<<endl;
cin>>cls;
cout<<"Enter the fees"<<endl;
cin>>fees;
}
void admission::display()
{
cout<<"Admission no. : "<<admno<<endl;
cout<<name;
cout<<"\nclass : "<<cls<<endl;
cout<<"Fees : "<<fees<<endl;
}
int admission::drawnos()
{
int r1;
srand(time(0));
r1=(rand()%2000)+10;
return r1;
}
int main()
{
clrscr();
    admission ad[5],ad1;
    int po;
    for(int n=0;n<5;n++)
    {
    cout<<"Enter the details of "<<n+1<<" student "<<endl;
    ad[n].readdata();
    }
    for(int x=0;x<10;x++)
    {
    po=ad1.drawnos();
    if(ad[x].adnos()==po)
    {
    ad[x].display();
    break;
    }
    }
    getch();
    return 0;
}

Please help me
Last edited on
1) When posting code here, please enclose it in code tags, so that it is easier to read.

2) What do you mean by "it is not giving expected results"? Please be more specific.
Thanks for the reply
the problem in this code is that, in the function Draw_Nos() it is not choosing any student and I am not able to display the student details.
1) When posting code here, please enclose it in code tags, so that it is easier to read.
First of all I should point out that you create an array of only 5 admission objects but you try to index outside it when you call the adnos and display member functions. C++ does not check at run time that your array indices are inside the particular array you're accessing - you need to make sure of that yourself.

There are two main reasons your program prints nothing. First is because you seed the random number generator using the time function every time before you call the rand function. The time function will return the same value as last time if you run it again in the same second (of clock time) which means srand() will be seeded with the same value and rand() will generate the same value.
Given that your computer is almost certainly fast enough to run the loop which calls display() much more than 10 times in one second this means that the variable 'po' is usually the same all 10 times through the loop.

You should move the srand line to the start of the program.

The other reason you get nothing printed is that with a range of 2000 values your random number will only get a match one in 2000 times.

I suspect you mean the display loop to be something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
        bool printed = false;
        while(!printed)
        {
                po=ad1.drawnos();
                for(int x=0;x<5;x++)
                {
                        if(ad[x].adnos()==po)
                        {
                                ad[x].display();
                                printed = true;
                                break;
                        }
                }
        }
@Plover thanks for what you have suggested and after making the changes, the program is running fine but according to the problem I need to call the display() from the drawnos();
Topic archived. No new replies allowed.