Hello mpelle,
I am not allowed to use member functions I have not learned yet |
. Well that is stupid. I may have a different way of looking at things, but you were told that you needed the header file"vector" for this program. Once you include this file you have access to all that the header file has to offer even if it is not taught in class. Some of that you will have to learn on your own. If you wait for something to be taught in class it may take years and still may not be covered.
As I have had the time to work with your program I have found this:
1 2 3 4
|
int main()
{
vector <int> vector(0);
int decision;
| |
I changed it to this:
1 2 3 4 5 6
|
int main()
{
std::vector<int> vNums;
int decision;
srand(static_cast<size_t>(time(nullptr))); // <--- Only needs done once. And not in a function.
| |
I originally put the "std::" to distinguish between the class being used as the type and the variable name, but since I changed the variable name you can remove the "std::" if you want. The space beterrn "vector" and "<int>" is not needed, but makes no difference to the compiler. More often I see things like this written without the space.
After watching this video
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful I found this is the best way to write "srand". When you look up "srand" you will find that its parameter is an "unsigned int", size_t is an alias for "unsigned int", and "time" does not return an "int" or "unsigned int" hence the type cast. Easy to miss, but the video does mention that "nullptr" is the better parameter form C++11 on. There is more than 1 way to write "srand", but these are likely to produce warnings at compile time and should be addressed.
The rest of "main" works and I have not looked closely at what is there to see if it is worth changing.
The first function called is
loadVector(vNums);
. Looking at that function you can use what you have, but as
Ganado said
Also,
1 2
|
vector.resize(vector.size() + 1);
vector[vector.size() - 1] = num;
| |
Small tip: This is doing what push_back already does (just less efficiently). |
The function can be written as:
1 2 3 4 5 6 7 8 9
|
void loadVector(std::vector<int> &vNums)
{
//int seed = time(0); // <--- "time(0)" does not return an "int" or the "unsigned int" that "srand" needs.
//srand(seed); // <--- Should be in "main"
for (int index = 0; index < SIZEMAXIMUM; index++)
vNums.push_back(rand() % (MAX - MIN + 1));
}
| |
The way this works is that "rand" will generate a number before it is put at the end of the vector with "push_back".
Another way to write this would be:
1 2 3 4 5 6 7 8 9 10 11 12
|
void loadVector(std::vector<int> &vNums)
{
//int seed = time(0); // <--- "time(0)" does not return an "int" or the "unsigned int" that "srand" needs.
//srand(seed); // <--- Should be in "main"
for (int index = 0; index < SIZEMAXIMUM; index++)
{
int num = rand() % (MAX - MIN + 1);
vNums.push_back(num);
}
}
| |
The 2 lines commented out ar there to show you that they do not belong in the function.
In either bit of code the "push_back" function will add to the end of the vector and the vector class will keep track of the new size of the vector.
In the other functions where you use
for (int index = 0; index < vector.size(); index++)
. The ".size()" function will return a "size_type" variable. This is defined as:
Member type size_type, or size_t, is an unsigned integral type. |
Usually not a problem. It should produce a warning, but will not stop the program from running.
Also usually best to keep the types the same. This would be a better way to write these lines:
for (size_t index = 0; index < vector.size(); index++)
If there is anything you do not understand just ask.
In the "deleteFromVector" function the 2 if statements could be changed to an if/else/else. I ended up with this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
bool deleteFromVector(vector<int> &vNums, int decision)
{
if (decision == ODDNUMS)
{
for (auto it = vNums.begin(); it != vNums.end();)
{
if (*it % 2)
it = vNums.erase(it);
else
it++;
}
}
else if (decision == EVEN_NUMS)
{
for (auto it = vNums.begin(); it != vNums.end();)
{
if (!(*it % 2)/**it % 2 != 0*/)
it = vNums.erase(it);
else
it++;
}
}
else
{
std::cout << " Invalid Choice!\n";
return true;
}
return false;
}
| |
Andy