"Define a class Student that has as its members the student_name and a student_idnumber both of type string.

Input from the keyboard the number of students in the database, then create a dynamic array of type Student and input the information for each student.

Once you populate the database (stored all information), display the entire database. Remember to delete the dynamic array pointer before exiting the program."


please forgive my english its so not good :( but i am need help with this problem. i have define class 'student' correctly i believe but the rest i feel i dont understand to do

how to define dynamic array? and the rest? 8-) appreciate much help thank you

#include "stdafx.h"
#include <iostream>

class student
{
public:
	student();
 	void setid(string a);
 	void setname(string b);
 	string getid();
 	string getname();
private:
 	string id;
 	string name;
};

Recommended Answers

All 19 Replies

cout << "input number of students";
int numStudents;
cint >> numStudents;
student * students = new students(numStudents);

Be sure to delete any memory you declare dynamically using the delete[] operator. In this case, when you are done with the students array, delete using:

delete[] students;

First of all you need a pointer to declare a dynamic array ...
Let's say we want a dynamic array which consists of integers:

int *ptr_to_array;

If you want a dynamic array of type 'double', you'll have to declare a pointer of that type instead of 'int', it's analog for the other types ...
In your case you might want to declare a pointer of type 'student', as it was in your assignment ...

After that we can dynamically assign memory to the pointer by using new[] :

ptr_to_array = new int[number_of_elements];

(where number_of_elements needs to be replaced by the number of elements you want to store into the array (duh;)))

When you don't need the dynamic array anymore you shouldn't forget to release the assigned memory by using delete[] :

delete[] ptr_to_array;
/* This will not delete the pointer, only the memory assigned to it... */

Hope this helps !!!

student * students = new students(numStudents);

Has to be: student * students = new students[numStudents]; with [] brackets instead of the () ones ...

Yup, typo, wrong set of brackets, sorry.

says...error C2061 syntax error identifier 'students'

-_-
thank you for quick responses very helpful :-D

Edit:: (Try 'student' instead of 'students' ==> probably won't work ...)

Can you please post the whole code you're trying to compile ?

thank you for looking at it =_= i am so bad c++

#include "stdafx.h"
#include <iostream>

using namespace std;

class student
{
public:
	student();
 	void setid(string a);
 	void setname(string b);
 	string getid();
 	string getname();
private:
 	string id;
 	string name;
};

int main ()
{
	cout << "input number of students";
	int numStudents;
	cin >> numStudents;
	student * students = new students[numStudents];

	delete[] students; 

	system ("PAUSE");
	return 0;
}

type var_name = new type[n]; not type var_name = new var_name [n]; You need to establish a better naming system that allows you to recognize what is what, with just a glance.

sorry
i dont understand?

Where you allocate your pointer, notice something wrong with new's "type"?

By the way, you should be checking if you allocation was successful or not!

int *p;

try{
  p = new int [5];
  if(!p) throw; }
catch(...) {
  emergency_handling(); }

Look at the comments in the code to see what I've changed ...

#include <iostream>
#include <string> /* In your class you've functions which are returning string values */

using namespace std;

class student
{
public:
	student();
 	void setid(string a);
 	void setname(string b);
 	string getid();
 	string getname();
private:
 	string id;
 	string name;
};

/* IF YOU DEFINE A CONSTRUCTOR, YOU ALSO HAVE TO CREATE HIM */
student::student()
{
	/* Constructor */
}

int main()
{
	cout << "input number of students";
	int numStudents;
	cin >> numStudents;
	student * students = new student[numStudents];
        /* YOURS WAS: student * students = new students[numStudents]; */

	delete[] students; 

	system ("PAUSE");
	return 0;
}

Where you allocate your pointer, notice something wrong with new's "type"?

By the way, you should be checking if you allocation was successful or not!

int *p;

try{
  p = new int [5];
  if(!p) throw; }
catch(...) {
  emergency_handling(); }

You could also check it by using the following code:

if(p == NULL)
{
    /* The memory wasn't allocated */
}

But MosaicFuneral's code is superior if you want to use error-handling ...

You could also check it by using the following code:

if(p == NULL)
{
    /* The memory wasn't allocated */
}

if(p == NULL) is if(!p)

Oh, I didn't notice it but you're absolutely right ...

so then i need to display umm

"Define a class Student that has as its members the student_name and a student_ idnumber both of type string. Input from the keyboard the number of students in the database, then create a dynamic array of type Student and input the information for each student. Once you populate the database (stored all information), display the entire database. "

need to input ID and display? how to?

need to input ID and display? how to?

This is an example of a 'dog' class I wrote:

#include <iostream>
#include <string>

using namespace std;

class dog {
	string name;
	int age;
public:
	string get_name() {return name;}
	int get_age() {return age;}
	dog(string his_name);
	dog(string his_name, int his_age);
	dog(){};
	void set_name(string his_name) {name = his_name;}
	void set_age(int his_age) {age = his_age;}
	void disp_details();
};

dog::dog(string his_name)
{
	name = his_name;
}

dog::dog(string his_name, int his_age)
{
	name = his_name;
	age = his_age;
}

void dog::disp_details()
{
	cout << "Name: " << name << endl;
	cout << "Age: " << age << endl;
}

int main()
{
	int number_of_dogs = 0;
	string name;
	int age = 0;
	cout << "Please specify the number of available dogs in the shop: ";
	cin >> number_of_dogs;
	dog * doggies = new dog[number_of_dogs];
	
	/* Create a dog database */
	cout << endl << "DOG-DATABASE INPUT" << endl;
	cout << "~~~~~~~~~~~~~~~~~~" << endl << endl;
	for(int i = 0; i < number_of_dogs; i++)
	{
		cout << "Please type dog number " << i+1 << "'s name: ";
		cin >> name;
		doggies[i].set_name(name);
		cout << "Please type dog number " << i+1 << "'s age: ";
		cin >> age;
		doggies[i].set_age(age);
		cout << endl;
	}
	
	/* Disp dog database */
	cout << endl << "DOG-DATABASE DISPLAY" << endl;
	cout << "~~~~~~~~~~~~~~~~~~~~" << endl << endl;
	for(int i = 0; i < number_of_dogs; i++)
	{
		doggies[i].disp_details();
		cout << endl;
	}
	
	/* Delete our dog database */
	delete[] doggies;

	return 0;
}

If you give it a good look you'll understand how you can implement this ...

Wouldnt it be much more sensible to make a linked list

Wouldnt it be much more sensible to make a linked list

That's also an option, but:

"Define a class Student that ...

Yeah that would work fine.

E.g have Student as the list node , and StudentClass as the linked list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.