Well, i'm really new into programming, and I'm not a native english sepaker, so please, if you dont't get what I'm saying don't get mad :)

What I'm trying to do is this:

int i, numberOfStudents
string studentName[100];

cout<<"Input the number of students"<<endl;

cout<<"Input the student's names"<<endl;    
    for (i=0; i<numberOfStudents; i[CODE][/CODE]++) {

        printf("Student %d: ",i+1);


NOTE: I've translated the code from its original language to English, so if you see any silly syntax mistake, is probably just a typo.

Now, what I wanna do is to be able to input the complete students name, by this I mean name and last name, but when I do so, it takes the value for student 1 and student 2, instead of just one student

BTW, also when i declare the studentName variable, you may notice it is inicialized in a 100 value, i did that 'cause I don't know how to make it whit my numberOfStudents variable. In other words, WHAT I HAVE IS THIS:

string studentName[100];

And what I want is this:

string studentName[numberOfStudents];

Edited by mike_2000_17: Fixed formatting

6 Years
Discussion Span
Last Post by WaltP

To your first question:
cin by default 'splits' on the space character. So say:

string test;
cin >> test;

If the user enters "one two", test will be equal to "one".

My original 'easy' way was plain dirty as thankfully pointed out by mike_2000_17, use his solution as an easy way out (I'm not sure how getline slipped my mind ;))

The nice way, however is to create a class 'Student' and overload the >> operator, but for now the above should do fine.

To your second question, in C++ to create arrays that don't have a fixed length on compile time you need to create (and delete!) them dynamically.

Look up on google for 'new' and 'delete':

string* studentName = new string[ numberOfStudents ];
// use studenetName here
delete[] studentName; // delete studentName

Edited by thelamb: n/a


>>The nice way, however is to create a class 'Student' and overload the >> operator, but for now the above should do fine.

And the _easy_ way is to use std::getline() from the <string> header. As so:

string fullName;

that will capture everything until the return key is hit.

For your second question, just use std::vector instead of an array, as so:

vector<string> studentName(numberOfStudents);

You can access the elements just like you do for a static array.

Edited by mike_2000_17: n/a

Votes + Comments
Not really sure how that slipped my mind... thanks

Well, i'm really new into programming...

Which is why I'll suggest the 'beginners' way for your 2nd question.

1) vectors -- beginners never learn vectors early on. Use arrays as you've done.
2) new/delete -- generally also beyond the beginner's grasp.

Your solution to use studentName[100] was the correct solution for your level.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.