Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone help me with this code in C++? Write a function to convert a string

ID: 3931097 • Letter: C

Question

Can someone help me with this code in C++?

Write a function to convert a string containing lastName, firstName middleName to firstName middleName lastName. For example, if the string contains: "Miller, Jason Brian", then the altered string should contain "Jason Brian Miller." A data file with names has been provided for testing. Write a driver that reads each line in as a string, uses your function to convert the name and then displays the converted name.

data:

Include:

Analysis at the top of the file

Function analysis, preconditions, and postconditions after each function prototype

Major tasks identified as comments in main function

Comments in functions as needed

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;

//function to convert input as required
//pre-conditions: input line as param
//post-cndiiotns:return changes updated string
void convert(string input)
{
    //declare req variables
    string output="";
    //display input string
    cout<<" Input string: "<<input;
    //find index of ',' and update string as requried
    int index=input.find(',');
    output= input.substr(index+1) +" " + input.substr(0,index);
    //display output string
    cout<<" Converted string: "<<output;
}

//main function
//pre-conditions: file exists and data is there,convert() in place
//postcondiitons:exits after readng file
int main()
{
    //declare variables
   string line;
   //open file for reading
ifstream myfile ("names.txt");
if (myfile.is_open())
{
      //read until EOF reached
    while ( getline (myfile,line) )
    {
        //pass each read line to funciton
      convert(line);
    }
    //close file after reading
    myfile.close();
}
//display err message if unable to open file
else cout << "Unable to open file";
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote