By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,474 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

instances of my student class help!!

P: n/a
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[i];

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

Thats the bases!! If anyone can help me pls email direct
ki********@hotmail.com or reply on the group!!!

I really really need help pls!!

Kirk.
Jul 22 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Kirk Bevins wrote:
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[i];


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" );

Jul 22 '05 #2

P: n/a

"Kirk Bevins" <ki********@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
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[i];
Reverse those:

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

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

Thats the bases!! If anyone can help me pls email direct
ki********@hotmail.com or reply on the group!!!

I really really need help pls!!

Kirk.

Jul 22 '05 #3

P: n/a
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" <al*****@hotmail.com> wrote in message news:<bthqs5> > .....void main() {

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


Reverse those:

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

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

Jul 22 '05 #4

P: n/a
Kirk Bevins writes:
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.
Jul 22 '05 #5

P: n/a
Kirk Bevins wrote:
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

Jul 22 '05 #6

P: n/a
Peter Johansson wrote:


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];
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7

P: n/a
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
Jul 22 '05 #8

P: n/a
Kirk Bevins writes:
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.
Jul 22 '05 #9

P: n/a
In article <1d**************************@posting.google.com >, Kirk Bevins wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #10

P: n/a
In article <1d**************************@posting.google.com >, Kirk Bevins wrote:
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,
--
Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/
Jul 22 '05 #11

P: n/a
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.
Jul 22 '05 #12

P: n/a
ki********@hotmail.com (Kirk Bevins) wrote:
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 );
Jul 22 '05 #13

P: n/a
Karl Heinz Buchegger wrote:
Peter Johansson wrote:

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 ;)
Jul 22 '05 #14

P: n/a
Kirk Bevins wrote:
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

Jul 22 '05 #15

P: n/a
Kirk Bevins wrote:

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 ...

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #16

P: n/a
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.
Jul 22 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.