Write a program using MVS C++ which uses the following file attached \"Original.
ID: 3558428 • Letter: W
Question
Write a program using MVS C++ which uses the following file attached "Original.txt". This file has 10 people: name, age, weight
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. 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.
Explanation / Answer
//Write a program using MVS C++ which uses the following file attached "Original.txt". This file has 10 people: name, age, weight
#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<time.h>
using namespace std;
void delete_rand(string name[],int age[],double weight[],int index)
{
// This function will pick a random number from 0-9.Locate the name of this element in the array. 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).
int del_num = rand()%10;
ofstream outfile("Deleted.txt");
ofstream outfile2("Updated.txt");
// 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.
for(int i=0; i<index; i++)
{
if(name[i].compare(name[del_num])==0)
{
outfile << name[i] << " " << age[i] << " " << weight[i] << endl;
name[i] = "";
}
}
for(int i=0; i<index; i++)
{
if(name[i]!="") {
outfile2 << name[i] << " " << age[i] << " " << weight[i] << endl;
}
}
outfile.close();
outfile2.close();
}
int main()
{
string name[10];
int age[10];
double weight[10];
srand(time(NULL));
ifstream infile("Original.txt");
if(!infile)
{
cout <<"unable to open file so exiting from program " << endl;
return 0;
}
int index = 0;
while(!infile.eof())
{
infile >> name[index] >> age[index] >> weight[index];
index++;
}
infile.close();
delete_rand(name,age,weight,10);
//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)...
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.