Convert string to char. No desired output

I tried to convert strings which are read from external input file into char however I didn't get my desired result...

My desired output
1
2
3
CS0900001
CS1000002
CS1188888


What I got
1
2
CS0900001
97


Below is my txt file

1
2
3
CS0900001	12	45	87	97		
CS1000002	11	45	32	64
CS1188888	14	46	89	67


Below is my code

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
int main()
{
	ifstream indata;
	indata.open("input.txt");
	
	string nm;
	string name[100];
	int cum1[100],cum2[100],cum3[100];
	
	int c1,c2,c3;
	int num=0;
	
	while(getline (indata, nm, '\t'))
	{
		indata>>c1; indata.ignore();
		indata>>c2; indata.ignore();
		indata>>c3; indata.ignore();
		
		name[num]=nm;
		cum1[num]=c1;
		cum2[num]=c2;
		cum3[num]=c3;
		
		num++;
	}

	int n=strlen(name[0].c_str());
	char word[3][n+1];
	
	for(int i=0; i<3; i++)
	{
	
		strcpy(word[i], name[i].c_str());
		
	}
	
	for(int i=0; i<3; i++)
	{
		//in char
		cout<<word[i]<<endl;
	}
			
	return 0;
	
}


Can someone help me with my code. Any help will be appreciated.
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.

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.

You don't need to convert the string into a C-string to use the individual characters, just stick with the C++ string.

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";
    }
  }

Output

CS0900001
CS1000002
CS1188888

Last edited on
Removal of bad C code idea. See below for better way!
Last edited on
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 
That works. Use that instead!
Topic archived. No new replies allowed.