I'm currently using MS visual C++, learning to do my assignments.
i need to prompt user to enter his name, index number and gender using 2D array. i guess i need to use cin.getline, but i got an error C2664, cannot convert parameter.
so, how do i create 2D array with 3 row and 4 column and should store 30 words in 1st column, 10 in 2nd and 1 in 3rd?
please.. thx~
How do I create a two-dimension array to store student's name, index number, gender in C++?
Well the problem is that you probably want to store their name as a string, but their index number as an integer, and a gender as an enum/boolean. You could either maintain 3 separate arrays (1 for each name, index, gender) and then update all three individually, but that is a pain, the best way to do it is to create a class or structure as a student, since its C++, probably a class.
Class Student{
public:
Student();
Student( String name, int indexNumber, bool gender );
getName();
getIndex();
getGender();
private:
String name;
int indexNumber;
bool gender;
}
Then you just create an array of Students and you can access each students information via the get methods.
Student listofstudents = new Student[100];
listofstudents[41].getName() is the 41st students name
The better way to do gender is to create an enum called gender with male and female entries, but you can use a bool such that male is true and female is false or vice versa.
Make sure that when you get the input as a user you parse it into the correct type, so when you get the age you need to make it into an int.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment