LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   String arrays C programming in Linux (https://www.linuxquestions.org/questions/programming-9/string-arrays-c-programming-in-linux-4175448422/)

merlin704 02-02-2013 11:16 PM

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!

everest40 02-03-2013 12:54 AM

What have you already tried, if anything? Do you have any code you can post?

millgates 02-03-2013 09:06 AM

Quote:

Originally Posted by merlin704 (Post 4883397)
Implement a class Student
...
information should be kept private

Are you sure it's supposed to be C and not e.g. C++?

johnsfine 02-03-2013 09:44 AM

Quote:

Originally Posted by merlin704 (Post 4883397)
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 (Post 4883397)
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 (Post 4883397)
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".

merlin704 02-04-2013 03:46 PM

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!


All times are GMT -5. The time now is 05:44 PM.