473,657 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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********@hotm ail.com or reply on the group!!!

I really really need help pls!!

Kirk.
Jul 22 '05 #1
16 2611
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<stu dent> students; // You need #include <vector>

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

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

Jul 22 '05 #2

"Kirk Bevins" <ki********@hot mail.com> wrote in message
news:1d******** *************** ***@posting.goo gle.com...
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********@hotm ail.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*****@hotmai l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1756
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 = 5 Traceback (most recent call last): File "<stdin>", line 1, in ? AttributeError: 'object' object has no attribute 'a'
6
1465
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 confsed me.For intance: >>>class Xyz: .... def y(self): .... q = 2 .... >>>Xyz.y() Traceback (most recent call last):
1
291
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
1788
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
3194
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.. The String passed is in the format like Student S("David 18 M") each is separated by a single space. Now the private members of the class need to be filled with corresponding values....
3
34458
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 field `ClearList' declared void expected `;' before '(' token
11
4052
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”. Make the class implement the Comparable interface. Include a toString method. Write a driver program to demonstrate your work. Instantiate several objects of the Student class. Call your methods several times to show that class is ordered. ...
0
8392
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6163
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.