I have tried to pass an argument without using an additional string but I just thought that maybe that is the problem that I'm using a lot of characters in string. So I ended up using an extra string initializing it with an assignment operator (string detail1, string detail 2) in the main function but that didn't help and the error still kept coming. The program is running but after i have input the students name and id it fills the project id by its own and a dialogue box appears that says
Unhandled exception at 0x010163FF in Project2.exe: 0xC0000005: Access violation reading location 0x0000001C.
that eventually forces me to break. This is a program for "Automated FYP system".
#include <iostream>
#include <conio.h>
#include <string>
using namespace std;
class Student {
public:
string s_name;
int s_id;
void display() //function to display name and id
{
cout << "Student Name : " << s_name << endl
<< "Student ID : " << s_id << endl;
}
Student(char a[50], double b) //parameterized constructor to initialize name and id
{
s_name = a;
s_id = b;
}
};
class Project {
private:
Student* student1;
Student* student2;
Student* student3;
Student* student4;
int p_id;
string p_title;
string p_detail;
public:
Project(int pid, string ptitle, string pdetail, Student* s1, Student* s2)
{
p_id = pid;
p_title = ptitle;
p_detail = pdetail;
student1 = s1;
student2 = s2;
}
void show()
{
cout << "\nProject ID : " << p_id << endl;
cout << "Project Title : " << p_title << endl;
cout << "Project Detail : " << p_detail << endl;
}
void show1()
{
student1->display();
student2->display();
student3->display();
student4->display();
}
};
int main()
{
char n1[50], n2[50], n3[50], n4[50];
double ID1, ID2, ID3, ID4;
cout << "\nEnter name of student : ";
cin.getline(n1, 50);
cout << "Enter ID of student : ";
cin >> ID1;
Student S1 = Student(n1, ID1); //initializing the data members of object 'S' implicitly
cin.ignore();
cout << "\nEnter name of student : ";
cin.getline(n2, 50);
cout << "Enter ID of student : ";
cin >> ID2;
Student S2 = Student(n2, ID2); //initializing the data members of object 'S' implicitly
cin.ignore();
cout << "\nEnter name of student : ";
cin.getline(n3, 50);
cout << "Enter ID of student : ";
cin >> ID3;
Student S3 = Student(n3, ID3);
cin.ignore();
cout << "\nEnter name of student : ";
cin.getline(n4, 50);
cout << "Enter ID of student : ";
cin >> ID4;
Student S4 = Student(n4, ID4);
cout << "\nStudent 1 : " << endl;
S1.display();
cout << "\nStudent 2 : " << endl;
S2.display();
cout << "\nStudent 3 : " << endl;
S3.display();
cout << "\nStudent 4 : " << endl;
S4.display();
//PROJECT
string detail1 = "In collaboration with PCAA";
string detail2 = "Communication for Teacher/Student.";
Project P1('1', "Disaster Management System", detail1, &S1, &S2);
P1.show();
P1.show1();
Project P2('2', "E bridge", detail2, &S3, &S4);
P2.show();
P2.show1();
_getch();
return 0;
}
User contributions licensed under CC BY-SA 3.0