Question

Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15...

Do WITHOUT #include<sstream>

C++

You are to write a program that will read in upto 15 names, rearrange each person's name. The list has been collected from different places and the names are not in some standard format. A person's last name may appear last or it may appeaer first. A label has been associated with each name to identify where the last name appears. Either "surname" or "lastname" appears just before a person's last name. The other will be the first name followed by optionally middle initial or name. Capitalize the first letters. Examples of inputs are given below.

Read in the names, and arrange them with the last name first, followed by a comma, followed by the first name and his middle name or initial if it exist. Sort the list of names and print out the results.

input file: namesLast.txt

Example input:

     tom archer lastname jones

    lastname luewey Huewey dewy

    See More surname Or-less

Example Output(Sorted):

        Jones, Tom Archer

        Luewey, Huewey Dewy

       Or-less, See More

Please be original. Don't copy/paste from another response. I need explanation as to why things are assigned and what they do. If you want my upvote you need to earn it. Teach someone something new! I dislike having to use more than one question for the same question

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Note-While writing input to the names.txt if you copy them from somewhere else
//You might not get accurate results because the font-style and few other things may unknowingly get copied and you might get some extra characters
//So to check you should better type the names on your own and verify... :) :) Happy Coding
#include<iostream>
/*For file handling, you can be specific too.
For reading from a file use ifstream and for writing to a file use ofstream*/
#include<fstream>
/*For string datatype in this program*/
#include<string>
using namespace std;

int main()
{
//The variable for the file in the program
fstream read_file;
//Using the open function to open a file
//Remember that the file should be in the same location or the folder where your code exists
//Or else you have to give the full path of the file
//And dont forget to include the extension of the file type
read_file.open("names.txt",ios::in);
//Since the highest limit mentioned in the question is 15 I have taken 15 variables
string names[15];
//The variable for iterating through the names and keeping a count of them
int count_names=0;
//This if condition is used to check if the file has been properly loaded or not
if(!read_file)
{
cout<<"Unable to open the file"<<endl;
}
//The normal program gets executed if the file is loaded
else
{
//This while loop runs until the end of the file
while(!read_file.eof())
{
//Since every single name is on one line and contains four words we need to read four words
//from each line,so I have declared four string variables and read them
string s1,s2,s3,s4;
read_file>>s1>>s2>>s3>>s4;
//Since the program asks us to convert the first letter of all the words to uppercase
//I have subtracted ASCII value of 32 from the first letter of the word so that we get capital letter
s1[0]=s1[0]-32;
s2[0]=s2[0]-32;
s3[0]=s3[0]-32;
s4[0]=s4[0]-32;
//The below lines have the main logic of the program
//If we analyse the names in the files then there are only two possibilities
//The word surname or lastname can appear either as the first word or the third word
//Surname/LastName last_name first_name middle_name : 1st Case
//first_name middle_name Surname/LastName last_name: 2st Case
//So using this idea I have appended the names accordingly and created the full name for the person
if(s1=="Lastname"||s1=="Surname")
{
names[count_names]=s2 + ", " +s3+" "+s4;
}
else if(s3=="Lastname"||s3=="Surname")
{
names[count_names]=s4+", "+s1 +" "+s2;
}
//This variable keeps the count of the names so far
count_names++;
}
//The for loop below is used to sort the names in their alphabetical orders using a basic selection sort
for(int i=0;i<count_names;++i)
{
for(int j=i+1;j<count_names;++j)
{
string temp;
if(names[i]>names[j])
{
//This is an inbuilt function that takes two values and then swaps them
//If this doesn't work for in your compiler,then use a temporary variable and then swap them
swap(names[i],names[j]);
}
}
}
//This loop finally prints all the names to the console out
for(int i=0;i<count_names;++i)
{
cout<<names[i]<<endl;
}
}
//Finally we close the file that we opened
read_file.close();
return 0;
}

Input File......Output.....

How both the files should exist in same folder...The Program in the programming window...

Hope it helps you ...

And I hope I have cleared all your doubts

Add a comment
Know the answer?
Add Answer to:
Do WITHOUT #include<sstream> C++ You are to write a program that will read in upto 15...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You...

    I need help with this program using c++ language! CS 317 Program Assignment Name Arrange You are to write a program that will read in each person’s name and print out the results. The list has been collected form different places and the names are not in some standard format. A person’s last name may appear last or it may appear first. A label has been associated with each name to identify where the last name appears. Either “surname” or...

  • 6. Write a program that inputs the first name, middle initial (wirh period), and last name...

    6. Write a program that inputs the first name, middle initial (wirh period), and last name of a user and displays that person's n first name first, middle initial followed by a period, and last namelt hel will need the following variables: FirstName (a String) MiddleInitial (a String) LastName (a String) name with the

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is: Maya Jones Jones, Maya import java.util.Scanner; public class SpaceReplace {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       String firstName;       String lastName; //answer goes here//    } }

  • Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter...

    Write a C++ program that produces the following output: /* OUTPUT Enter your age: 21 Enter the last name: Lee Hello Tom Lee. You are 21 years old. Press any key */   Declare an array named: firstName The array is a c_string, i.e., it is a null-terminated character array. The size of the array is 10. Assign a first name to it when it is declared.   Declare an array named: lastName The array is a c_string, The size of the...

  • Write a program that reads a person's first and last names, separated by a space. Then...

    Write a program that reads a person's first and last names, separated by a space. Then the program outputs last name, comma, first name. End with newline. Example output if the input is Maya Jones Tones, Maya import java . util·Scanner; public class SpaceReplace 4 public static void main (String [1 ares) f( Scanner scnr-new Scanner(System.in); String firstName; String lastName; Your solution goes here */ 10 12

  • *****Requiremrnt: 1. Please do not use any array or arraylist 2. collects only a single String...

    *****Requiremrnt: 1. Please do not use any array or arraylist 2. collects only a single String input 3. uses at least 3 String methods 1) Just Initials: Print just the initials in upper case letters separated by periods 2) Last Name First With Middle Initial: Print the last name in lower case with the first letter capitalized, followed by a comma and the first name in in lower case with the first letter capitalized and then the middle initial capitalized...

  • Write a program that prompts the user to enter a list of names. Each person's name...

    Write a program that prompts the user to enter a list of names. Each person's name is separated from the next by a semi-colon and a space (: and the names are entered lastName, firstName (i.e. separated by ',). Your program should then print out the names, one per line, with the first names first followed by the last names. A sample run of your program should look like Please enter your list of names: Epstein, Susan; St. John, Katherine;...

  • Please help me with this program.You are to write a C++ program that will read in...

    Please help me with this program.You are to write a C++ program that will read in up to 15 students with student information and grades. Your program will compute an average and print out certain reports. Format: The information for each student is on separate lines of input. The first data will be the student�s ID number, next line is the students name, next the students classification, and the last line are the 10 grades where the last grade is...

  • In C++, You are given a file of customer data with records in the following format...

    In C++, You are given a file of customer data with records in the following format : account number, last name, first name, middle name. Write a program to convert the input data and display it to the monitor in the following format : Accout Name     Logon ID Initial Pswd 21-88282712-B Keith S. Adams ADAM21 Adam88282 08-36847734-A John R. Sturm STUR08 Stur36847    etc. Notes :     1) The account number on the input is 10 digits     2)  The names on...

  • C++ Write a program that reads students’ names followed by their test scores from the given...

    C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT