unwanted output from delimited file.

I am trying to finish up a homework assignment that reformats the output of a text file. When trying to run this it is skipping the first name in the list and putting that last persons information in twice. What am I doing wrong here?

text file
1
2
3
4
5
6
7
8
Bill Smith E 123 555 1212
Fred Smart B 234 333 5757
Joe Smith R 999 456 1234
Henry Field L 999 123 8900			
Blanche Devine S 122 234 5677
Arnold Tenagra R 237 789 2100
Wynn City D 985 231 7664
Cal Mazoo D 234 129 8908


code
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
        #include <iostream>
	#include <fstream>
	#include <cstdlib>

		int main()
	{
        char fName[25] = {};
		char lName[25] = {};
		char mName[25] = {};
		char area[10] = {};
        char prefix[10] = {};
        char lastfour[10] = {};
        char name[50] = {};
        char number [10] = {};
        char space [10] = {" "};
        char comma [10] = {","};
        char period [10] = {"."};
        char open [10] = {"("};
        char close [10] = {")"};
        char dash [10] = {"-"};
        int EmpCntr = 0;			
	     std::ifstream keithbyersIn;
	     keithbyersIn.open("tele.txt", std::ios::in);
	                        
	     if(!keithbyersIn)
	     {
	          std::cout << "Unable to open phone list file" 
					<< std::endl;
	          exit(-1);
	      }
	     
	     keithbyersIn >> fName
			          >> lName
                      >> mName
                      >> area
                      >> prefix
                      >> lastfour;        
        while(!keithbyersIn.eof())
       {
        keithbyersIn >> fName >> lName >> mName >> area >> prefix >> lastfour;
		strcpy(name, lName);
        strcat(name, comma);
        strcat(name, space);
        strcat(name, fName);
        strcat(name, space);
        strcat(name, mName);
        strcat(name, period);
        
        strcpy(number, open);
        strcat(number, area);
        strcat(number, close);
        strcat(number, space);
        strcat(number, prefix);
        strcat(number, dash);
        strcat(number, lastfour);
		std::cout << name << " " << number << std::endl;
		}
       keithbyersIn.close();   
       return (0);
}
You must modify your cycle. I do as follows:
1
2
3
4
5
6
7
8
while(1)
{
    keithbyersIn >> fName >> lName >> ......
    if(keithbyersIn.eof())
        break;
    strcpy(name, lName);
    strcat.........
}


without any input before the cycle.
Last edited on
Thank you for that information it worked perfectly. If you could though explain to me why the input before the cycle caused it to skip the first line and the change in cycle stopped it from printing the last line twice. I don't mean to inconvenience you I just want to understand why it did what it did.
In your original code, on lines 32-37, you put the first "line" of the file into the variables, and then enter the while loop, replacing the variables with the next line.

For the last record twice...I think it's because .eof() is not hit until you hit the actual last character, which you do not hit until you run through the loop once more at the end of the file...then when you put in "" to the variables, they don't change.
Topic archived. No new replies allowed.