(getline(infile, data)) help ignoring line breaks

im trying to reformat the address information given to me in a text file. that part i can do, but i need to know how to get my program to ignore the line breaks in between the text blocks.

example:
Jane Doe
1234 streetname avenue
city
state
zip
<------- (ignore?)
john doe
5678 streetname blvd
city
state
zip

i've tried an if else where if the value for customer name is 0 the program must reattempt to collect a value for customer, but it wont compile.


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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()

{
char input;

ifstream inFile;

string data, filename="C:\\TelegramData.txt", message, customer, street, city, state, zip, translation, blank;

int count;
double cost=0;


cout << "welcome to Western Union Telegraph Company." << endl;
cout << "1 - Process Telegram Bill" << "\n2 - Translate to Morse Code" << endl;

cin >> input;

cout<< endl;

switch (input)

	{
		
		
		break;
		
		case '3':
			inFile.open(filename.c_str());
				
			while (!inFile.eof())
			{
			(getline(inFile, customer));
			(getline(inFile, street));
			inFile >> city >> state >> zip >> count;
			cout << customer <<endl;
			cout << street << endl;
			cout << city << ", " << state << " " << zip << endl << "amount owed: $" << cost;
		 	}
		 	
	return 0;
			
		
		
Hello Yogib,

I can see several problems:

Line 34. If your compiler is able to compile to the C++11 standards the ".cstr()" is not needed. If not it would be a good idea to set the IDE to use the C++11 standards. or update your compiler.

The while condition is based on ".eof", This does not work the way you think it does. by the time the while condition figures out that the "eof" bit is set you will have processed your last read twice.

The first line in the while loop should be: if (customer == "") continue;. This will pick up on the blank line and pass over it.

Mixing a "getline" and formatted input ("inFile >>") as long as you understand the difference. A "getline" will extract everything from the input buffer including the new line character leaving the input buffer empty. The formatted input will extract up to the first white space or new line whichever comes first, but it will leave the new line in the buffer. If the next statement is a "getline" it will extract the new line and not read the file.

The while loop should work better as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
inFile.open(filename);

if (!inFile)
{
	std::cout << "\n File " << std::quoted(fileName) << " did not open" << std::endl;
	return 1;  //exit(1);  // If not in "main".
}

while (getline(inFile, customer))
{
	if (customer == "") continue;  // <--- To skip the blank line.
	getline(inFile, street);

	inFile >> city >> state >> zip >> count;
	inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.

	cout << customer << endl;
	cout << street << endl;
	cout << city << ", " << state << " " << zip << '\n' << "amount owed: $" << cost << std::endl;
}


When you open a file for input you need to make sure the file is open and ready to read. The if statement after the open will do this.

What is in bold are the changes or additions. I have not tested this, but I believe it should do what you want. If not let me know.

I also noticed on line 14 you read "count", but I do not see this in your input file.

On line 19 you output "cost", but I do not see where that comes from. Something to keep in mind as you change the code.

Some suggestions:

Watch the indenting, or lack of, on while loop. Also some blank lines makes the code easier to read.

Consider const std::string PATH{ "C:/" }. Yes the forward slash works just fine here. This path may be short this time, but in the future it could become longer.

Then you could do std::string fileName{ PATH + "TelegramData.txt" };

on line 13 of your code I see no use for "blank". This may have been something you tried and did not take out.

Tomorrow I will load this up and give it a test.

Hope that helps,

Andy
Topic archived. No new replies allowed.