473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

instances of my student class help!!

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
16 2581
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

"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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Krzysztof Stachlewski | last post by:
I tried to run the following piece of code: Python 2.3.4 (#53, May 25 2004, 21:17:02) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> o = object() >>> o.a...
6
by: Ivan Shevanski | last post by:
To continue with my previous problems, now I'm trying out classes. But I have a problem (which I bet is easily solveable) that I really don't get. The numerous tutorials I've looked at just...
1
by: Chinese student(Adam Zhang) | last post by:
I'm a Chinese student I found a very confusable proble when I studied Visual C++ 6.0 The problem is #include <iostream.h class confusable private int x,y
9
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:817840 hi, I was wondering how to do the following. I have a class A, class A{ //; int a;
2
by: ramakanth | last post by:
Hi, I would like to design a Student Class containing the private members namely Char *Name,int Age and Char Sex. The constructor of the class need to be passed with only one String argument.....
3
by: sandy | last post by:
I am a student who is losing his mind. The code below is a header file which has the line: void ClearList(ListType *list); which generates the following compile time errors: variable or...
11
by: xxbabysue123xx | last post by:
Heres the problem: Create a class Student with instance data name, studentNumber, class (where class is a String containing one of the following: “Freshman”, “Sophomore”, “Junior”, “Senior”. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.