I need help with my C++ program

FFS, you guys still do not get it. I am required to

  • Create a C++ project with the following files: degree.h, student.h and student.cpp, roster.h and roster.cpp, and main.cpp for a total of six source code files.

  • In degree.h I must define an enumerated data type, DegreeProgram containing the values: SECURITY, NETWORK, and SOFTWARE.

  • For the Student class, I must include the following variables: student ID, first name, last name, email address, age, array of number of days to complete each course, and degree program. The Student class must also include accessors and mutators for each variable. Additionally, the Student class must contain a constructor and a print() function.

  • For the Roster class, I must include an array of pointers, named classRosterArray to hold the data provided in the ā€œstudentData Table.ā€ Then I must create a student object for each student in the data table and populate classRosterArray. Next I have to define, specifically, each method of the roster class: public void add(string, string, string, string, int, int, int, int, DegreeProgram); public void remove(string studentID); public void printAll(); public void printAverageDaysInCourse(string studentID); public void printInvalidEmails(); and public void printByDegreeProgram(DegreeProgram). Each one of these functions has specific requirements telling me what it should do.

  • Finally, I have to ā€œdemonstrate the programā€™s required functionality by adding a main() function in main.cpp which will contain the required function calls to achieve the following results:ā€ print out to the screen the course title, the programming language used, my WGU student ID, and my name; create an instance of the Roster class called classRoster; add each student to classRoster; and convert specified pseudo code to complete the rest of main.

Iā€™ve been trying to tell you guys that the program instructions are very explicit. But yā€™all wonā€™t listen. I am trying to tell you guys that I agree with you that it is hella unreadable and messy as hell. But I canā€™t make it better because the code structure is made by idiots at the university.

1 Like

Thanks. That compiles now I can use GDB to debug it.

2 Likes

Well, if you donā€™t understand how to do it whilst meeting the requirements, make it work the simple way first, then tweak to fit the required solution?

4 Likes

I understand this. What you do not seem to grasp here, is that only the end result needs to fulfill your criteria.

Instead of trying to build a car from the get go:

  1. Start with a wheel.
  2. Make that wheel functional.
  3. Make the engine and get that working.
  4. Create steering.
  5. Build the chassi.
  6. Put it all together one piece at a time.

Same principles apply to software, start simple, expand later. :slightly_smiling_face:

4 Likes

Yeah, basically, start with the basic building blocks, build test units to verify that those individual parts work as intended. Then construct he main method to drive those individual parts to do what you want them to do.

I do see the frustration here, but I am seeing some things that would have been caught at the lower levels with some code testing and validation testing.

2 Likes

Well you know, you could have just said this instead of coming off as a complete and utter asshole.

Also, I did build the printInvalidEmails() method by itself to test it. That is because it was the first time Iā€™d ever used the regex library. (Itā€™s in the standard library so I guess thatā€™s okay for me to use).

Reading from the top:

  • references to avoid copying: ā€¦ good idea,

const references are what folks in industry would frequently use when thereā€™s no need to modify stuff, ā€¦ or if thereā€™s a need to modify things, theyā€™d put them on the heap using either a std::shared_ptr or std::unique_ptr

The latter requires the caller to give up ownership of the reference to the class instance by doing std::move(ā€¦) at the place of invocation.

By that same convention, if a function takes a raw pointer type of argument, it is implied that it uses a thing but does not take ownership of the thing. (If you come from unique_ptr school of thought / half of the industry).


constructors vs factory methods/functions.

factory methods are more versatile, they can return a unique_ptr or shared_ptr to an instanceā€¦ or they can populate a unique_ptr argument while returning some status enum, or do other kinds of things. (e.g. look at absl::statusor<> which is seeping into c++ standards as std::expected)

Certain parts of the industry are still not using exceptions as means of propagating failures, so factory methods are a neat workaround that allows one to communicate success/failure during instance creation, as well as to communicate the actually created instance.

With constructors you have a to return a usable instance, or throw an exceptionā€¦ or create a useless instance and then do fail-prone initialization in a separate method, factory methods give you another option here.


Re your input.

Itā€™s basically lines from a .csv file. In c++, you can use getline to read until a delimiter.

So essentially, you could define a explicit Student::Student(const string input_line&) perhaps, and roster.add could call it.

and inside, you could make a stringstream and do first=getline(ā€¦) ; last=getline(ā€¦); email=getline(ā€¦) etcā€¦

1 Like

You know who looks like a complete and utter asshole in this thread?

TIP: attacking people offering their assistance for YOUR PROBLEM isnā€™t a great look

Also, thanks for this part. You do sound harsh, so maybe next time be a little less brazen. I was able to fix student.cpp although some of my fixes havenā€™t made it to github yet.

I apologize for getting defensive, and thus coming off as an asshole to you. But I still feel that you guys could have taken a better approach; rather than attacking my code that I couldnā€™t do anything about. Or you could have just said what @Mastic_Warrior said in his first post here. I think I underestimated the complexity of this program because of how stupid it is. Normally, I do not have problems writing programs all at once for school, as well. Plus I donā€™t care about this program that much. Itā€™s useless and stupid. Its only purpose is to get me to pass the class.

Passing the strings in by reference were causing me the issue. Idk why, but the program couldnā€™t assign the reference variables to the value of the class variable like I wanted. It may have been syntax on my part, but it was unable to access the referenceā€™s location in memory. So instead, I am using std::move to move the strings around without copying them. Also, I donā€™t understand what you are saying to do in that last part. My student constructor does have to take in specific parameters, so that wonā€™t meet the requirements. That may be a better way to engineer this, but the university obviously doesnā€™t care about giving the students the freedom to write a better program than they can.

This sounds like a problem with scoping. Always declare referenced parameters as const unless you explicitly mean to change them around. Also, always declare the reference in the function declaration/definition only, that is:

void myFunc(const MyType &a, const MyType &b) {
    a.print();
    b.print();
}

...

MyType c();
MyType d();
myFunc(c, d);

Yes, I understand this particular exercise is frustrating for you, but it is designed to make you actually think about scoping, constructors and datatypes, to catch those students that just find every answer on StackOverflow and then copy-paste the solution. Keep at it, youā€™ll get there eventually. :slight_smile:

If you still have trouble this weekend, Iā€™ll see if I can write a similar program that does everything this one is supposed to do, but with a different setting so you cannot directly copy it. Fair enough?

2 Likes

I donā€™t want you to do it for me. And, do you remember what my code looked like before I changed it? The reference parameters were all declared as constants. And the exercise isnā€™t so much as frustrating as seemingly pointless. I do not like it when assignments given to me tell me how to do the program either. To be fair, Iā€™ve made a ton of progress so I am fairly confident that I can get it figured out now.

1 Like

Fair enough, I didnā€™t say Iā€™d do your work just give you a similar example as a reference. Great to hear youā€™re making progress!

C++ is by far the most complex programming language I have ever encountered, so thereā€™s no shame if you get lost with it. It allows for the implementation of pretty much every anti-pattern that exists in the programming world, so lots of foot-shooting in that one even for experienced devs.

2 Likes

As above, it is a learning exercise. There will definitely be a reason you were told to solve this in a specific way.

It may be a lead up to a follow on of ā€œhow to do this differentlyā€ or ā€œa better wayā€.

I mean in reality you wouldnā€™t be using C++ for this particular problem at all; but thereā€™s only one way to learn a language and thatā€™s by doing and seeing what does/doesnā€™t work and how to shoot yourself in the foot (whilst learning) so you donā€™t do it in production.

Yeah, I rarely use C++ as I tend to write C with Objects so I just use C if I need to. I am not a fan of C++ but the standard is starting to get more sane. I have seen some things in the past that has put me off on the language.

2 Likes

I prefer C and Python myself. I wouldnā€™t be using this language if it werenā€™t required. Also, C was my first language - unless you count TI Basic.

1 Like

I doubt it. As soon as I submit this program with a passing grade, I am done with the class.

I have a feeling that my issue here has something to do with the fact that each index is a pointer to a ā€œnew Studentā€ variable rather than being a nice regular variable. In the function,

void Roster::remove(const string& studentID)
{