I'm not sure what's wrong exactly. I keep getting different errors with different compilers and nothing will build this thing. Need a second opinion, thanks.
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.
You use a function getEmployeeList which is not defined in your code.
Syntax and indentation is right. getEmployeeList is a function I just didn't include that in this post. Just erase that. So your post is invalid please come back with an answer.
1. That is hard to see. You really should add those code tags, because it makes the code much easier to read.
2. You have implied that syntax is not right. It is the wrong syntax that generates compiler errors.
The compiler errors are very useful. They tell what/where is wrong. We cannot possibly help you (yet) to understand those messages, because you haven't told us the errors.
Variable length arrays are not a standard C++ feature.
I've tried updating xCode and everything, still doesn't work.
The error with VisualStudio is something completely different referring to fstream people(name,ios::app); It's a MinGW error? I completely updated MinGW and still doesn't work.
I saw many examples where you could throw a int variable into that structure variable to make it an array of structures. As long as the value is there as the array is being initialized. I'm not sure if you can tell what I'm doing with this program, but I'd like an individual variable for each different structure that the user wants. However, I can throw a number into the statement like so:
and that seems to compile fine. I just don't want to have 100 structures if the user only asks for 3. Opposite, I don't want to only have 100 structures if the user asks for 150..
I tried it and got this new error.
26:15: error: cannot initialize a variable of type 'Employee *' with
an rvalue of type 'int *'
Employee* emp = new int[Number];
Oh Im sorry, I gave you the wrong thing!
Its suppose to look like this Employee* emp = new Employee[Numbers];
Just to give you some quick info. The only thing this does is just allocate memory on the heap, rather than stack. Which is something you can look up anywhere :)
Please, no new like that. The manual dynamic allocation of memory with new is standard C++, but modern C++ has easier and safer alternatives. The thing is that if you explicitly allocate memory, then you have to deallocate it appropriately too and that is anything but trivial.
There are still situations, where you will have to use new (and raw pointers) but by the time you will have to face them, your overall C++ knowledge will be better.
The object that is created there (emp) behaves like plain array, but does ensure that the array is automatically deallocated at proper moment.
However, there is an alternative that has much more features; the std::vector. std::vector<Employee> emp( Numbers );
The emp is still used like an array:
1 2 3 4
for( size_t i = 0; i < emp.size(); ++i )
{
std::cout << emp[i];
}
Passing to a function naturally changes:
1 2
void saveEmployee( std::fstream & p, std::vector<Employee> & e );
void saveEmployee( std::fstream & p, const std::unique_ptr<Employee[]> & e );
Sure @Keskiverto. But I naturally assume people who post this kind of things are studying and this is a homework/assignment. And in those you cant use whatever you want, and they probably want them to use new and delete so they get an idea of what it is.
But if you're doing this on your own @OP. Go with what the post above me showed.
Thank You Guys that helped a lot! I've finished it completely. The whole Employee* emp = new Employee[Numbers]; part had never been explained to me. That opens quite a few windows