In: Computer Science
Write a program in C++(no import of Javax or anything similar)
that does the following:
There is a class human and a derived class student.
The enum in this case is gender(male, female)
Read in a text file of unknown line number that is
formatted:
name;id;enum
name1;id1;enum1
The delimiter is ';'
Dynamically allocate student objects and store the objects in the
array humanList (an array of human pointers). Include a counter for
number of lines in the file as well.
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
class human{
};
class student : public human{
public:
char name[10];
int id;
char gender[4];
};
int main()
{
student *s = new student; //dynamic allocation to
student object;
human *h[2]; //an array of human pointers
int j,k, i;
char line[64]; char words[3][10];
int count = 0, l = 0;
ifstream file("data.txt"); //open the input file
j = k = 0;
while(file >> line) // reading file line by
line
{
count++;
for(i=0; i<strlen(line);
i++){ //separate the words by ;
if(line[i] == ';'){
words[j][k] = '\0';
j++;
k=0;
}
else
words[j][k++] = line[i];
}
strcpy(s->name,words[0]);
s->id =
atoi(words[1]);
strcpy(s->gender, words[2]);
cout<<s->name<<" "<<s->id<<"
"<<s->gender<<"\n";
h[l++] = s; //
store the student object to an array of human pointers
j = 0;
}
cout<<"Number of Lines: "<<count<<"\n";
return 0;
}
