Removing newline in array of string

Hello.



First off, i have the .txt file that reads like this:

Abraham Jackson, 10, 15, 10, 13, 8, 18,
Joe Harrier, 13, 4, 5, 27, 12, 14,
Thomas High, 21, 2, 4, 15, 7, 3,
Jeffrey John, 4, 9, 8, 5, 27, 12,
Jason Smith, 3, 25, 8, 7, 4, 13,
$


My codes( reading data from .txt file and storing them into arrays):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
	
	const int studentsSIZE = 5; 
	string students[studentsSIZE]; 
	string stringscores[30];
        ifstream inputFile;
	inputFile.open("data.txt"); //OPEN THE .TXT FILE
	int k = 0;
	for (int i = 0; i < 5; i++) //READ AND STORE DATAS 
	{
		(getline(inputFile, students[i], ',' )); 
		{
			(getline(inputFile, stringscores[k], ',')); 
			k++;
		}
	}


So far, I've got it to read the .txt file and stored all the names into the array(students) as intended.

My question is how do I remove the \n in the array.

Currently the array is storing the datas like this:

{ "Abraham Jackson", "\nJoe Harrier", "\nThomas High", "\nJeffrey John", "\nJason Smith" }

Picture link: https://i.imgur.com/iDvTRpa.png

I have tried by putting inputFile.ignore() between line 14 and 15 in order to remove the \n, but doing so caused program to remove the very first letter 'A' that it will read from the text file.

{ "braham Jackson", "Joe Harrier", "Thomas High", "Jeffrey John", "Jason Smith" }







Last edited on
By using std::ws before calling std::getline you can get rid of all whitespace characters (including newlines) at the front.

http://en.cppreference.com/w/cpp/io/manip/ws
Topic archived. No new replies allowed.