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 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;...
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.
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.
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.
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT