LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-03-2013, 12:16 AM   #1
merlin704
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Rep: Reputation: Disabled
String arrays C programming in Linux


I am having trouble with this problem. I have programmed C but not in Linux. I just need some help getting started. Here is the problem:

Implement a class Student containing the following information. Keep in mind that some of the following assignments will make use of this class.

First name
Last name
Student id number
Academic classification (freshman, sophomore, ...)
Major
Remember that information should be kept private if possible. Create an array that can store students. Make it sufficiently large, e.g. 20. Once the program is started, it will print out the prompt "students> " (> is followed by a whitespace):

./a.out

students>

You will implement the commands "add", "remove", "print" and "quit":



add

Add takes 5 arguments, representing the individual attributes in the order listed above. Store the new student in the next available array spot. If a student id already exists, print out an error message starting with "Error!" Then repeat the prompt. You can assume that there are no spaces within the attribute values specified by the user.

students> add James Bond 7 Sophomore Dance
students> add Bugs Bunny 3229 Junior Business
students> add Donald Duck 230 Junior ComputerScience
students>



If i can get some help on the "add" implementation, hopefully i can figure out remove, print and quit by myself. Thanks!
 
Old 02-03-2013, 01:54 AM   #2
everest40
Member
 
Registered: Jul 2008
Distribution: Ubuntu $LATESTVERSION
Posts: 164

Rep: Reputation: 66
What have you already tried, if anything? Do you have any code you can post?
 
Old 02-03-2013, 10:06 AM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by merlin704 View Post
Implement a class Student
...
information should be kept private
Are you sure it's supposed to be C and not e.g. C++?
 
Old 02-03-2013, 10:44 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,139

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by merlin704 View Post
I am having trouble with this problem.
See the answer posted by everest40.

Quote:
I have programmed C but not in Linux.
Nothing about this problem should make it different in Linux than in some other OS.
But it should be very different in C++ than in C. See the answer posted by millgates for the two items in the problem description that make sense only if it is C++, not C.

Quote:
Create an array that can store students. Make it sufficiently large, e.g. 20.
It appears the C vs. C++ confusion is at least partially your instructor's fault.

"Make it sufficiently large, e.g. 20" is the sort of thing one might say for a trivial beginner example of using arrays in C. (Even then, it is a bad practice that should be dropped as soon as possible).

For a beginner in C++, I strongly believe it is better to introduce std::vector before introducing arrays. You don't need to really understand std::vector to make simple use of it. Later you need to know about arrays as well and you might want to know that when the final size is know at the moment of allocation an array may be better than a std::vector.

In fact, you have described a problem in which std::map is even better than std::vector, but I can understand not telling you about std::map yet.

If this assignment is really in C, or you are stuck with your instructor's bad idea of using an array instead of std::vector, an array is only slightly worse for now.

In any case, make sure you understand you need an array of the class objects, not a string array as your thread title suggests.
Each object of the class you are defining has some members that are strings. Then the program using that class has an array of objects of that class.

Quote:
Originally Posted by merlin704 View Post
If a student id already exists, print out an error message
Learn right away to make a helper function wherever similar requirements exist in multiple places in the overall problem.

If you choose (or are forced to use) an array or std::vector for a data structure that ought to be a std::map, then you need a function to search for a student id and find out where it is in the array or if it is not there. Your remove command will need to use that function. The error check in the add command will need that same function.

Quote:
Originally Posted by merlin704 View Post
If i can get some help on the "add" implementation, hopefully i can figure out remove, print and quit by myself.
The essence of programming is dividing up big tasks into small enough tasks that they are individually obvious. When you look at "add", "remove" etc. as your tasks, you are either no dividing correctly or not dividing enough (because you don't know how to do add).

1) You need to decide how to store each field of the class. In C++, the easy way to store text is std::string. If you are using C or you aren't permitted std::string, then you must use char* which makes you responsible for some storage allocation complications.
1a) If you are stuck with an array, rather than std::vector or std::map then you need an extra variable to keep track of how much of the array is used at the moment. That is an extra detail to keep track of, but it obviously isn't difficult.

2) Input of a command always takes some thought/effort. How do you want to take a line of input and split it into the command word and the arguments of that command? If the user is perfect, then scanf can do that job well in C and the stream operator << can do it even more easily in C++. But if the user isn't perfect, you need some extra effort to make sure you retry only on line boundaries of the user's input.

3) Once you have done (1) and (2) (and the helper function described earlier) the core task of "add" is copying the five fields into a position in the array. But that is trivial even for a beginner. So not knowing how to do "add" meant you haven't yet done the basics you need before working on "add".

Last edited by johnsfine; 02-03-2013 at 11:13 AM.
 
2 members found this post helpful.
Old 02-04-2013, 04:46 PM   #5
merlin704
LQ Newbie
 
Registered: Feb 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for the help! I figured it out finally. My teacher made a mistake and it was supposed to be coded in C++. Thanks again! I will definitely be putting this forum to use in the future. Our next assignment is the traveling sales man program! Wish me luck!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Question with c++,string and arrays? peacepanda Programming 3 10-16-2012 05:52 AM
[SOLVED] I got a question about inserting char arrays into a string... trist007 Programming 1 04-20-2011 07:21 PM
Structures and String Arrays, C SithArrow Programming 4 04-18-2011 11:32 PM
[SOLVED] Accessing arrays position in a split string xeon123 Linux - Newbie 4 03-10-2011 10:36 AM


All times are GMT -5. The time now is 09:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration