0

How to read string variable with spaces in an array?

int stuS,courseS; cout<<" stu:"; cin>>stuS; cout<<"Course :"; cin>>courseS; string course [courseS],names[stuS]; double grade[courseS][stuS]; cout<<"course"; for(int i=0;i<courseS;i++) { cin>>course[i]; } cout<<"names:"; for(int i=0;i<stuS;i++) { getline(cin,names[i]); } cout<<"grades"; for(int i=0;i<courseS;i++) { for(int j=0;j<stuS;j++) cin>>grade[i][j]; } I've included <cstring> & <string> ,but it didn't work,

11th Apr 2018, 3:46 PM
Sara
Sara - avatar
9 Respuestas
+ 5
Creating an array of non-constant size is ambitious. Could you try to use pointer instead? Or vector header. // pointer ex. string* names = new string[stuS]; // vector ex. vector<string> names (stuS);
11th Apr 2018, 5:54 PM
Rull Deef 🐺
Rull Deef 🐺 - avatar
+ 5
Okay, good luck)
11th Apr 2018, 6:05 PM
Rull Deef 🐺
Rull Deef 🐺 - avatar
+ 1
Are you getting any compiler errors?
11th Apr 2018, 3:54 PM
Timon Paßlick
+ 1
And the names are read without spaces? Or where exactly don't you have spaces?
11th Apr 2018, 4:44 PM
Timon Paßlick
+ 1
Martin Taylor Maybe these strange arrays are dangling pointers? I don't know, too.
12th Apr 2018, 5:04 AM
Timon Paßlick
0
no
11th Apr 2018, 4:16 PM
Sara
Sara - avatar
0
stu: 2 Course :3 course cis cs math names: sara Ahmad grades this is the output - Sara Ahmad is suppose to be the name of one person
11th Apr 2018, 6:00 PM
Sara
Sara - avatar
0
I'm still learning the language am not good at vectors and pointers.
11th Apr 2018, 6:03 PM
Sara
Sara - avatar