Whats Wrong In This Code......HELP

Write a C++ program to create a class District. having district_code, district_name, area_sqft, population, literacy_rate. For displaying details use appropriate manipulators. The program should contain the following menu :
a. Accept details of n district
b. Display details of the district.
c. Display details of the district having the highest literacy rate.
d. Display details of the district having the least population.


So This Is The Assignment I am Trying To Solve But No Out Put I Have Written Code:
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include<iostream>
#include<stdlib.h>
using namespace std;

class District
{
    int district_code,area_sqft,pop,lr;
    char name[20];
public:
    void accept()
    {
        cout<<"\n\nEnter The Details:";
        cout<<"\nDistrict Code:";
        cin>>district_code;
        cout<<"District Name:";
        cin>>name;
        cout<<"Area In Sqft:";
        cin>>area_sqft;
        cout<<"District Population:";
        cin>>pop;
        cout<<"District Literacy Rate:";
        cin>>lr;
        cout<<"\nDetails Have Been Entered!!";
    }

    void display()
    {
        cout<<"\nEntered Details Are:";
        cout<<"\nDistrict Code:"<<district_code;
        cout<<"\nDistrict Name:"<<name;
        cout<<"\nArea In Sqft:"<<area_sqft<<" Sqft";
        cout<<"\nDistrict Population:"<<pop;
        cout<<"\nDistrict Literacy Rate:"<<lr<<"\n\n";

    }

    void hlrate(District D[],int n,int i)
    {
        int maxx,pos;
        maxx=D[0].lr;
        for(i=0;i<n;i++)
        {
            if(maxx<D[i].lr)
            {
                maxx=D[i].lr;
                pos=i;
                cout<<"Details Of District Having Highest Literacy Rate Is:\n";
                D[pos].display();
            }
        }
    }

    void lpop(District D[],int n,int i)
    {
        int minn,pos1;
        minn=D[0].pop;
        cout<<"Details Of District Having Least Population Is:\n";
        for(i=0;i<n;i++)
        {
            if(minn>D[i].pop)
            {
                minn=D[i].pop;
                pos1=i;
                D[pos1].display();
            }
        }
    }
};

int main()
{
    int i,n,ch;
    District D[20];
    cout<<"How Many Details You Want To Enter(Max 20 Details Can Be Entered):";
    cin>>n;
    while(1)
    {
        cout<<"\n\n---MENU---\n 1.Accept \n 2.Display \n 3.Display details of district having highest literacy rate. \n 4.Display details of district having least population. \n 5.Exit\n";
        cout<<" \n Enter Your Choice :";
        cin>>ch;
        switch(ch)
        {
            case 1:
                    for(i=0;i<n;i++)
                    {
                        D[i].accept();
                    }
                    break;
            case 2:
                    for(i=0;i<n;i++)
                    {
                        D[i].display();
                    }
                    break;
            case 3:
                    D[i].hlrate(D,n,i);
                    break;
            case 4:
                    {
                        D[i].lpop(D,n,i);
                    }
                    break;
            case 5:
                    {
                        exit(1);
                    }
            default:
                    cout<<"MENU PARAT VACH NEET!!";
                    break;
        }
    }
}


Whats Should Be The Correct Code plz Help.
Any Suggestions In The Code Are Appreciated.
The 3rd Switch Case Is Not Working

Sorry For My Bad English
Last edited on
Line 16 will only read one word. So if you enter a name like "Tinton Falls", it will only read "Tinton". Then line 18 will try to read "Falls" into a number and that fails. from then on, the cin stream is flagged as bad and all reads fail.

To solve this, I'd read the name with getline(), but that introduces a new problem. Line 14 reads the district code but NOT the newline that you entered. So you have to skip that newline before reading the name.

Another problem is hlrate and lpop need to initialize pos and pos1.

Finally, hprate and lpop should find the smallest/largest item first, and then print it out. So they need to print the results after the loop.
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<iostream>
#include<stdlib.h>
using namespace std;

class District
{
    int district_code,area_sqft,pop,lr;
    char name[20];
public:
    void accept()
    {
        cout<<"\n\nEnter The Details:";
        cout<<"\nDistrict Code:";
        cin>>district_code;
        cout<<"District Name:";
	cin.ignore(10000, '\n');
	cin.getline(name, sizeof(name));
        cout<<"Area In Sqft:";
        cin>>area_sqft;
        cout<<"District Population:";
        cin>>pop;
        cout<<"District Literacy Rate:";
        cin>>lr;
        cout<<"\nDetails Have Been Entered!!";
    }

    void display()
    {
        cout<<"\nEntered Details Are:";
        cout<<"\nDistrict Code:"<<district_code;
        cout<<"\nDistrict Name:"<<name;
        cout<<"\nArea In Sqft:"<<area_sqft<<" Sqft";
        cout<<"\nDistrict Population:"<<pop;
        cout<<"\nDistrict Literacy Rate:"<<lr<<"\n\n";

    }

    void hlrate(District D[],int n)
    {
        int maxx,pos=0;
        maxx=D[0].lr;
        for(int i=0;i<n;i++)
        {
            if(maxx<D[i].lr)
            {
                maxx=D[i].lr;
                pos=i;
            }
        }
	cout<<"Details Of District Having Highest Literacy Rate Is:\n";
	D[pos].display();
    }

    void lpop(District D[],int n)
    {
        int minn,pos1=0;
        minn=D[0].pop;
        for(int i=0;i<n;i++)
        {
            if(minn>D[i].pop)
            {
                minn=D[i].pop;
                pos1=i;
            }
        }
        cout<<"Details Of District Having Least Population Is:\n";
	D[pos1].display();
    }
};

int main()
{
    int i,n,ch;
    District D[20];
    cout<<"How Many Details You Want To Enter(Max 20 Details Can Be Entered):";
    cin>>n;
    while(1)
    {
        cout<<"\n\n---MENU---\n 1.Accept \n 2.Display \n 3.Display details of district having highest literacy rate. \n 4.Display details of district having least population. \n 5.Exit\n";
        cout<<" \n Enter Your Choice :";
        cin>>ch;
        switch(ch)
        {
            case 1:
                    for(i=0;i<n;i++)
                    {
                        D[i].accept();
                    }
                    break;
            case 2:
                    for(i=0;i<n;i++)
                    {
                        D[i].display();
                    }
                    break;
            case 3:
                    D[i].hlrate(D,n);
                    break;
            case 4:
                    {
                        D[i].lpop(D,n);
                    }
                    break;
            case 5:
                    {
                        exit(1);
                    }
            default:
                    cout<<"MENU PARAT VACH NEET!!";
                    break;
        }
    }
}

Topic archived. No new replies allowed.