ERROR HELP !!

No matter what i try i keep getting an error.

class student
{
public:
char name[];
int age;
};
int main()
{
student q;
q.name[]="Jim"; // This is the line with the error
q.age=19;
cout<<q.name<<q.age;
getch();
return 0;
}

The error is as follows:

12 D:\Trial Programs\Chumma.cpp expected primary-expression before ']' token
The first thing is that, unless im mistaken, the "[]" at the end of "name[]" makes it an array, of an undefined size and the error is asking you to say which number in the array it is. i just put "1" inside of both braces(? not sure if they are braces or brackets?) it accept that, but it also doesnt accept "Jim" as a char for a reason im not even going to pretend i know enough to explain, so i put #include <string> at the top, and made "name" a string instead of a char and it worked just fine after that. heres what i came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
class student
{
public: 
	std::string name[1];
	int age;
};
int main()
{
student q;
q.name[1]="Jim"; // Error fixed.
q.age=19;
std::cout<<q.name<<q.age;
std::cin.ignore();
return 0;
}
Thanks for the tip. But in the same code i posted i gave 10 as the array size and this is the error after tat :

12 D:\Trial Programs\Chumma.cpp invalid conversion from `const char*' to `char'
that is the error that i dont know enough to explain, so i switched it from a "char" variable to a "string". also you wouldnt need an array of 10 for this, that would create 10 "slots" for names to be put in e.g.
1
2
3
4
5
6
string name[10]; //Makes a string array 10 strings big.

name[1]="tom";
name[2]="dick";
name[3]="larry";
etc...


also note i made a few changes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string> //added this library to get the string keyword
class student
{
public: 
	std::string name[1]; //makes a string array 1 string long. 
	int age;
};
int main()
{
student q;
q.name[1]="Jim"; // Error fixed.
q.age=19;
std::cout<<q.name<<q.age;
std::cin.ignore();
return 0;
}


now im not saying that this is the only way that it can be done, but its just the only answer to the problem that i know.
Topic archived. No new replies allowed.