|This file has 10 people: name, age, weight.|
You are to infile this into 3 different arrays to hold the data.
This means that you will have three arrays of length 10, which will hold name, age and weight.
Your own code has some mistakes so far: you declare name
twice as two different things (this is an error) and also do not declare age
|Once the information is on the arrays, you are to pass these arrays to a function.|
What is that function's name? Is it required to have a certain name?
void my_function(string n, int a, int w)
// will later be used like:
my_function(names, ages, weights);
|You are to search for everyone with this name and delete all the information of that element in the array (there may be many people with that name, so delete them all).|
This is a stupid requirement, if I understand it correctly.
In C++ an array cannot change its size; that is to say you can't delete elements from it. What you can do is overwrite them, and ignore the extra elements.
I believe a good solution should focus on the goal: write the "deleted" people to Deleted.txt
and the remaining people to Updated.txt
. This can be done by simply reading the arrays, without trying to "delete" anything from them.
using namespace std;
// seed the Random Number Generator;
// without doing this you'd get the same "random" number
// every time you ran the program
// pick a random number 0-9
int r = rand() % 10;
for (int i=0; i < 10; ++i)
if (names[i] == names[r])
// write names[i] ages[i] weights[i] to Deleted.txt
// write names[i] ages[i] weights[i] to Updated.txt
The code above doesn't respect the requirements strictly: you need to move the for()
loop and the choosing of r
out of main()
and into my_function()
My post doesn't cover reading data from Original.txt