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! |
What have you already tried, if anything? Do you have any code you can post?
|
Quote:
|
Quote:
Quote:
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:
"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:
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:
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". |
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 09:52 PM. |