xcode - How to initialize array with object in c++ by user input? -
i have code below, credit , load saves object code , title takes first letter of string. tried course[noofcourses_].setcode(code)
@ first, gives me error:
cannot initialize parameter type char lvalue char[7]
.
another problem input skips input prompt title , goes directly credit prompt.
void addcourse() { course*course=new course[max_no_recs]; char code[max_coursecode_len]; char title[20]; int credit; int load; cout << "course code: "; cin.clear(); cin >> code; cin.clear(); course[noofcourses_].setcode(code[0]); cin.clear(); cout << "course title: "; cin.getline(title, 20); cin.clear(); course[noofcourses_].settitle(title[0]); cin.clear(); cout << "credits: "; cin >> credit; course[noofcourses_].setcredits(credit); cout << "study load: "; cin >> load; course[noofcourses_].setload(load); noofcourses_++; courselist_[noofcourses_]=course; }
your first way right. should use course[noofcourses_].setcode(code)
(by way: better if variable code
not start capital letter).
the problem in cource::setcode
function. seems parameter char
. fix it, should change char*
void cource::setcode(char* code) { ... }
Comments
Post a Comment