Skip to main content
GameDev.net gamedev.net
🔒 Locked

Computer Science Midterm...HELP

Started by sab3156 Jan 4, 2003 at 5:25 PM 60 replies 4.6k views
Original Post
sab3156
sab3156
Hey y''all. For my computer science class, we are writing a somewhat complex program that will illustrate our C++ programming skills. It''s to create school administration software. Now, I was wondering if anybody can give me some tips on how to allow the user to dynamically create a new student. I also need to allow the user to create his/her own login user name dynamically. Can anybody help?
Air-Conditioners are like computers. They stop working when you open windows.
j stoll
j stoll
check out list classes
sab3156
sab3156
well, we''re not supposed to be using object programming. i was thinking much simpler things like looping arrays or something like that. waddya think? got anything better than that?
Air-Conditioners are like computers. They stop working when you open windows.
LordElectro
LordElectro
STL STL STL STL STL STL STL STL STL STL STL STL STL

If they insist that you write your own dynamic allocation code, all you have to do is keep a pointer to your array of students, and resize the array when you need to add a new student. To resize the array, you can either create a new, larger spot in memory to copy it over to, or you can use realloc() (note: afaik, if you use realloc, you should be using malloc and free instead of new and delete).
BetaShare - Run Your Beta Right!
Big Sassy
Big Sassy
quote:
Original post by sab3156
well, we''re not supposed to be using object programming.
What do you think cout is? cout and cin are classes as well (well, instances of classes anyway). Look up the list class in STL. It would probably be a pretty good solution to your problem.

***********************
          
Chozo
Chozo
Why would they want to test your C++ skills, and then say you can''t use object programming? That seems... odd...
Shannon Barber
Shannon Barber
If the instructions were not to use any classes, then I would agree that it''s unreasonable. But it''s not unreasonable to say don''t use an object oriented design.

A controversial aspect of C++ is that you can use it for many different programming idioms. You can do procedural, modular, or object oriented. You can write perfectly structured code, or write no-so perferctly structured code (using return, continue, break, & goto as you see fit). You even mix in generic code (templates) and with a little teeth pulling you can actually do functional programing (as opposed to imperative) in C++ (e.g. meta-templates).

Using the STL isn''t using object-oriented code, it''s using generic code.


To dynamically create a new student, just do
Student* new_student = new Student

Where to keep that pointer is the interesting question.

#include <string>
#include <map>
struct Student
{
unsigned long id;
std::string first_name;
std::string middle_name;
std::string last_name;
//...other info
};

typedef std::map<unsigned long, Student> Student_Map
Student_Map student_database;

Now use the member functions of map to make new students, delete them, and look them up (use thier student ID as the key).
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
sab3156
sab3156
To dynamically create a new student, just do
Student* new_student = new Student

Where to keep that pointer is the interesting question.

#include <string>
#include <map>
struct Student
{
unsigned long id;
std::string first_name;
std::string middle_name;
std::string last_name;
//...other info
};

typedef std::map Student_Map
Student_Map student_database;

Now use the member functions of map to make new students, delete them, and look them up (use thier student ID as the key).

-----------------------------------------------------------

hmm... i understand everything except for the last part with typedef... can it be simpler than that? i don''t need a very sophisticated DB, just enough for a struct for each student or something. Plus, my instructor is a real big bi*ch, and she won''t let us use that anyway. (by the way, there is a rumor that she doesn''t even KNOW C++, and that she just keeps about 1 chapter ahead of the students in the book... i''m not lying...)

so now i know how to create the students...how would i send them to a text file for storage? and then what about deletion? can you show me a mini-program or something?
Air-Conditioners are like computers. They stop working when you open windows.
sab3156
sab3156
come on guys i really need help on this...
Air-Conditioners are like computers. They stop working when you open windows.
Krisc
Krisc
Ummm, I don''t do this too often but, it is your class, so do it your way, unless they say you can use outside sources like messageboards and such...

And I believe classes is a very big part of programming, especially games, so yell at them for not allowing that because if you created a student class and all and then made a vector of students you could easily resize the vector (vector.resize()) when needed and add new students!

<- Digital Explosions ->
"Discipline is my sword, faith is my shield
do not dive into uncertainty, and you may live to reap the rewards" - (Unreal Championship)
Promit
Promit
typedef std::map Student_Map

That line basically means that Student_Map is now the same thing as std::map.


It sounds to me like you are caught in one of those second rate high school C++ courses where a random math teacher was tricked into teaching teh course. Poor poor guy, I know what that can be like...
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
JoeZhang
JoeZhang
Cannot using object-based technology? well, here's a way of function-based to your case

#define MAX_NAME_LENG 25
typedef struct _STUDENT_INFO_
{
char szName[MAX_NAME_LENG+1];
UINT iSex;
UINT iAge;
UINT iClass;

//... something else goes here
} STUDENT_INFO, *PSTUDENT_INFO;

typedef struct _STUDENT_LIST_
{
PSTUDENT_INFO pList;
UINT uMaxCount;
UINT uCurCount;

//... something else goes here
} STUDENT_LIST, *PSTUDENT_LIST;

#define MAX_USER_NAME_LENG 25
#define MAX_PASSWORD_LENG 8
trpedef struct _LOGIN_USER_INFO_
{
char szUserName[MAX_USER_NAME_LENG+1];
char szPassword[MAX_PASSWORD_LENG+1];

//...something else goes here
} LOGIN_USER_INFO, *PLOGIN_USER_INFO;

typedef struct _LOGIN_LIST_
{
PLOGIN_USER_INFO pList;
UINT uMaxCount;
UINT uCurCount;

//...something else go here
} LOGIN_LIST, *PLOGIN_LIST;


STUDENT_LIST g_StuList;
bool g_SthListInited = false;

LOGIN_LIST g_LogList;
bool g_LogListInited = false;


int InitStudentList(UINT uMaxCount)
{
if (g_StuListInited)
return 0;

g_StuList.uMaxCount = uMaxCount;
g_StuList.uCurCount = 0;

g_StuList.pList = new STUDENT_INFO[uMaxCount];
if (!g_StuList.pList)
return -1;

g_StuListInited = ture;

return 0;
}

int AddNewStudent(PSTUDENT_INFO pInfo)
{
if (!pInfo || !g_ListInited)
return -1;

if (g_StuList.uMaxCount <= g_StuList.uCurCount)
{
return 1;
}

memset(g_StuList.pList[g_StuList.uCurCount],pInfo,sizeof(STUDENT_INFO));

++g_StuList.uCurCount;

return 0;
}

int InitLoginList(UINT uMaxCount)
{
if (g_LogListInited)
return 0;

g_LogList.uMaxCount = uMaxCount;
g_LogList.uCurCount = 0;


g_LogList.pList = new LOGIN_USER_INFO[uMaxCount];
if (!g_LogList.pList)
return -1;

g_LogListInited = ture;

return 0;
}

bool DoUserExisting(char *pszUserName)
{
UINT uTmp;

if (!pszUsername || !g_LogListInited)
{
return false;
}

for (uTmp=0; uTmp < g_LogList.uCurCount; ++uTmp)
{
if (stricmp(pszUserName,g_LogList.szUserName)==0)
{
return true;
}
}

return false;
}

int AddNewLoginUser(PLOGIN_USER_INFO pLUI)
{
if (!pLUI || !g_LogListInited)
{
return -1;
}

if (g_LogList.uMaxCount<=g_LogList.uCurCount)
{
return 1;
}

if ( DoUserExisting(pLUI->szUserName) )
{
return 2;
}
memset(g_LogList[g_LogList.uCurCount],pLUI,sizeof(LOGIN_USER_INFO));

++g_LogList.uCurCount;

return 0;
}

void FreeResource()
{
if (g_StuListinited)
{
delete [] g_StuList.pList;
g_StuListinited = false;
}

if (g_logListinited)
{
delete [] g_LogList.pList;
g_LogListinited = false;
}

}

int main()
{
//... other initilize stuff go here

InitStudentList(MAX_STUDENT_COUNT);
InitLoginList(MAX_LOGIN_USER_COUNT);

//while about to add a new student record...
STUDENT_INFO si;

//initialize the si from the standard input device from the user or a file...

AddNewStudent(&si);

//...


//while about to add a new login user
LOGIN_USER_INFO lui;

//initialize the lui from the standard input device from the user or a file..

AddNewLoginUser(&lui);

//...

FreeResource();


return 0;
}


Note: This is just a DEMO, itsn't a complete program, you should add the other stuff before compile it, such as error checking.

Also, for a real application, you should consider the following titles:

1) you may store the student list information and the login user list information into a disk file,and in the initialize part of the main program,reload them into memory from the file;

2)you also should consider the effect efficiency title while the number of the total students is a huge value;

3)On the above DEMO code section, the max number of the students and the login users are a fixed value, but for the real case, it's not a wise way, you may dynamicially expand it on demand.

Hope this helps!




[edited by - JoeZhang on January 7, 2003 11:41:23 PM]

[edited by - JoeZhang on January 7, 2003 11:43:18 PM]
Crispy
Crispy
quote:
Original post by Magmai Kai Holmlor
Student* new_student = new Student



Although I''m not very sure what this statement does, I''m pretty sure it won''t allocate the memory you''re asking. I''m sure it''s just a typo, though:

Student* new_student = new Student();

btw: what does allocing a new object without calling the actual constructor (Magmai''s code) do? I know it''s possible and that my bc5.02 doesn''t whine about it (allowing for a nasty error to debug), but why would anyone do that?

Crispy
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Miserable
Miserable
quote:
Original post by Crispy
Although I''m not very sure what this statement does, I''m pretty sure it won''t allocate the memory you''re asking. I''m sure it''s just a typo, though:

Student* new_student = new Student();

btw: what does allocing a new object without calling the actual constructor (Magmai''s code) do? I know it''s possible and that my bc5.02 doesn''t whine about it (allowing for a nasty error to debug), but why would anyone do that?


Are you by any chance a Java programmer? In Java, you have to use parentheses to indicate the constructor call; in C++, you do not.

What makes you think that the constructor is not called? The very point of a constructor is that it is implicitly called whenever you create an object - whether you use parentheses or not (you can, but I fail to see the point ...). Just run this through your favourite compiler:


  
#include <iostream>

class Bla
{
public:
Bla(void)
{
std::cout << "Bla!" << std::endl;
}
};

int main(void)
{
Bla *b = new Bla;
delete b;
}
DerekSaw
DerekSaw
... and they call it default constructor.
"after many years of singularity, i'm still searching n the event horizon" o
Miserable
Miserable
quote:
Original post by DerekSaw
... and they call it default constructor .

For some reason, I''ve developed an annoying tendency of making what I consider to be informative posts on topics that I know, but accidentally (and stupidly) omitting important details - yes; of course that''s the case; thank you.

To those who did not know: The implicitly called constructor is called the default constructor and takes no parameters; if you do not supply your class with any constructors, one will be created for you by the compiler, and this will be called when the class is instantiated. If you provide constructors, but not a default constructor, none will be automatically generated, and then the ''parenthesis-less'' code will not work (but then of course, the constructor must be supplied the proper parameters).

I''m almost afraid to ask what I forgot to say this time ...
SabreMan
SabreMan
quote:
Original post by Magmai Kai Holmlor
Using the STL isn''t using object-oriented code, it''s using generic code.

Please can you provide the definition of "object-oriented" you are using which leads you to make that statement.
Simagery
Simagery
"STL is not object oriented." - A. Stepanov, co-creator of the STL

Case closed. STL is generic programming, that''s different from OOP. In fact, the fathers of the STL would be very insulted by the fact that you think STL is OOP (which, to me, reveals an ignorance of sophisticated C++ use):

"I find OOP technically unsound ... philosophically unsound ... methodologically wrong ..." - A. Stepanov

Stepanov makes the excellent point that the algorithms are the directing force in software, not the data. He also discusses how this relates back to mathematics (rings, algebra, etc.) and makes an excellent point.

That being said, I still very much enjoy OOP myself. But, I do see the place for generic programming and *definitely* love the STL.
SabreMan
SabreMan
quote:
Original post by Simagery
STL is generic programming, that''s different from OOP.

I asked for the definition of OOP which makes that claim true. Despite what you seem to think, you haven''t provided it. If you tried thinking for yourself rather than shouting your head off about what Stepanov said and calling "case closed", you might realise you have in no way answered what I asked for.
quote:

In fact, the fathers of the STL would be very insulted by the fact that you think STL is OOP

How do you make any inference about what I think is the relationship between STL and OOP?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.