Dynamic arrays and pointers problem

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 1063 Views - Last Post: 22 February 2018 - 04:19 AM Rate Topic: -----

#1 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Dynamic arrays and pointers problem

Posted 21 February 2018 - 08:26 AM

If it's not pointer overwriting, then sorry for the incorrect title, but I think it is the case. The thing is: I am working on an exam program in C++ which is based on dynamic arrays (the reason is: I want to be able to enter new questions and answers to them additionaly if I want to). The problem is the following: Let's say we use 2 students and we enter 2 questions and then we start with exam. First student does the exam and the second one also does the exam. Now occurs the problem -> Second student's answers overwrote the first student's answers, so when I try to output the results, it takes last student's results as any other student's.

#include <iostream>
#include <iomanip>
#include <assert.h>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

char crt[] = "\n----------------------------------------------\n";

enum wayOf_Studying { REGULAR, DL };
enum study_Year { FIRST = 1, SECOND, THIRD, FOURTH };
enum answers { CORRECT, INCORRECT };

struct Test {
	string *_questions;
	int _numberOfquestions;
	answers *_correctAnswers;
};

struct Student {
	char _nameSurname[30];
	int _indexNumber;
	bool _didTest = false;
	int _username;
	int _password;
	bool _loginCreated = false;
	wayOf_Studying _way;
	study_Year _year;
	answers *_answers;
};

void show_Menu(int&);
void input_Students(Student &, Test &);
void Search(Student *, int, Test, int);
void Sort(Student *, int, Test, int);
void run_Test(Student &, Test);
int show_Students(Student *, int);
int show_Results(Student, Test);
void add_Questions(Test &, Student*, int);
void admin_Menu(Test&, Student*, int);
void create_Login(Student*, int);
bool student_Login(Student *, int, int);
int student_Recognize(Student *, int);

int main() {

	int max = 0;
	cout << "Enter number of students: " << flush;
	cin >> max;
	while (!cin || max < 0) {
		cout << "[!] Enter the whole number [!]: " << flush;
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cin >> max;
	}

	Test configuration;
	configuration._numberOfquestions = 0;
	configuration._questions = nullptr;
	configuration._correctAnswers = nullptr;

	Student * array = new Student[max];
	for (int i = 0; i < max; i++)
		input_Students(array[i], configuration);
	system("CLS");

	int choice = 0, student = 0, correct_Answers = 0;

	do {
		show_Menu(choice);
		if (choice == 6)
			break;
		if (choice == 1) {
			student = student_Recognize(array, max);
			if (array[student]._didTest == true)
				cerr << crt << "Student " << array[student]._nameSurname << " already took an exam!" << endl;
			else if (array[student]._loginCreated == true) {
				if ((student_Login(array, max, student)) == true) {
					run_Test(array[student], configuration);
				}
				else
					cout << crt << "Unsuccessful login!" << endl;
			}
			else if (array[student]._loginCreated == false)
				cout << crt << "Student " << array[student]._nameSurname << " has no created login!" << endl;
		}
		else if (choice == 2) {
			cout << crt << "\t\t:: LIST OF STUDENTS ::" << crt;
			student = show_Students(array, max);
			if (array[student]._didTest == false)
				cerr << crt << "Student " << array[student]._nameSurname << " has not taken an exam yet!" << endl;
			else {
				correct_Answers = show_Results(array[student], configuration);
				cout << crt << "Student " << array[student]._nameSurname << " has " << correct_Answers << "/" << flush;
				cout << configuration._numberOfquestions << flush;
				cout << "  ---->\t" << (correct_Answers * 100.) / configuration._numberOfquestions << "%" << crt;
			}
		}
		else if (choice == 3)
			Search(array, max, configuration, student);
		else if (choice == 4)
			admin_Menu(configuration, array, max);
		else if (choice == 5)
			create_Login(array, max);

	} while (true);

	delete[] array;
	array = nullptr;

	cout << crt << setw(33) << ":: Kraj programa :: " << crt << endl;
	system("PAUSE > NULL");
	return 0;
}

int student_Recognize(Student *obj, int max) {
	system("CLS");
	int student = 0, IndexNumber = 0;
	cout << crt << "Enter index number: " << flush;
	cin >> IndexNumber;
	while (!cin) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter index number [!]: " << flush;
		cin >> IndexNumber;
	}

	for (int i = 0; i < max; i++)
		if (obj[i]._indexNumber == IndexNumber)
			student = i;
	system("CLS");
	return student;
}

bool student_Login(Student *obj, int max, int student) {
	system("CLS");
	cout << crt << "\t\t:: LOGIN :: " << crt;
	int enteredUSERNAME;
	cout << "Enter username: " << flush;
	cin >> enteredUSERNAME;
	while (!cin) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter username [!]: " << flush;
		cin >> enteredUSERNAME;
	}

	int enteredPASS;
	cout << "Enter password: " << flush;
	cin >> enteredPASS;
	while (!cin) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter password [!]: " << flush;
		cin >> enteredPASS;
	}

	if (obj[student]._username == enteredUSERNAME && obj[student]._password == enteredPASS)
		return true;
	return false;
}

void create_Login(Student *obj, int max) {
	system("CLS");
	cout << crt << "\t\t:: CREATE LOGIN:: " << crt;
	int IndexNumber;
	cout << "Enter index number: " << flush;
	cin >> IndexNumber;
	int student;
	for (int i = 0; i < max; i++) {
		if (obj[i]._indexNumber == IndexNumber)
			student = i;
	}
	int password = 0;
	cout << crt << "Username: " << obj[student]._indexNumber << " (index number)" << endl;
	obj[student]._username = obj[student]._indexNumber;
	cout << "Enter 4-digit password: " << flush;
	cin >> password;
	while (!cin || password < 1000 || password > 9999) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter 4-digit password[!]: " << flush;
		cin >> password;
	}
	system("CLS");
	obj[student]._password = password;
	obj[student]._loginCreated = true;
	cout << crt << "Successfully created login! " << endl;
	cout << "Remember you login data: " << crt;
	cout << "USERNAME: " << obj[student]._username << endl;
	cout << "PASSWORD: " << obj[student]._password << crt;
}


void admin_Menu(Test& configuration, Student *obj, int max) {
	system("CLS");
	int choice = 0;
	cout << crt << "\t\t:: ADMIN MENU ::" << crt;

	cout << "\t1 - Add questions" << endl;
	cout << "\t2 - Let student take exam again" << crt;
	cout << "Choice: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > 2) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid choice [!]: " << flush;
		cin >> choice;
	}
	system("CLS");

	if (choice == 1) {
		cout << crt;
		add_Questions(configuration, obj, max);
		cout << crt;
	}
	if (choice == 2)
	{
		int student = show_Students(obj, max);
		obj[student]._didTest = false;
		cout << crt << "Student " << obj[student]._nameSurname << " can take an exam again!" << crt;
	}
}

void add_Questions(Test &configuration, Student* obj, int max) {
	int number = 0;
	cout << "How many questions are you going to enter: " << flush;
	cin >> number;
	while (!cin || number <= 0) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid number [!]: " << flush;
		cin >> number;
	}
	cin.ignore();


	int len = configuration._numberOfquestions + number;
	string * temp_Questions = new string[len];
	answers * temp_correct_Answers = new answers[len];
	answers * temp_Answers = new answers[len];

	for (int i = 0; i < configuration._numberOfquestions; i++) {
		temp_Questions[i] = configuration._questions[i];
		temp_correct_Answers[i] = configuration._correctAnswers[i];
	}

	for (int i = 0; i < max; i++)
		obj[i]._answers = temp_Answers;

	delete[] configuration._questions;
	configuration._questions = temp_Questions;
	delete[] configuration._correctAnswers;
	configuration._correctAnswers = temp_correct_Answers;

	int input = 0;
	answers answer;
	for (int i = configuration._numberOfquestions; i < len; i++) {
		cout << crt << "Enter " << configuration._numberOfquestions + 1 << ". question: " << crt;
		getline(cin, configuration._questions[i]);
		configuration._numberOfquestions++;
		do {
			cout << crt << "Enter correct answer for question " << configuration._numberOfquestions << " (1 - CORRECT | 2 - INCORRECT): " << crt;
			cout << "Answer: " << flush;
			cin >> input;
			cin.ignore();
			answer = answers(--input);
		} while (answer != CORRECT && answer != INCORRECT);
		configuration._correctAnswers[i] = answer;
	}
	system("CLS");
}

int show_Results(Student obj, Test configuration) {
	int correctAns = 0;
	for (int i = 0; i < configuration._numberOfquestions; i++) {
		if (obj._answers[i] == configuration._correctAnswers[i])
			correctAns++;
	}
	return correctAns;
}

void run_Test(Student & obj, Test configuration) {
	system("CLS");
	cout << crt << " --- WELCOME TO THE EXAM ---" << crt;
	answers answer;
	int input;
	for (int i = 0; i < configuration._numberOfquestions; i++) {
		cout << crt << configuration._questions[i] << crt;
		do {
			cout << "1 - CORRECT | 2 - INCORRECT >> " << flush;
			cin >> input;
			answer = answers(--input);
		} while (answer != CORRECT && answer != INCORRECT);
		obj._answers[i] = answer;
	}
	system("CLS");
	obj._didTest = true;
}

int show_Students(Student *pok, int max) {
	system("CLS");
	int choice = 0;
	for (int i = 0; i < max; i++)
		cout << i + 1 << " -> " << pok[i]._nameSurname << endl;
	cout << crt;
	cout << "Enter student's ordinal number: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > max) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid ordinal number [!]: " << flush;
		cin >> choice;
	}
	system("CLS");
	return --choice;
}

void input_Students(Student & pok, Test &configuration) {
	int input = 0;
	cin.ignore();
	cout << crt << "\t\t:: STUDENT INPUT ::" << crt;
	cout << "Name and surname: " << flush;
	cin.getline(pok._nameSurname, 29);
	cout << "Index number: " << flush;
	cin >> pok._indexNumber;
	pok._didTest = false;
	do {
		cout << "Way of studying (1 - regular | 2 - DL): " << flush;
		cin >> input;
	} while (input < 1 || input > 2);
	pok._way = wayOf_Studying(--input);
	do {
		cout << "Study year (1 - first | 2 - second | 3 - third | 4 - fourth): " << flush;
		cin >> input;
	} while (input < 1 || input > 4);
	pok._year = study_Year(input);

	pok._answers = nullptr;

}

void Search(Student * array, int max, Test configuration, int student) {
	int choice = 0, study_Year = 0, study_Way = 0;
	system("CLS");
	cout << crt << "\t\t:: SEARCH ::" << crt;
	do
	{
		cout << "\t1 - study year" << endl;
		cout << "\t2 - study way" << endl;
		cout << "\t3 - exam results" << crt;
		cin >> choice;
		system("CLS");
	} while (choice < 1 || choice > 3);

	if (choice == 1) {
		do {
			cout << "Enter study year (1 - first | 2 - second | 3 - third | 4 - fourth): " << flush;
			cin >> study_Year;
		} while (study_Year < 1 || study_Year > 4);
		cout << crt;
		for (int i = 0; i < max; i++) {
			if (array[i]._year == study_Year) {
				cout << array[i]._indexNumber << " " << array[i]._nameSurname << endl;
			}
		}
		cout << crt;
	}

	if (choice == 2) {
		do {
			cout << "Enter study way (1 - regular | 2 - DL): " << flush;
			cin >> study_Way;
		} while (study_Way < 1 || study_Way > 2);
		cout << crt;
		study_Way--;
		for (int i = 0; i < max; i++) {
			if (array[i]._way == study_Way) {
				cout << array[i]._indexNumber << " " << array[i]._nameSurname << endl;
			}
		}
		cout << crt;
	}

	if (choice == 3) {
		cout << crt;
		Sort(array, max, configuration, student);
		cout << crt;
	}

}

void Sort(Student *array, int max, Test configuration, int student) {
	int numberOf_passes = max - 1;
	bool change = true;
	while (change) {
		change = false;
		for (int i = 0; i < numberOf_passes; i++) {
			if (array[i]._didTest == false || array[i + 1]._didTest == false)
				continue;
			else if (show_Results(array[i], configuration) < show_Results(array[i + 1], configuration)) {
				swap(array[i], array[i + 1]);
				change = true;
			}
		}
		numberOf_passes--;
	}
	int correctAns = 0;
	for (int i = 0; i < max; i++) {
		if (array[i]._didTest == false) {
			cout << array[i]._indexNumber << " " << array[i]._nameSurname << "\t\tHAS NOT TAKEN AN EXAM" << endl;
		}
		else {
			correctAns = show_Results(array[i], configuration);
			cout << array[i]._indexNumber << " " << array[i]._nameSurname << flush;
			cout << "  ---->\t" << (correctAns * 100.) / configuration._numberOfquestions << "%" << endl;
		}
	}
}

void show_Menu(int &choice) {
	cout << crt << "\t\t:: MENU :: " << crt << endl;
	cout << "\t1 - Run test" << endl;
	cout << "\t2 - Show results" << endl;
	cout << "\t3 - Search " << endl;
	cout << "\t4 - ADMIN MENU " << endl;
	cout << "\t5 - Create login" << endl;
	cout << "\t6 - END" << crt;
	cout << "Enter choice: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > 6) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter valid choice [!]: " << flush;
		cin >> choice;
	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic arrays and pointers problem

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 09:55 AM

So perhaps you should use your debugger and watch where the students' answers are stored.
Was This Post Helpful? 1
  • +
  • -

#3 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:02 AM

View Postr.stiltskin, on 21 February 2018 - 09:55 AM, said:

So perhaps you should use your debugger and watch where the students' answers are stored.


Thanks for the answer. I already did debug many times and noticed the following: When one student enters his answers, they are applied to every other student's. (so I guess the bug is in the run_Test function?). I've learned at Uni about pointers and I haven't mastered them yet and I would appreciate any help in finding this bug.

I removed non-key functions in this code and added output of all student's answers as help in finding the bug, so if you are in helping me:

#include <iostream>
#include <iomanip>
#include <assert.h>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

char crt[] = "\n----------------------------------------------\n";

enum wayOf_Studying { REGULAR, DL };
enum study_Year { FIRST = 1, SECOND, THIRD, FOURTH };
enum answers { CORRECT, INCORRECT };

struct Test {
	string *_questions;
	int _numberOfquestions;
	answers *_correctAnswers;
};

struct Student {
	char _nameSurname[30];
	int _indexNumber;
	bool _didTest = false;
	wayOf_Studying _way;
	study_Year _year;
	answers *_answers;
};

void show_Menu(int&);
void input_Students(Student &, Test &);
void run_Test(Student &, Test);
int show_Students(Student *, int);
int show_Results(Student, Test);
void add_Questions(Test &, Student*, int);
void admin_Menu(Test&, Student*, int);

int main() {

	int max = 0;
	cout << "Enter number of students: " << flush;
	cin >> max;
	while (!cin || max < 0) {
		cout << "[!] Enter the whole number [!]: " << flush;
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cin >> max;
	}

	Test configuration;
	configuration._numberOfquestions = 0;
	configuration._questions = nullptr;
	configuration._correctAnswers = nullptr;

	Student * array = new Student[max];
	for (int i = 0; i < max; i++)
		input_Students(array[i], configuration);
	system("CLS");

	int choice = 0, student = 0, correct_Answers = 0;

	do {
		show_Menu(choice);
		if (choice == 6)
			break;
		if (choice == 1) {
			student = show_Students(array, max);
			if (array[student]._didTest == true)
				cerr << crt << "Student " << array[student]._nameSurname << " already took an exam!" << endl;

			else
				run_Test(array[student], configuration);

		}
		else if (choice == 2) {
			cout << crt << "\t\t:: LIST OF STUDENTS ::" << crt;
			student = show_Students(array, max);
			//printing answers for all students to check if they are the same (bug)
			cout << "\t\t:: STUDENT'S ANSWERS ::" << crt;
			for (int i = 0; i < max; i++) {
				cout << crt << array[i]._nameSurname << endl;
				for (int j = 0; j < configuration._numberOfquestions; j++) {
					cout << array[i]._answers[j] << "  " << flush;
				}
				cout << crt;
			}
			if (array[student]._didTest == false)
				cerr << crt << "Student " << array[student]._nameSurname << " has not taken an exam yet!" << endl;
			else {
				correct_Answers = show_Results(array[student], configuration);
				cout << crt << "Student " << array[student]._nameSurname << " has " << correct_Answers << "/" << flush;
				cout << configuration._numberOfquestions << flush;
				cout << "  ---->\t" << (correct_Answers * 100.) / configuration._numberOfquestions << "%" << crt;
			}
		}
		else if (choice == 4)
			admin_Menu(configuration, array, max);

	} while (true);

	delete[] array;
	array = nullptr;

	cout << crt << setw(33) << ":: Kraj programa :: " << crt << endl;
	system("PAUSE > NULL");
	return 0;
}

void admin_Menu(Test& configuration, Student *obj, int max) {
	system("CLS");
	int choice = 0;
	cout << crt << "\t\t:: ADMIN MENU ::" << crt;

	cout << "\t1 - Add questions" << endl;
	cout << "\t2 - Let student take exam again" << crt;
	cout << "Choice: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > 2) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid choice [!]: " << flush;
		cin >> choice;
	}
	system("CLS");

	if (choice == 1) {
		cout << crt;
		add_Questions(configuration, obj, max);
		cout << crt;
	}
	if (choice == 2)
	{
		int student = show_Students(obj, max);
		obj[student]._didTest = false;
		cout << crt << "Student " << obj[student]._nameSurname << " can take an exam again!" << crt;
	}
}

void add_Questions(Test &configuration, Student* obj, int max) {
	int number = 0;
	cout << "How many questions are you going to enter: " << flush;
	cin >> number;
	while (!cin || number <= 0) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid number [!]: " << flush;
		cin >> number;
	}
	cin.ignore();


	int len = configuration._numberOfquestions + number;
	string * temp_Questions = new string[len];
	answers * temp_correct_Answers = new answers[len];
	answers * temp_Answers = new answers[len];

	for (int i = 0; i < configuration._numberOfquestions; i++) {
		temp_Questions[i] = configuration._questions[i];
		temp_correct_Answers[i] = configuration._correctAnswers[i];
	}

	for (int i = 0; i < max; i++)
		obj[i]._answers = temp_Answers;

	delete[] configuration._questions;
	configuration._questions = temp_Questions;
	delete[] configuration._correctAnswers;
	configuration._correctAnswers = temp_correct_Answers;

	int input = 0;
	answers answer;
	for (int i = configuration._numberOfquestions; i < len; i++) {
		cout << crt << "Enter " << configuration._numberOfquestions + 1 << ". question: " << crt;
		getline(cin, configuration._questions[i]);
		configuration._numberOfquestions++;
		do {
			cout << crt << "Enter correct answer for question " << configuration._numberOfquestions << " (1 - CORRECT | 2 - INCORRECT): " << crt;
			cout << "Answer: " << flush;
			cin >> input;
			cin.ignore();
			answer = answers(--input);
		} while (answer != CORRECT && answer != INCORRECT);
		configuration._correctAnswers[i] = answer;
	}
	system("CLS");
}

int show_Results(Student obj, Test configuration) {
	int correctAns = 0;
	for (int i = 0; i < configuration._numberOfquestions; i++) {
		if (obj._answers[i] == configuration._correctAnswers[i])
			correctAns++;
	}
	return correctAns;
}

void run_Test(Student & obj, Test configuration) {
	system("CLS");
	cout << crt << " --- WELCOME TO THE EXAM ---" << crt;
	answers answer;
	int input;
	for (int i = 0; i < configuration._numberOfquestions; i++) {
		cout << crt << configuration._questions[i] << crt;
		do {
			cout << "1 - CORRECT | 2 - INCORRECT >> " << flush;
			cin >> input;
			answer = answers(--input);
		} while (answer != CORRECT && answer != INCORRECT);
		obj._answers[i] = answer;
	}
	system("CLS");
	obj._didTest = true;
}

int show_Students(Student *pok, int max) {
	system("CLS");
	int choice = 0;
	for (int i = 0; i < max; i++)
		cout << i + 1 << " -> " << pok[i]._nameSurname << endl;
	cout << crt;
	cout << "Enter student's ordinal number: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > max) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << crt << "[!] Enter valid ordinal number [!]: " << flush;
		cin >> choice;
	}
	system("CLS");
	return --choice;
}

void input_Students(Student & pok, Test &configuration) {
	int input = 0;
	cin.ignore();
	cout << crt << "\t\t:: STUDENT INPUT ::" << crt;
	cout << "Name and surname: " << flush;
	cin.getline(pok._nameSurname, 29);
	cout << "Index number: " << flush;
	cin >> pok._indexNumber;
	pok._didTest = false;
	do {
		cout << "Way of studying (1 - regular | 2 - DL): " << flush;
		cin >> input;
	} while (input < 1 || input > 2);
	pok._way = wayOf_Studying(--input);
	do {
		cout << "Study year (1 - first | 2 - second | 3 - third | 4 - fourth): " << flush;
		cin >> input;
	} while (input < 1 || input > 4);
	pok._year = study_Year(input);

	pok._answers = nullptr;

}

void show_Menu(int &choice) {
	cout << crt << "\t\t:: MENU :: " << crt << endl;
	cout << "\t1 - Run test" << endl;
	cout << "\t2 - Show results" << endl;
	cout << "\t3 - Search " << endl;
	cout << "\t4 - ADMIN MENU " << endl;
	cout << "\t5 - Create login" << endl;
	cout << "\t6 - END" << crt;
	cout << "Enter choice: " << flush;
	cin >> choice;
	while (!cin || choice < 1 || choice > 6) {
		cin.clear();
		cin.ignore(INT_MAX, '\n');
		cout << "[!] Enter valid choice [!]: " << flush;
		cin >> choice;
	}
}



Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:06 AM

When you say "I did debug" what exactly does that mean? Did you actually use a debugger? If so, did you watch the address of each student's answer array?
Was This Post Helpful? 1
  • +
  • -

#5 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:12 AM

View Postr.stiltskin, on 21 February 2018 - 10:06 AM, said:

When you say "I did debug" what exactly does that mean? Did you actually use a debugger? If so, did you watch the address of each student's answer array?


Yes I used debugger - that's what I meant :)
And yes, I checked the addresses - they are the same, that's the problem I guess but I don't know how to fix it since I haven't mastered the pointers yet, we've just started to learn about them :)
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:13 AM

The problem isn't pointers per se.

Where did you allocate memory for the answers array of each Student?
Was This Post Helpful? 1
  • +
  • -

#7 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:15 AM

View Postr.stiltskin, on 21 February 2018 - 10:13 AM, said:

The problem isn't pointers per se.

Where did you allocate memory for the answers array of each Student?


In function add_Questions
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:18 AM

Yes, I know. So that's where you should be looking. For example, what is this code doing?
	for (int i = 0; i < max; i++)
		obj[i]._answers = temp_Answers;

Was This Post Helpful? 1
  • +
  • -

#9 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:21 AM

View Postr.stiltskin, on 21 February 2018 - 10:18 AM, said:

Yes, I know. So that's where you should be looking. For example, what is this code doing?
	for (int i = 0; i < max; i++)
		obj[i]._answers = temp_Answers;


Oh is this the bug?
My intention was to set every student's answers to this new allocated array(?)?

Could you write it correctly please ? :)
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:21 AM

Or to put it another way, how many answers arrays have you actually allocated?
Was This Post Helpful? 1
  • +
  • -

#11 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:23 AM

View Postr.stiltskin, on 21 February 2018 - 10:21 AM, said:

Or to put it another way, how many answers arrays have you actually allocated?


Oh, I guess only 1 then ?
Should I implement array of an array then ? (pointer to pointer)
Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:27 AM

No - each student is a distinct object. Each one has its own distinct data members - _nameSurname, _indexNumber, etc. Each has its own answers pointer, so shouldn't that pointer point to ITS OWN array?

PS: If they all point to the same array (as you now have it) how can there be more than 1 set of answers?
Was This Post Helpful? 1
  • +
  • -

#13 WhiteroseX   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 13
  • Joined: 21-February 18

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:29 AM

View Postr.stiltskin, on 21 February 2018 - 10:27 AM, said:

No - each student is a distinct object. Each one has its own distinct data members - _nameSurname, _indexNumber, etc. Each has its own answers pointer, so shouldn't that pointer point to ITS OWN array?

PS: If they all point to the same array (as you now have it) how can there be more than 1 set of answers?


Ah, I see it now :)

I understood everything you said and I agree completely, except I don't know how to implement it, so if you would be so kind to help me out with that :)
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2032
  • View blog
  • Posts: 5,435
  • Joined: 27-December 05

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:34 AM

If you have written this program you should be able to figure out how to allocate its own memory to each of the answers pointers.
Was This Post Helpful? 2
  • +
  • -

#15 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Dynamic arrays and pointers problem

Posted 21 February 2018 - 10:41 AM

It also appears that you have created several memory leaks, make sure you delete[] what you new[].

Also mixing C-strings and C++ std::strings is a bad idea. You know about std::string I suggest you stick with the std::strings.

And also posted here.

Jim

This post has been edited by jimblumberg: 21 February 2018 - 10:42 AM

Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2