2D char array input using for loop. ?!

Having Problem in 2D Char Array Input ..
Major Problem in the function "fetch_name",
Please Have an eye . . . .


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
include <iostream>
#include <string>

using namespace std;

struct student
{
	char name[5][10];
	int id[5];
};


void fetch_id(student& s)
{
	for (int i = 0; i < 5; i++)
	{
		cout << "roll no: ";
		cin >> s.id[i];
	}
	
}
void fetch_name(student& s)
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 5; j++)
		{
			cout <<"name: ";	
			cin.getline(s.name[i], 20);
		}
	}
}

void display_name(student s)
{
	for (int i = 0; i < 5; i++)
	{

		for (int j = 0; j < 5; j++)
		{
			
			if ( s.name[i][j] != '\0' )
			cout << s.name[i][j];
		}
	cout << "\n";
	}
}

void display_id(student s)
{
	cout << "Roll Numbers Are" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << s.id[i] << " || ";
	}
}

int main()
{
	student s;
	fetch_id(s);
	display_id(s);
	cout << '\n';
	fetch_name(s);
	display_name(s);
	system("Pause");
	return 0;
}
Change the fetch_name function to iterate over every of the 10 elements, 5 times, for each time you .push_back(element); into an std::string object.

Start with this skeleton:
1
2
3
4
5
for (int i = 0; i < 5; i++)
    for (int j = 0; j < 10; j++)
    {
        // Put reading code here.
    }
Ok UpDated cOde:

1
2
3
4
5
6
7
8
9
10
11
void fetch_name(student& s)
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			cout <<"name: ";	
			cin.getline(s.name[i], 20);
		}
	}
}


Here problem is: loop never terminates. . .
Topic archived. No new replies allowed.