need hwlp with loop and class

l am trying to enter name by user choice and trying to display that but l'm making a mistake, can anyone help me out please.

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
  #include<iostream>
using namespace std;
class Test{
	char name[30];
	public:
		void get();
		void show();
		
	
};
int num;
Test t;
void Test::get()
{
	cout<<"HOw many names you want to enter?\n";
	cin>>num;
	for(int i=0;i<num;i++)
	{
	
	cout<<"Enter name: ";
	cin>>t[i].name;
	
}

}
void Test::show()
{
	cout<<t[i].name;
}

int main()
{
	Test T1;
	T1.get();
	T1.show();
}
Look at your compiler's messages.

One compiler says:
 In member function 'void Test::get()':
21:8: error: no match for 'operator[]' (operand types are 'Test' and 'int')
 In member function 'void Test::show()':
28:10: error: 'i' was not declared in this scope


Line 21 does not read into "this". It attempts to read into global object 't'. Why?

Line 28 attempts to access same global 't', and has name 'i' that we know nothing about.


Why is the 'name' an array or char? You would probably be better off with single std::string.
Last edited on
can you write code please, i dont understand what are you saying
Lets try another approach.

Please explain what you do on lines 11, 12, 21, and 28. What is the purpose of each of them?
like we run array elements using loops lm implementing that idea here, can you please give me a rough sketch of this program?
Topic archived. No new replies allowed.