Trouble with strcat and text file input

I'm having trouble making sense of what is happening to my text file input. The goal of the assignment is to take a text file of names, and reformat the order into a separate char string. Then sort alphabetically.

The first iteration of my getNames function produces the correct output but after that, it garbles the rest. I checked the text file itself for any funny business. It's clear. However, I am at a loss because the logic makes perfect sense to me but the computer doesn't feel the same way as I do. Any guidance would be MUCH appreciated!

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;
const int MAX_NAMES = 10;
const int MAX_COMBINED = 60;
const int MAX_SINGLE = 20;

//Declare Prototypes
//void displayInfo();
void getNames(char [][MAX_COMBINED + 1], const int , const int, const int);
void displayNames(char [][MAX_COMBINED + 1], const int);
void bubbleSort(char[][MAX_COMBINED +1], const int );
void swap(char *x, char *y);



int main()
{
   char names[MAX_NAMES][MAX_COMBINED + 1];
   //Call Functions
   getNames(names, MAX_NAMES, MAX_SINGLE, MAX_COMBINED);

   displayNames(names, MAX_NAMES);

   bubbleSort(names, MAX_NAMES);

   displayNames(names, MAX_NAMES);
}
/********************************************************************
***  FUNCTION displayInfo                   			               ***
*********************************************************************
***  DESCRIPTION:  <Displays program information to user>         ***
***  INPUT ARGS:  <list of all input argument names>		         ***
***  OUTPUT ARGS:  <list of all output argument names>		      ***
***  IN/OUT ARGS:  <list of all input/output argument names>	   ***
***  RETURN:  <return type and return value name>		            ***
********************************************************************/

void displayInfo() //displays program information
{

   cout <<"*********************************************************" << endl;

   cout << "This program will read names from a data file one name at a time " << endl
        <<"then format and store them in a separate array. " << endl
        <<"It will then print them as they were entered " << endl
        <<"then sort them alphabetically and print again." << endl;

   cout <<"*********************************************************" << endl;
}
void displayNames( char nameList[][MAX_COMBINED + 1],const int size)
{
   for(int count = 0; count < size; count++) {
      cout << nameList[count] << endl;
   }
   cout << endl;
}

void getNames(char nameList[][MAX_COMBINED + 1], const int maxSize, const int maxSingle, const int maxLength)
{

      char firstName[maxSingle]; //in function
      char middleName[maxSingle];//in function
      char lastName[maxSingle];//in function
      char fullName[maxLength + 1];
      int index = 0; //in function

      ifstream inputFile;
      inputFile.open("names.txt");


      while((index < maxSize) && (!inputFile.getline(firstName, maxSingle, '\n').eof()))
      {

         inputFile >> middleName;
         inputFile >> lastName;


         strcpy(fullName,lastName);
         strcat(fullName, ", ");
         strcat(fullName, firstName);
         strcat(fullName, " ");
         strcat(fullName, middleName);
         strcpy(nameList[index], fullName);
            index++;


      }
      inputFile.close();

   //   for(int i = 0; i < MAX_NAMES; i++)
   //      cout << names[i] << endl;
}
void bubbleSort(char nameList[][MAX_COMBINED+1], const int size)
{
   for(int i = 0; i < size; i++)
//      for(int j = 0; j < MAX_COMBINED +1; j++)
         if(strcmp(nameList[i], nameList[i+1]) > 0)

         swap(nameList[i], nameList[i+1]);


}
void swap(char *x, char *y)
{
   char temp[MAX_COMBINED+1];
   strcpy(temp, x);
   strcpy(x, y);
   strcpy(y, temp);
}


Here is the text file Info I was using:
Barack
Hussein
Obama
George
Walker
Bush
William
Jefferson
Clinton
George
Herbert
Bush
Ronald
Wilson
Reagan
James
Earl
Carter
Gerald
Rudolph
Ford
Richard
Milhous
Nixon
Lyndon
Baines
Johnson
John
Fitzgerald
Kennedy

And here is my output result:
Obama, Barack Hussein
Walker, George
William, Bush
Clinton, Jefferson
Herbert, George
Ronald, Bush
Reagan, Wilson
Earl, James
Gerald, Carter
Ford, Rudolph

Obama, Barack Hussein
Walker, George
Clinton, Jefferson
Herbert, George
Ronald, Bush
Reagan, Wilson
Earl, James
Gerald, Carter
The problem is that the operator >> leaves a new line ('\n') in the stream which getline(...) interprets as an empty line.

By the way: why don't you use std::string. I'd much easier.
I would love to use std::string. Unfortunately, this is an assignment and the purpose is to work with cstrings. And for the record, I hope to never have to do that in the real world.

It took me a minute to realize what you meant but thank you for pointing that out. For any future people that are curious this is what it should look like, potentially:

[code]
void getNames(char nameList[][MAX_COMBINED + 1], const int maxSize, const int maxSingle, const int maxLength)
{

char firstName[maxSingle];
char middleName[maxSingle];
char lastName[maxSingle];
char fullName[maxLength + 1];
int index = 0; //in function

ifstream inputFile;
inputFile.open("names.txt");


while((index < maxSize) && (!inputFile.getline(firstName, maxSingle, '\n').eof()))
{

inputFile.getline(middleName, maxSingle, '\n'); //Don't use extraction operator
inputFile.getline(lastName, maxSingle, '\n');


strcpy(fullName,lastName);
strcat(fullName, ", ");
strcat(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, middleName);
strcpy(nameList[index], fullName);
index++;

}
inputFile.close();

}
/code]
Topic archived. No new replies allowed.