Remove comma when read in text file

Sep 11, 2019 at 2:23pm
closed account (jEb91hU5)
Hi. I’m reposting on here because I’m having a bit of trouble understanding some help I was given on the beginners forum. I'm having trouble being able to read in my input data to my code function from a file. I have it organized: last, first ID. I need to be able to later output it: ID First Last. I need to be able to erase the comma. I'm not quite sure what to put in my while loop. I think I'm supposed to use pos = str.find(',') and str.erase(pos, n) . Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
  void readem()
{
	int i;
	ifstream inf("demo.dat");
	i = 0;
	while (!inf.eof())
	{
		//What goes here?
	}
}




Here are some test names:


Cruz, Terry 340
Temple, Shirley 112
Armstrong, Lance 725
Fey, Tina 202
Last edited on Sep 11, 2019 at 2:55pm
Sep 11, 2019 at 2:46pm
I'm not sure what you want, given that you've basically posted the same question again.
http://www.cplusplus.com/forum/beginner/262181/

At least TRY to put something inside your while loop that demonstrates your level of skill.

And you're still using eof wrong.

This would be a start.
1
2
while ( inf >> last >> first >> id ) {
}

Then maybe your question would be 'how to remove a comma?'
Sep 11, 2019 at 2:54pm
closed account (jEb91hU5)
Yes. “How to remove a comma” is my question. I forgot to explicitly write that in. I know how to read in a data set without the comma—Being exactly what you did.
Sep 11, 2019 at 3:01pm
Well now is the time to have a not-so-quick read through of all the features that std::string provides.
http://www.cplusplus.com/reference/string/string/

The aim is not to memorise every last detail, but enough to get a sense of the "lay of the land".

So whilst you might not remember the detail (that's what manual pages are for), you'll recall enough to remember that there was something that could be of use.

Along the way, you'll figure out a way to do what you want.
Sep 11, 2019 at 3:15pm
By the way it would probably be easier if all the fields were separated by the "comma" instead of just the names.

1
2
3
4
Cruz,Terry,340
Temple,Shirley,112
Armstrong,Lance,725
Fey,Tina,202


This way if any of the names are "multi-word" it will be easier to process them.

Last edited on Sep 11, 2019 at 3:15pm
Sep 11, 2019 at 4:15pm
/me suspects the comma is there to make the OP think a bit, rather than being a serious file format.
Sep 12, 2019 at 8:38am
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


void readem( istream &in )
{
   string firstname, lastname, id;
   while( getline( in >> ws, lastname, ',' ) && in >> firstname && getline( in >> ws, id ) )
   {
      cout << id << " " << firstname << " " << lastname << '\n';
   }
}


int main()
{
// ifstream in( "demo.dat" );
   stringstream in( "Cruz, Terry 340\n"
                    "Temple, Shirley 112\n"
                    "Armstrong, Lance 725\n"
                    "Fey, Tina 202\n" );

   readem( in );
}
340 Terry Cruz
112 Shirley Temple
725 Lance Armstrong
202 Tina Fey



You could also do
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;


void readem( istream &in )
{
   string firstname, lastname, id;
   while( in >> lastname >> firstname >> id )
   {
      lastname.resize( lastname.size() - 1 );
      cout << id << " " << firstname << " " << lastname << '\n';
   }
}


int main()
{
// ifstream in( "demo.dat" );
   stringstream in( "Cruz, Terry 340\n"
                    "Temple, Shirley 112\n"
                    "Armstrong, Lance 725\n"
                    "Fey, Tina 202\n" );

   readem( in );
}
Last edited on Sep 13, 2019 at 8:53am
Topic archived. No new replies allowed.