Vectors, Structs, and either not setting or retrieving string variable

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1674 Views - Last Post: 25 April 2018 - 07:23 AM Rate Topic: -----

#31 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 24 April 2018 - 05:20 PM

Quote

Then you're either doing something wrong or the compiler you're using is a pre-standard piece of crap. Even if you use the c_str() function you should not need to use that horrible cast, even in C++98.


It definitely doesn't function normally without that syntax. We use g++, but maybe it's an old version? Anyway, it's really strict. If it doesn't compile for the TA/Professor (whoever grades), they don't even attempt to fix it, and you get a 0. So, for the sake of this assignment, I'm not going to mess around with the cast, no matter how terrible it is, because it's what he specifically put. :( But I'll end up going back after the class is over and see what I need to do so it does compile fine.


Quote

What is part of the requirements, outputting an error message or ignoring the problem?


That's a valid question. I would say the requirement is outputting an error and not necessarily ignoring the problem. I changed the message to better fit the issue, so now it's atleast "Error trying to open <filename>". So it's more clear where the issue lies. The issue with alot of these courses is if we aren't taught about a topic, and then randomly add it in our code, we can/will be accused of cheating, which is really dumb. This professor seems alot more lax, so honestly, I'll just email him and see if throwing exceptions will cause me to lose points.

Quote

But when inside the class you can only ever use the structure inside the class.


I swear there is a method to that madness. I wouldn't usually do that for something that is versatile like the Employee struct and could be used elsewhere. But we are asked to "secure" this data from those without the proper security level. So, the idea of mine was that putting the Employee struct in EmployeeManager means that the outside classes (like the System class that runs the other parts of the program) can only change Employee information by using the EmployeeManager functions, which limit exactly what can be changed (and the System class checks if an employee is an admin before allowing them to access admin functions like adding or removing an employee).
Was This Post Helpful? 0
  • +
  • -

#32 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,063
  • Joined: 25-December 09

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 24 April 2018 - 09:54 PM

Quote

But we are asked to "secure" this data from those without the proper security level.

You do know that you have a plain text file that contains this information, right? So anyone who has access to the computer is free to look at that information no matter what your program does.

Quote

I'll just email him and see if throwing exceptions will cause me to lose points.

You may want to question that cast as well. And ask him exactly why it is needed since "Modern C++" doesn't require such an abomination, much less the use of the c_str() member function. And by the way don't you realize that the std::string.c_str() function returns a char* so that cast should not be necessary in the first place.


Jim

This post has been edited by jimblumberg: 24 April 2018 - 09:58 PM

Was This Post Helpful? 0
  • +
  • -

#33 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 25 April 2018 - 06:10 AM

If you look at the constructors for ifstream, among the many variants, there are:
explicit basic_ifstream( const char* filename,
                std::ios_base::openmode mode = ios_base::in );
explicit basic_ifstream( const std::string& filename,
                std::ios_base::openmode mode = ios_base::in );



The first one is for before the C++11. In that case, you would simply passing in a C-style string. const char * is exactly what std::string::c_str() returns.

The second one if for C++11 and later. In that case, you would simply pass in a std::string.

I have a suspicion that your teacher learned how to use C++ using Microsoft's MFC which had its own CString class, and then incorrectly applied the concepts to the C++ standard libary.. If you were using that MFC class, then you had to cast the CString instance to a char * to invoke it's operator overrides and get the effect of returning a C string.

This post has been edited by Skydiver: 25 April 2018 - 10:46 AM
Reason for edit:: Added link to the MFC documentation regarding casting.

Was This Post Helpful? 1
  • +
  • -

#34 SloanTheSloth   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-April 18

Re: Vectors, Structs, and either not setting or retrieving string variable

Posted 25 April 2018 - 07:23 AM

View Postjimblumberg, on 24 April 2018 - 09:54 PM, said:

You do know that you have a plain text file that contains this information, right? So anyone who has access to the computer is free to look at that information no matter what your program does.



Haha I do. That's why "secure" was in quotes. Obviously for real world it's not secure, but for this project, it counts.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3