Question

In: Computer Science

Write a program in C++(no import of Javax or anything similar) that does the following: There...

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.

Solutions

Expert Solution

#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;
}


Related Solutions

Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Write a C program that does the following In this part, you will write more complicated...
Write a C program that does the following In this part, you will write more complicated functions. They will require parameters and return values. The purpose is to give you experience with these components, and to show you how functions can be used to break your code down into smaller parts. You will also get some more experience with iterating through arrays.Open repl project Lab: User-Defined Functions 2. Write a program that does the following: 1.(20 pts.) Allows the user...
You are to write a C++ program which does the following: Reads in the size of...
You are to write a C++ program which does the following: Reads in the size of a list of characters. Reads in the list of characters. Prints the list of characters in the opposite order read in. Prints the list of characters in the order read in. Sorts the list. Prints the sorted list. You may assume there will be no more than 1000 characters in the list. (You should use a constant to make this limit easily changeable.) You...
Write a C program that does the following. (a) Start with comment statements at the top...
Write a C program that does the following. (a) Start with comment statements at the top (before your includes) with the following info: Name, Date (b) Write the following functions: void setupRandArray(int n, int x[], int min, int max) void printArray(int n, int x[], char label[]) float getAverage(int n, int x[]) int getMaximum(int n, int x[]) int getCountInRange(int n, int x[], int min, int max) Note: This function returns the number elements of x that are between min and max...
Write a C++ Program that does the following: As you can see, there is a file...
Write a C++ Program that does the following: As you can see, there is a file named "invoice1_test1.txt". You are to use this file as your input file for your program. Do the following: 1. Sort the file by last name using an array. You can use any of the sorting algorithms we have previously used in class. You may use the string data type to store text data. 2. Compute the following: a. The total balance due using the...
Write a program that does the following things: in C++ 1 ) ask the user for...
Write a program that does the following things: in C++ 1 ) ask the user for 2 primary colors (red, blue, yellow) [should be in main] 2 ) determine the secondary color the 2 primarily colors make [should be in newColor function] red + blue = purple red + yellow = orange blue + yellow = green 3 ) Print the following message <color1> and <color2> makes <color3>
I have to write a c program for shipping calculator. anything that ships over 1000 miles,...
I have to write a c program for shipping calculator. anything that ships over 1000 miles, there is an extra 10.00 charge. I have tried everything. no matter what I put, it will not add the 10.00. please help here is my code #include <stdio.h> #include <stdlib.h> int main() { double weight, miles, rate, total; printf("Enter the weight of the package:"); scanf("%lf", &weight); if (weight > 50.0) { puts("we only ship packages of 50 pounds or less."); return 0; }...
Write a C++ program that does the following: Read and input file containing the following PersonAName,...
Write a C++ program that does the following: Read and input file containing the following PersonAName, PersonBName, XA,YA, XB, YB where the coordinates of PersonA in a 100 by 100 room is XA, YA and the coordinates of PersonB is XB, YB. Use square root function in cmath sqrt() to calculate the shortest distance between two points. A file will be uploaded in this assignment that will list coordinates of two people. The program should use a function call that...
Write a program in C that does the following: 1. Declares an array called numbers_ary of...
Write a program in C that does the following: 1. Declares an array called numbers_ary of 6 integer numbers. 2. Declares an array called numbers_ary_sq of 6 integer numbers. 3. Reads and sets the values of numbers_ary from the keyboard using a loop. 4. Sets the values of numbers_ary_sq to the square of the values in numbers_ary using a loop. 5. Displays the values of numbers_ary and the values of numbers_ary_sq beside each other using a loop. Example Output Assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT