inputting into a 2d char array

The assignment is to read from a file, put the first two words into a single row of a 2d char array, and the next two numbers into a separate array. My issue is that I cannot get more than one of the words into a row of the array at a time.

the code reads from a file that looks like the following:
1
2
3
4
5
6
7
8
9
10
Johnson Michael 60 12.50
Aniston Thomas 65 13.25
Cooper Union 50 14.50
Gupta Mary 70 14.75
Blair Smith 55 10.50
Clark Chris 40 18.75
Kennedy John 45 20.50
Bronson Keith 60 20.00
Sunny Paul 65 18.75
Smith Patrick 30 9.75


But when inputted, the first row stops at the word Johnson. Attempting to use getline again, it simply overwrites Johnson with Michael.

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
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
using namespace std;

const int rowsize = 10;

void read(char name[10][20], int data[10][3], int rowsize = 10)
{
	ifstream in;
	in.open("p1data.txt");
	int i,li;
	for (i = 1; i <= rowsize; i++)
	{
		in.getline(name[i - 1], 20, ' ');
		in.getline(name[i - 1], 20, ' ');
		cout << name[i - 1] << endl;
	}




}

int main()
{
	char name[rowsize][20] = {};
	int data[rowsize][3] = {};
	read(name, data, rowsize);
	system("pause");
	return 0;
}


Essentially, I want to read "Johnson Michael" into the first row of the array, and follow by putting the two numbers into a second array, which I can handle with in << data[i-1][0] << data[i-1][1];
Is there a way to skip the first delimiter (the space) yet stop at the second?
Last edited on
Is there a reason you're using C-strings instead of std::string?

Why is your for() loop starting at 1 and ending at rowsize? It should start a zero and end at rowsize - 1. for(i = 0; i < rowsize; ++i)

But when inputted, the first row stops at the word Johnson. Attempting to use getline again, it simply overwrites Johnson with Michael.

You probably want to use two different variables and combine them after the getlines.
No real reason, and the for loop is mostly by preference. I tried using two different strings to get the first and last names, but then combining them into ONE row of the character array proved to be troublesome.
and the for loop is mostly by preference.

You need to get used to arrays being zero based, otherwise you'll start having major problems. Such as forgetting to subtract 1 from the index value every time you try to access the array in the loop.

I tried using two different strings to get the first and last names, but then combining them into ONE row of the character array proved to be troublesome.

That's probably because you're using C-strings instead of std::strings. You need to use one of the functions in <cstring> to combine C-strings. With std::strings you can use the assignment operators.

Got it! I used your idea of using two separate strings, and then throwing them together with an operand, then using strcopy_s to place them into the row of the 2d char array, like so.

1
2
3
4
5
6
7
8
9
10
for (i = 1; i <= rowsize; i++)
	{
		in >> first >> last >> data[10][0] >> data[10][1];

		first = first + " " + last;

		strcpy_s(name[i-1], first.c_str());

		cout << name[i-1] << endl;
	}


I will focus on representing my arrays more as zero based, though. And I think the only way these strings worked is because i #include'd <string>.
I suggest you stop using C-strings and stick with std::string:
1
2
3
4
5
6
7
8
9
10
11
12
std::string first, last;
string name[10];
double data[10][2];
...
for(int i = 0; i < rowsize; ++i)
{
    in >> first >> last >> data[i][0] >> data[i][1]
    name[i] = first + " " + last; 
    cout << name[i];
}

...


Also consider using std::vector instead of the arrays.

Edit: You will also need to #include <cstring> if you want to use strcpy().


Last edited on
Topic archived. No new replies allowed.