It appears that you're not processing one of the numbers. Doesn't your file consist of the "name" followed by four numbers? If so you're only processing three numbers and the name.
You really should be using a structure to hold the data from each line (and array of the struct).
What do each of those "numbers" represent?
By the way the following is not allowed in C++ programs. Array sizes must be compile time constants.
1 2
int n=strlen(name[0].c_str());
char word[3][n+1];
And why are you trying to convert the string to a horrible C-string in the first place?
Actually I am practicing to make my program separate the 9-digit ID from the numbers while reading from txt file. After that, I want to convert the ID which is a string into char so that I can use the individual characters from the 9-digit ID.
Your code and description don't match.
According to your desired output you want only the first column and ignore the rest but you still try to read all the rest.
To read only the first column you could do it like this;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
ifstream src("input.txt");
if (!src)
{
cerr << "File error" << "\n";
return 1;
}
string line;
string name;
while (getline(src, line))
{
auto pos = line.find('\t');
if (pos != string::npos)
{
name = line.substr(0, pos);
cout << name << "\n";
}
}
You should use strings mostly, but be aware that there are a small # of things string can't do cleanly ... injection of an end of string is one of them.
Why shouldn't it work with a string?
1 2 3
string input = "The agile dog jumps over the lazy fox.";
input.resize(13);
cout << input; // output = The agile dog