mike7k wrote: |
---|
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.
1 2 3
|
string names[10];
int ages[10];
int weights[10];
|
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 and
weight as arrays.
mike7k wrote: |
---|
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?
1 2 3 4 5 6 7
|
void my_function(string n[10], int a[10], int w[10])
{
// ...
}
// will later be used like:
my_function(names, ages, weights);
|
mike7k wrote: |
---|
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.
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
|
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// seed the Random Number Generator;
// without doing this you'd get the same "random" number
// every time you ran the program
srand(time(NULL));
// 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
}
else
{
// 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.