Displaying a .txt File with a While loop and the .substr() function

Hey guys, my first post here.

I'm working on an assignment that basically has a list of Mail records (Name, address following the name) and need to figure out how to open this with a loop and the .substr() function. I've given it a fair shot, but I think it needs a few kinks ironed out. The main problem is that when I run it, nothing is displayed so I'm not sure how to approach the issue itself.

For any background context, the listRecords() function is used in a switch case where the user inputs that they want to see a list of the records.

Also provided are a list of the names which were added to the MailList.txt file through being appended.

Martin P Sanderson 8329 Madison Kansas City MO 64134
Samuel K. Roberts 4319 Oak Kansas City MO 64110
Eleanor M. Hutchinson 8814 Floyd Overland Park KS 66218
Jennifer Sue Bron 6824 Oxford Independence MO 64028
Jerry Lee Hovis 3015 Main Kansas City MO 64109
Mary Jo Serviss 2419 E. 89th St. Kansas City MO 64134

(EDIT) I know the spacing on this is an eyesore and I'm trying to find the fix to it, sorry!


I'm not sure if that gives any usefulness on the situation but I just want to make it clear as possible for any takers on this.. Anyway, I thank you for the read and any assistance you can provide!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void	listRecords()
{

 	ifstream iInput;
	ofstream showMe;
	string inputLine;



	while (!iInput.eof())
	{
		showMe.open("MailList.txt",ios::app);
		showMe << inputLine.substr(0, 10) << " " 
                << inputLine.substr(4,24) << endl;
		showMe.close();

		getline(iInput, inputLine);
		cout << inputLine << endl;
		
	}

}
Last edited on
Try this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

void	listRecords()
{
        ifstream showMe("MailList.txt") ;
	string inputLine;

	if( !showMe )
	{
		cout << "Failed to open the file \n" ;
		 exit(1) ;
	}


	while (!showMe.eof())
	{
		getline(showMe , inputLine);
		cout << inputLine << endl;
	}

	showMe.close() ;

}
Wow that worked great! If you wouldn't mind could you possibly walk me through what I messed up on (which may be asking a LOT haha) or what I needed to do differently? If not, either way, thank you for your assistance and I really appreciate it!
Ok...

You want to open a file to read from and output to the screen ..
1
2
3
4
 ifstream showMe("MailList.txt") ; //Opening a file for reading
string inputLine; //for reading from the file



Then we want have to check if opening the file is successful
1
2
3
4
5
if( !showMe )
	{
		cout << "Failed to open the file \n" ;
		 exit(1) ;
	}


Then outputting the content of the file to the screen..
1
2
3
4
5
while (!showMe.eof())
	{
		getline(showMe , inputLine);
		cout << inputLine << endl;
	}


Finally we must close the file..
showMe.close()

As for mistakes in your code ..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void	listRecords()
{

 	ifstream iInput;
	ofstream showMe;  // Why this ?  You only need to display data .. there is no need for it
	string inputLine;



	while (!iInput.eof()) // You haven't opened (iInput) yet 
	{
		showMe.open("MailList.txt",ios::app);           // no need 
		showMe << inputLine.substr(0, 10) << " "    // for those
                << inputLine.substr(4,24) << endl;               // lines
		showMe.close();                                            //

		getline(iInput, inputLine); //  Also you haven't opened (iInput) yet
		cout << inputLine << endl;
		
	}

}






This is extremely helpful, thank you so much!!
Topic archived. No new replies allowed.