Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need more specific help...im having an error while running this problem string subscript out of range this is the code im running now, and

i need more specific help...im having an error while running this problem

"string subscript out of range"

this is the code im running now, and the directions below.

thanks for the help!

#include

#include

#include

#include

#include

using namespace std;

// Enum varialbe

enum if_married { MARRIED, SINGLE, DIVORCED, WIDOWED } status;

// Function prototypes

if_married marrage(string, int&, int&, int&, int&);

string prefix(string, if_married);

string mid(string);

string last_names(string);

string first_name(string);

int main()

{

// declair variables to hold names and counts

ifstream infile;

ofstream out;

int wed = 0;

int div = 0;

int sin = 0;

int wid = 0;

int degree = 0;

int no_degr = 0;

string line, pre, first, middle, last;

// open the text file

infile.open("mp6names.txt");

//create output file to store the output

out.open("outnames.txt");

// check to see whether the file is exists or not

if (!infile)

{

cout << "cant open";

// error code

return -1;

}

// prime in read

getline(infile, line);

while (infile)

{

//counts the amount of people married and returns //their status

status = marrage(line, wed, div, sin, wid);

// converts the status to Mr, Mrs and Dr, etc

pre = prefix(line, status);

//gets the first name

first = first_name(line);

// gets the middle initail

middle = mid(line);

// gets the last name

last = last_names(line);

//Output to the file

out << endl;

out << left << setw(30) << line;

out << right << setw(5) << pre

<< left << setw(8) << first

<< left << setw(5) << middle

<< left << setw(14) << last << endl;

//get the line again

getline(infile, line);

}//end of while loop

out << endl;

out << setw(30) << setfill('*') << "*" << endl;

out << "Married " << wed << endl;

out << endl;

out << "Divorced " << div << endl;

out << endl;

out << "Single " << sin << endl;

out << endl;

out << "Widowed " << wid << endl;

out << setw(30) << setfill('*') << "*" << endl; cout << "Data written to output.txt" << endl;

system("pause");

return 0;

}

//implement the method for track the lastnames

string last_names(string name)

{

// starts at position 3

int tracker = 3;

// as long as its a space

while (name.substr(tracker, 1) == " ")

// keep going and adding one to tracker

tracker++;

//set the second tracker to tracker

int sec = tracker;

//keep adding one to sec as long as its not a ,

while (name.substr(sec, 1) != ",")

sec++;

return name.substr(tracker, sec - tracker);

}

//implement the method for the first name

string first_name(string fir)

{

// start at where ever a , is and add one

int position = fir.find(",") + 1;

// just in case know the size

string::size_type size = fir.size();

// if its a spaces keep adding on to position

while (fir.substr(position, 1) == " ")

position++;

int sec_pos = position;

// keep going unless the sec gets bigger size of the //string.

while (fir.substr(sec_pos, 1) != " " && sec_pos < size)

sec_pos++;

return fir.substr(position, sec_pos - position);

// return whats found

}

//implement the method for middle name

string mid(string middle)

{

string mid;

// start at where a period is

int start = middle.find(".");

// if the npos happened just exit or if its not

// or if its not at less 8 characters in

if (start < 8)

return " ";

start -= 2;

mid = middle.substr(start, 3);

mid = toupper(mid[1]);

return mid + ".";

}

//check the marrital status

if_married marrage(string pre, int& w, int& d,

int& s, int&ww)

{

switch (pre[1])

{

//increement the marritul statuses

case 'M': w++; return MARRIED;

case 'S': s++; return SINGLE;

case 'D': d++; return DIVORCED;

case 'W': ww++; return WIDOWED;

}

}

string prefix(string gen, if_married mar)

{

// if the person is a female and married

if (gen[0] == 'F')

{

if (mar == MARRIED)

return "Mrs.";

else

return "Ms.";

}

else

return "Mr.";

}

This assignment is to give you practice using enums, string variables, and string functions. In order to get full credit for the program you must use these three topics.

You are to write a program that will read a series of names of people from a data file that has been created with extra blank spaces and reformat the names into a standardized format.

The datafile is mp6names.txt. It is arranged with the information for one person on each line. The first character on the line will be either M or F indicating the gender of the person, and the second character will be either M, S, or D indicating married, single or divorced. You may assume that there will be no bad data here. The remainder of the line is the persons name in the form:

Last_name, First_name Middle_Initial.

Note that there will always be a comma immediately after the last name and, if the person has middle initial, it will always be followed by a period. However, there could be any number of blank spaces in between each part of the name and some people do not have middle initial.

Your task is to clean up the name and print it out in the standard format, e.g. Mr. Bill T. Jones with the appropriate title and exactly one space between each part of the name.

You are REQUIRED to use functions, enums, string variables and string functions in the solution to this problem. Define an enum for the Marital Status of the person (with the values SINGLE, MARRIED and DIVORCED) and write a function that reads in the character from the file and returns the appropriate enum value.

Read the name from the file into a string variable and write another function that uses the functions of the string class (e.g. find(), substr() etc.) to clean up the name and create a new string variable containing the name in the new format.

Finally, add the appropriate title to the name and print it out. All males will have the title Mr. married females will have Mrs. and single or divorced females will have Ms.

Continue doing this until the end of file is reached. Your output must include the original name as read from the file followed by the new name in the standardized format. For formatting purposes you may assume that no name will have more than 30 characters.

This is just a SUGGESTED method and for each step you may want to include output for debugging purposes to insure that your program is working correctly. Feel free to design your own modules as you like. A sample output from the first few lines of the data file follows:

Original name Standardized name Bach, Johann S. Mr. Johann S. Bach Curie, Marie A. Mrs. Marie A. Curie Parker, Alice M. Ms. Alice M. Parker

mp6names.txt

MS Bach, Johann S. FMCurie, Marie A. FS Parker, Alice M. FD Meir,Golda T. MDRobeson, Paul MM Ashe, Arthur FM Tubman, Harriet A. MSBraille,Louis 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 2.0 And Databases

Authors: John Kauffman, Bradley Millington

1st Edition

0471781347, 978-0471781349

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago