instances of my student class help!!

K

Kirk Bevins

Hello, new to posting, got a dilema in c++. I cant seem to create new
instances of my student class. The idea is to make a database where
the user inputs surnames and library card numbers etc. The piece of
code I cant get to compile is:

......void main() {

for (int i=0; i<10; i++) {
student s;

cout << "Enter student surname: " << endl;
cin >> c;
s.setSurname(c);
}
}

Thats the bases!! If anyone can help me pls email direct
(e-mail address removed) or reply on the group!!!

I really really need help pls!!

Kirk.
 
H

Howard

Kirk Bevins said:
Hello, new to posting, got a dilema in c++. I cant seem to create new
instances of my student class. The idea is to make a database where
the user inputs surnames and library card numbers etc. The piece of
code I cant get to compile is:

.....void main() {

for (int i=0; i<10; i++) {
student s;


Reverse those:

student s[10];
for (int i = 0; i < 10; ++i) {
cout << "Enter student surname: " << endl;
cin >> c;
s.setSurname(c);
}
}

Thats the bases!! If anyone can help me pls email direct
(e-mail address removed) or reply on the group!!!

I really really need help pls!!

Kirk.
 
A

Aggro

Kirk said:
Hello, new to posting, got a dilema in c++. I cant seem to create new
instances of my student class. The idea is to make a database where
the user inputs surnames and library card numbers etc. The piece of
code I cant get to compile is:

.....void main() {

int main()

main() should always return int, anything else might cause unspecified
results.
for (int i=0; i<10; i++) {
student s;


What exactly are you trying to do here? Perhaps you would like to have
something like (assuming student is the class name):

std::vector<student> students; // You need #include <vector>

for( unsigned int i = 0; i < 10; i++ )
{
student s; // Add contructor parameters if needed.
students.push_back( s )
}

s[0].setSurname( "whatever" );
 
K

Kirk Bevins

Thanx for the help!! I seem to be useless at c++ even tho I need to do
it!

My next problem is I cant respond to user input to create an amount of
student instances:

I have:

cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];

for (int i=0; i<f; i++) {

Can't seem to compile! :(

Would appreciate any help pls.






Howard said:
for (int i=0; i<10; i++) {
student s;


Reverse those:

student s[10];
for (int i = 0; i < 10; ++i) {
cout << "Enter student surname: " << endl;
cin >> c;
s.setSurname(c);
}
 
P

Peter Johansson

Kirk said:
Thanx for the help!! I seem to be useless at c++ even tho I need to do
it!

My next problem is I cant respond to user input to create an amount of
student instances:

I have:

cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];

for (int i=0; i<f; i++) {

Can't seem to compile! :(

Would appreciate any help pls.

I suppose your compiler is complaining about the fact the f is not
constant (needs to be when declaring student student[f]) and the
variable name should be different than the class name.
Hence, you need to allocate memory dynamically by use of new (don't
forget to use delete with [] later to ensure that the destructor of the
student class gets run). Anyhow, it seems to me that this is not where
you are in your course right now (been teaching this a bit myself a
while ago).
So, why not just declare an array of student of size 100 (since your max
input anyhow is 100) like:

Alternative 1 (no dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student theStudent[100];
if(f < 100)
{
for (int i=0; i<f; i++) {
theStudent[f].setSurname(...);
}// for
}// if
else
{
cout << "The input value is too large" << endl;
}

Alternative 2 (dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student* theStudent = student[f];
if(f < 100)
{
for (int i=0; i<f; i++) {
theStudent[f].setSurname(...);
}// for
}// if
else
{
cout << "The input value is too large" << endl;
}
// Make sure that the destructor of each student instance gets run.
delete [] theStudent;

/ Peter
 
O

osmium

Kirk said:
Thanx for the help!! I seem to be useless at c++ even tho I need to do
it!

My next problem is I cant respond to user input to create an amount of
student instances:

I have:

cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];

for (int i=0; i<f; i++) {

Can't seem to compile! :(

Post more code; you haven't posted enough so that we can be helpful. The
problem appears to be quite small so perhaps you could post the whole thing.
You also might want to identify the line that causes the first error.
 
K

Karl Heinz Buchegger

Peter said:
Alternative 2 (dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student* theStudent = student[f];

typo:

student* theStudent = new student[f];
 
K

Kirk Bevins

Hello, I have sorted this problem now with thanx to the posters!! I
nearly had the solution just had a few teething problems!!!

Im thinking about using a bubble sort now to sort the data in the
array alphabetically. Is this easily implemented? and ideal?

Kirk
 
D

Donovan Rebbechi

cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];
^^^^^^^^^^^^^^^

This code is incorrect. Is student
(a) a data type, or
(b) the name of an array ?

You can't use the same name for both of those things.

Cheers,
 
D

Donovan Rebbechi

Hello, I have sorted this problem now with thanx to the posters!! I
nearly had the solution just had a few teething problems!!!

Im thinking about using a bubble sort now to sort the data in the
array alphabetically. Is this easily implemented? and ideal?

Whether or not it's "ideal" depends on your goals. If learning C++ and
learning a bit about algorithms is a key goal, then it's a perfectly
good way to do it. But it's not the most efficient in terms of programmer
time or program efficiency. The alternative choices will be obvious as
you learn the standard library, but for now I'd recommend coding the bubble
sort.

Cheers,
 
K

Kirk Bevins

Ok thanx for the help here guys ive finally cracked it!!!!

My next problem is implementing a bubble sort:

if (sortYesNo==1) {
int L, L2 = e;
while( L2 > 1 ) {
L = -1;
for(int p = 1; p < L2; p++) {
if( student[p - 1] > student[p] ) {
(student[p - 1]).swap(student[p]);
L = p;
}
}
L2 = L;
}
}

the .swap doesnt seem to want to work ahhhhhhhhhhhhhh!!!!

Kirk.
 
O

osmium

Kirk said:
Hello, I have sorted this problem now with thanx to the posters!! I
nearly had the solution just had a few teething problems!!!

Im thinking about using a bubble sort now to sort the data in the
array alphabetically. Is this easily implemented? and ideal?

The bubble sort is fine for a few items. It will give you a sense of
accomplishment and confidence to go forward with perhaps more difficult
sorts in the future. After you get it working you will look back and see
that it was relatively easy.
 
D

Daniel T.

Thanx for the help!! I seem to be useless at c++ even tho I need to do
it!

My next problem is I cant respond to user input to create an amount of
student instances:

I have:

cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student student[f];

for (int i=0; i<f; i++) {

Can't seem to compile! :(

Would appreciate any help pls.

Try:

#include <vector>

....

unsigned int f;
cin >> f;
std::vector<student> myStudents( f );
 
P

Peter Johansson

Karl said:
Peter said:
Alternative 2 (dynamic memory allocation):
cout << "How many students would you like to add? Enter a number under
100" << endl;
cin >> f;
student* theStudent = student[f];


typo:

student* theStudent = new student[f];
Oh crap, I could not compile it in Netscape mail ;)
 
P

Peter Johansson

Kirk said:
Ok thanx for the help here guys ive finally cracked it!!!!

My next problem is implementing a bubble sort:

if (sortYesNo==1) {
int L, L2 = e;
while( L2 > 1 ) {
L = -1;
for(int p = 1; p < L2; p++) {
if( student[p - 1] > student[p] ) {
(student[p - 1]).swap(student[p]);
L = p;
}
}
L2 = L;
}
}

the .swap doesnt seem to want to work ahhhhhhhhhhhhhh!!!!

Kirk.
Guess you need to post your code for the swap routine then.

/ PJ
 
K

Karl Heinz Buchegger

Kirk said:
Ok thanx for the help here guys ive finally cracked it!!!!

My next problem is implementing a bubble sort:

if (sortYesNo==1) {
int L, L2 = e;
while( L2 > 1 ) {
L = -1;
for(int p = 1; p < L2; p++) {
if( student[p - 1] > student[p] ) {
(student[p - 1]).swap(student[p]);
L = p;
}
}
L2 = L;
}
}

the .swap doesnt seem to want to work ahhhhhhhhhhhhhh!!!!

Suggestion:
Don't post a message saying: 'Doesn't work'.

Post: I have a problem with an error message from the compiler which I cannot
sort out. This is the message ... and this is the line the compiler flags ....

Post: This function doesn't do what I think it should do. It should ...., but
it does ...
 
K

Kirk Bevins

Hello, thankyou to EVERYONE who has given advice on helping me solve
the problem!

The full code for my program can be found at

www.themanstu.4t.com

and then the link on the left.

The sorting is becoming quite tricky, its bound to be just a small
problem like forgetting a ; or something! If someone can point me in
the right direction and not fully help, I would appreciate it!

Kirk over and out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top