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,
9 Answers
+ 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);
+ 5
Okay, good luck)
+ 1
Are you getting any compiler errors?
+ 1
And the names are read without spaces? Or where exactly don't you have spaces?
+ 1
Martin Taylor Maybe these strange arrays are dangling pointers? I don't know, too.
0
no
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
0
I'm still learning the language am not good at vectors and pointers.