home work

this is my assignment, and below is what i've done so far. Any help is appreciated I know I don't have much but if someone could explain how to do it
I would really appreciate it thanks

You are to write a program which uses the following file attached "Original.txt".
This file has 10 people: name, age, weight.
You are to infile this into 3 different arrays to hold the data.
Once the information is on the arrays, you are to pass these arrays to a function.

(the rest of the program is done in this function)... This function will pick a random number from 0-9.
Locate the name of this element in the array.
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). outFile these people into a file called "Deleted.txt". All those which were not deleted will be outFiled into a file called "Updated.txt". keep the person's name, age, and weight correctly for each person when you send them to these two files.


#include <iostream>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;


int main()
{
ifstream inFile;
inFile.open("Original.txt");
string name;
char name [10];
int age;
int weight;

string nameguess;
char guess[4];
inFile >> name;
for (int x = 0; x<4; x++)
inFile >> [x] age;
inFile >> age;
cout << name << " ";
for (int x = 0; x<4; x++)
cout << [x] age;
cout << " " << age << endl;
What does the "Original.txt" look like?
Nick 18 155
Tom 22 180
Jessica 14 87
Brian 38 225
Maria 21 122
Jeff 33 186
Maria 19 148
Nick 11 111
Maria 45 133
Tom 11 95
This file has 10 people: name, age, weight.
You are to infile this into 3 different arrays to hold the data.

Three arrays: name, age, weight. What would be good types for those three arrays?
not sure what you mean by types sorry I'm barely learning
Consider using getline()
http://www.cplusplus.com/reference/string/string/getline/?kw=getline

and a for loop to loop through the file:
for (int lineno = 0; getline (inFile,line); lineno++)
not sure what you mean by types sorry I'm barely learning


The built-in data types like int, double, char etc. string class too
Last edited on
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.
okay i have this now but what I don't get is how to first input original.txt and then how to separate it into the array and then how do I get which ones are supposed to be deleted thanks in advance. And thanks to all those who already helped

#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()

{
ifstream inFile;
inFile.open("Original.txt");
string names[10];
int ages[10];
int weights[10];

fstream file;
ifstream inputfile;
ofstream outputfile;

ifstream file("Original.txt");
if (file.is_open())
{
string myArray[10];
cout << "File open error!" << end1;
return 0;
}




void my_function(string n[10], int a[10], int w[10])

{

srand(time(NULL));


int r = rand() % 10;
mike7k wrote:
okay i have this now but what I don't get is how to first input original.txt and then how to separate it into the array and then how do I get which ones are supposed to be deleted thanks in advance.

Please use code tags when posting code. Read this article:
http://www.cplusplus.com/articles/jEywvCM9/

I strongly urge you to read the C++ tutorial on this website:
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/files/

Back to your code, here's a sample layout that you can build on top on:

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
#include </* ... */>

using namespace std; // bad habit, should un-learn one day

void my_function(string n[10], int a[10], int w[10])
{
    // - open Updated.txt
    // - open Deleted.txt
    // - choose a random r
    // - use the for() loop provided in my previous post
}

int main()
{
    srand(time(NULL)); // seeding goes here, not inside my_function()

    string names[10];
    int ages[10];
    int weights[10];

    // - open Original.txt

    for (/* 10 times, i counts from 0 to 9 */)
    {
        // - read names[i] from Original.txt
        // - read ages[i] from Original.txt
        // - read weights[i] from Original.txt
    }

    my_function(names, ages, weights);
}

Last edited on
Topic archived. No new replies allowed.