Student class

How can I have a class as follows:
cin:
student 1 : ID,Name,Last name,Address
.
.
.
.
to
student 10 : ID,Name,Last name,Address

And have the ability to search(Based on ID And Last name).

Tnx
use a data structure:
1
2
3
4
5
6
7
8
9
10
11
12
struct students{
int ID;
string Name; // use char if u want
string Lastname; // use char if u want
string Adress;
}
//declaring the student and each characteristic
students student1;
student1.ID= "1232132";
student1.Name= "Sherlock";
student1.Lastname= "Holmes";
student1.Adress="221b Baker Street"

well, as for the ability to search i dont know yet
Last edited on
use a for loop to search based on highest ID or based on highest ASCII code of the name. reading in the names as int will work for the ascii method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct Student
{
int ID;
char* Name;
char* LastName;
char* Address;
};

Student* getperson()//some function
{
  Student* person1 = new Student;
  person1->ID = 12312;
  person1->Name = "My Name";
  person1->LastName = "Last name";
  person1->Address = "123 srwf kl";
  
  return person1
}
Last edited on
he asked for a class, not a struct
Structs and classes are the same thing, except for the default access level
@ maeriden: hm, did some reading and you're right. and i was sooo sure it's not the same :D
here it is, but it has a problem in the lines 66-68 because i dont know why after that it loads and it stops, but i think u will understand the idea.
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
#include <iostream>
#include <map>
#include <string>
#include <new>
using namespace std;
class Student{
        int id;
        string name, lastname, address;
    public:
    void SetStudentProperties(int,string,string,string);
    void GetInfo(int);
    map<int, string> mystudents;
        map<int, string>::iterator it;
        pair<map<int, string>::iterator,bool> ret;
    };

    void Student::SetStudentProperties (int id, string name, string lastname, string address){
        map<int, string> mystudents;
        map<int, string>::iterator it;
        pair<map<int, string>::iterator,bool> ret;
        string fullinfo= "Name: "+name+"\nSurname: "+lastname+"\nAdress"+address+"\n";
        mystudents.insert(pair<int, string>(id,fullinfo));
    }
    void Student::GetInfo(int id){
        map<int, string> mystudents;
        string getinfo;
    if(mystudents.find(id) == mystudents.end()){
    cout<<"ERROR! Unexisting ID.";
    }
    else{
    getinfo=mystudents.find(id)->second;
    cout<<getinfo;
    }

    }




int main(){
    string *address1, *name2, *surname;
    int id2;

    string answer;
    do{
    int i;
    int a;
    int *p;
    cout<<"Enter quantity of students: ";
    cin>>a;
    p= new (nothrow) int [a];
    address1= new (nothrow)string [a];
    name2= new (nothrow)string [a];
    surname= new (nothrow)string [a];
Student man[a];
        for(i=0;i<a; i++){
        cout<<"Student "<<"Id: ";
        cin>>p[i];
        cout<<"Student "<<"Address: ";
        cin>>address1[i];
        cout<<"Student "<<"Name: ";
        cin>>name2[i];
        cout<<"Student "<<"Surname: ";
        cin>>surname[i];
        }
          for(int c=0; c<a;a++){
            man[c].SetStudentProperties(p[c], name2[c], surname[c], address1[c]);
            }


    cout<<"Check for someone? Enter his ID: ";
    cin>>id2;
    for(int d=0; d<a;d++){
        if(id2 == p[d]){
        man[d].GetInfo(id2);
        }
        }
        for(i=0; i<a;i++){
        delete[] p;
        }


    cout<<"Do you want to run again?\n1. Yes\n2. No\n";

    }while(answer=="1" || answer=="Yes" || answer=="y" ||answer=="Y" || answer=="yes");
  return 0;

}
@SirSen

I believe your problem, line 66-68
1
2
3
 for(int c=0; c<a;a++){
            man[c].SetStudentProperties(p[c], name2[c], surname[c], address1[c]);
            }


is the for loop. It should read for(int c=0;c<a;c++)
omg, how could i miss that =O thanks so much!
Btw, check line 84, i forgot to tell the program to wait for an input to rerun the program;
1
2
cout<<"Do you want to run again?\n1. Yes\n2. No\n";
cin>>answer;
Last edited on
@SirSen

Well, I missed that!! In my defense, I wasn't really for anything other than the rows that contained the problem. Well, I guess I should look more carefully at code in the future. Thanks for pointing that one out to me. ;)

Merry Christmas and a Joyous New Year..
hahaha its ok. In fact, I didn't realize until i ran the program with ur correction, so thank you. Thanks and have a merry Christmas and a joyous New Year as well =)
Last edited on
Topic archived. No new replies allowed.