クラスのメモリ管理と、new
演算子で動的にメモリを割り当てる方法を学ばなければなりません。構造体のポインタ配列を持つgetline
私は
struct Course
{
int courseNumber, creditHours;
string courseName;
char grade;
};
である構造体を持っている私は、forループを持つメンバ変数を埋めるためにしようとしていますが、私はcourseName
でgetline
を使用する方法がわからないと思います。私は通常のcin
を使用することができましたが、クラス名にスペースが含まれていると動作しません。
以下は私のコードですが、私が試したことはありますが、courseArrayが定義されていないという引数エラーが発生します。
Course* readCourseArray(int &courses) //Read Courses
{
cout<<"\nHow many courses is the student taking?\n";
cin>>courses;
const int *sizePTR = &courses;
Course *coursePTR = new Course[*sizePTR];
for(int count = 0; count < *sizePTR; count++) //Enter course information
{
cout<<"\nEnter student "<<count+1<<"'s course name\n";
getline(cin,courseArray[count].courseName);
cout<<"\nEnter student "<<count+1<<"'s course number\n";
cin>>coursePTR[count].courseNumber;
cout<<"\nEnter student "<<count+1<<"'s credit hours\n";
cin>>coursePTR[count].creditHours;
cout<<"\nEnter student "<<count+1<<"'s grade\n";
cin>>coursePTR[count].grade;
}
return coursePTR;
}
の戻り値を記憶している願っていますか? – Nawaz
ああ私はまだポインタやものを渡すことと少し混乱しています。intからconst int値を設定する際に問題があると思いますが、私は覚えていません。それはしかし、動作します。 – sircrisp
入力が同期していないことが分かった場合、 'std :: cin.ignore(std :: numeric_limits :: max()、 '\ n');ループ内の最後の行 –