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