check true/false for end of string

closed account (S2wCM4Gy)
I am attempting to write a program that reads names from a file, rearranges the names, and then writes them to a new file in a different order. In the original file there are the following names below:

Miller, Jason Brian
Blair, Lisa Maria
Gupta, Anil Kumar
Arora, Sumit Sahil
Saleh, Rhonda Beth

I created a second file removing the middle names to test my conditional statement. The problem I have is that in my attempt to make a conditional statement for if the individual has a middle name or not the results puts the initial of the last name "Miller" in place in the first result giving me Jason M Miller for example in the first line.

What is wrong with my conditional?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

int main()
{
	ifstream infile;
	ofstream outfile;

	//starts lastName middleName firstName
	//ends firstName middleName lastName

	//opening the input file and creating output file

	infile.open("Names.txt");
	outfile.open("NamesRevised.txt");

	string first, middle, last;
	string names = " ";
	int x = 0;
	int y = 0;
	int z = 0;

	//Put any number of names in file

	while (!names.empty())
	{
	getline(infile, names);
	
	//I will find the last name below.
		
	x = names.find(",");
	last = names.substr(0,x);

	/*Below I will erase the first space in the string to make my find function easier. I find the first name.*/

	names.erase(x+1,1);
	y = names.find(" ");
	first = names.substr(x+1,y-x-1);

	//Below I will find the middle name.
	if (names == "\0")
{
	z = names.length();
	middle = names.substr(y+1,z-y);
}
	//One for testing one for writing.

	cout << first << " " << middle << " " << last << endl;
	outfile << first << " " << middle << " " << last << endl;

	}

	system("pause");
	return 0;
}
Last edited on
Using a string stream to parse the input would simplify the task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

int main()
{
    {
        // create infile for tesing
        std::ofstream( "Names.txt" ) << "Miller, Jason Brian\n"
                                        "Blair, Lisa Maria\n"
                                        "Gupta, Anil Kumar\n"
                                        "Arora, Sumit Sahil\n"
                                        "Saleh, Rhonda Beth\n" ;
    }

    std::ifstream infile( "Names.txt" ); // open the input file
    std::ofstream outfile( "NamesRevised.txt" ) ; // and the output file

    std::string line ;
    while( std::getline( infile, line ) ) // canonical: for each line in the input file
    {
        std::istringstream stm(line) ; // initialise a string stream that reads from the line

        std::string last_name ;
        std::getline( stm, last_name, ',' ) ; // read everything up to the comma, extract and discard the comma

        std::string first_name, middle_name ;
        stm >> first_name >> middle_name ; // skips leading white space

        char initial = middle_name.empty() ? ' ' : middle_name[0] ;

        std::cout << first_name << ' ' << initial << ' ' << last_name << '\n';
        outfile << first_name << ' ' << initial << ' ' << last_name << '\n';
    }
}

http://coliru.stacked-crooked.com/a/eb831c8698cfa6d2
closed account (S2wCM4Gy)
Thank you very much JLBorges and sorry it took me a while to get back to you. Thanks for your help. :)
Topic archived. No new replies allowed.