0
Array doesn’t let me write for the first
int sizen, sizec; //////////////////////////////////////////////////////////// cout<<"Enter the number of students "; cin>>sizen; string names[sizen]; for (int i=0; i < sizen ; i++) { cout<<"enter #"<<(i+1)<<"student's name ': "; getline(cin,names[i]);} I don’t know what is my problem, can any one help .
9 Antworten
+ 7
I'm not sure if this is right, but I don't think we're allowed to specify the size of an array at runtime. I think the array size has to be written into the code. Maybe have to use vector.
+ 1
The for loop is missing a curly bracket but other than that the code looks fine
+ 1
@Eric Zatarack is correct.
Variable Length Arrays are indeed not allowed in C++.
However some compilers, like gcc, has a compiler extension that does allow it. ( using -pedantic-errors will cause a compile error )
Code that does use this are not portable.
So it's indeed a better idea to go for a vector.
0
Sorry , even with the bracket , it doesn’t work
0
I don't know about the getline() function but you could simply use "cin >>"
like this:
int sizen, sizec;
cout<<"Enter the number of students ";
cin>>sizen;
string names[sizen];
for (int i=0; i < sizen ; i++) {
cout<<"\nenter #"<<(i+1)<<"student's name ': ";
cin >> names[i];
cout << "\n" << names[i];
}
0
Cin will not let me write with space
0
When I try and run it I get two errors:
..\Playground\: In function 'int main()':
..\Playground\:6:11: error: 'sizen' was not declared in this scope
cin>>sizen;
^~~~~
..\Playground\:6:11: note: suggested alternative: 'size_t'
cin>>sizen;
^~~~~
size_t
..\Playground\:11:22: error: 'names' was not declared in this scope
getline(cin,names[i]);
0
I post part of the program
0
If you use cin before getline you have to use a cin.ignore(); in between. cin leaves a newline in the stream which getline then reads and calls it a day.
https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction