Array doesn’t let me write for the first | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 .

27th Mar 2018, 4:10 PM
Alhanoof Aldossary
Alhanoof Aldossary - avatar
9 Answers
+ 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.
27th Mar 2018, 9:51 PM
Eric Zatarack
Eric Zatarack - avatar
+ 1
The for loop is missing a curly bracket but other than that the code looks fine
27th Mar 2018, 4:19 PM
TurtleShell
TurtleShell - avatar
+ 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.
27th Mar 2018, 10:05 PM
Dennis
Dennis - avatar
0
Sorry , even with the bracket , it doesn’t work
27th Mar 2018, 4:21 PM
Alhanoof Aldossary
Alhanoof Aldossary - avatar
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]; }
27th Mar 2018, 4:36 PM
Type
Type - avatar
0
Cin will not let me write with space
27th Mar 2018, 4:37 PM
Alhanoof Aldossary
Alhanoof Aldossary - avatar
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]);
27th Mar 2018, 4:39 PM
Adam
Adam - avatar
0
I post part of the program
27th Mar 2018, 4:40 PM
Alhanoof Aldossary
Alhanoof Aldossary - avatar
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
27th Mar 2018, 4:58 PM
Dennis
Dennis - avatar