Question

In: Computer Science

Using OOP, write a C++ program that will read in a file of names. The file...

Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in a grade submission of 0.

The Names used for the Names.txt are: Jackie, Sam, Bill, Tom, Mary, Paul, Zev, Barb, John, Sharon, Dana, Dominic, Steven, Padro,,Katey, Kathy, Darius, Angela, Mimi,Jesse
Kenny, Lynn, Hector, Brittany, Jenn, Joe, Chloe, Geena, Sylvia, Dean.

I tried this solution on this question and it had a lot of errors.

Solutions

Expert Solution

#include<iostream>
#include<fstream>
using namespace std;
//class read_name
class read_Name
{
    private:
        ifstream infile;
        string name[30];
    public:
//method prototypes
      void setdata();
       void sort();
       void print();   
};
//this method will read names from file and store it in array
void read_Name ::setdata()
{
    int i=0;
    //open the file
    infile.open("names.txt");
    //unable to open the file
    if(!infile)
    {
       cout<<endl<<"Unable to open the file";
       exit(0);
       }
       //read the names from file
   while(!infile.eof())  
   {
      //read the names from file assign it to name array
      infile>>name[i];
     
      i++;//increase the index
   }
   //close the file
   infile.close();
}

//this method will print the names
void read_Name :: print()
{
   int i;
   //loop to print the names
   for(i=0;i<30;i++)
   {
      cout<<"\t"<<name[i];//print the name
     
       }
   }
  
   //this method will print the names
void read_Name :: sort()
{
   int i,j;
   string t;
   //loop to sort the names
   //selection sort
   for(i=0;i<30;i++)
   {
   for(j=i+1;j<30;j++)
           {
              if(name[i] > name[j])
              { //swap the names
                 t=name[i];
                 name[i]=name[j];
                 name[j]=t;
               }
               }  
           }
     
   }
   //driver program
int main()
{
   read_Name obj; //declare an object
   obj.setdata(); //call to setdata()
       cout<<endl<<"Before sort the data \n";
   obj.print(); //call to print()
   obj.sort(); //call to sort()
   cout<<endl<<"After sort the data \n";
   obj.print(); //call to print()
  
   }

output


Related Solutions

Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
Write a pyhton program to read from a file the names and grades of a class...
Write a pyhton program to read from a file the names and grades of a class of students, to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a dataInput() function that reads data from a file and stores it and returns it as a dictionary....
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
Write a program to read in a list of domain names from the provided file (domains.dat),...
Write a program to read in a list of domain names from the provided file (domains.dat), and print the reverse domain names in sorted order. For example, the reverse domain of cs.princeton.edu is edu.princeton.cs. This computation is useful for web log analysis. To do so, create a data type Domain, using reverse domain name order. The H file contains the required functions. The compareTo function is provided for you. This will allow you to compare 2 Domain objects. Also use...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a c program that creates a struct to be able to read a .img file...
Write a c program that creates a struct to be able to read a .img file and a .pat file.
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Program in Bash: Write a program using bash script that can read a file from the...
Program in Bash: Write a program using bash script that can read a file from the same directory, sort the nonrepeating integers from 0-9 from smallest to largest, and output the results on the same line. Do not use the sort function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT