c++ - classList array wont print output to the user -
so point of program ask student first name, how many classes they're taking semester, , asks names of each of classes. problem can't classlist
array work , display class names user in program output. i've marked think issue coming , think super easy fix i'm not seeing it. help?
i'm coding in microsoft visual studio 2013 way.
#include <iostream> #include <string> using namespace std; class student { public: //public class student student(string first_name, string last_name, int num, string list); student(); void input(); //input user void output(); //output user depending on input // accessors const string get_name(); const int get_numclasses(); const string get_classlist(); // mutators void set_name(string name); void set_numclasses(int num); void set_classlist(string list); private: string name; int numclasses; string classlist; }; // constructor's variable student::student() { name; numclasses = 0; classlist; } // accessors called public class student string const student::get_name() { return name; } int const student::get_numclasses() { return numclasses; } string const student::get_classlist() { return classlist; } // mutators set public class student void student::set_name(string studentname) { name = studentname; } void student::set_numclasses(int num) { numclasses = num; } void student::set_classlist(string list) { classlist = list; } // accessors here void student::input() { int x; cout << "enter name (first name only): "; cin >> name; cin.ignore(); cout << "how many classes taking semester?: "; cin >> numclasses; cin.ignore(); if (numclasses> 0) { string* classlist = new std::string[numclasses]; // <--- error on line think (x = 0; x<numclasses; x++) // loop classlist array { cout << "enter name of class " << (x + 1) << endl; cin >> classlist[x]; // <--- or maybe error on line too? } } cout << '\n' << endl; } // displays output according user void student::output() { cout << "students name: " << name << endl; cout << "number of classes semester?: " << numclasses << endl; cout << "displayed class list: " << classlist << endl; } int main() { char go_again = 'y'; while (go_again == 'y' || go_again == 'y') { student s1; s1.input(); s1.output(); //output student cout << "\nrestart new data? (press y/n) " << endl; cin >> go_again; } if (go_again == 'n') { return 0; } system("pause"); return 0; }
your student
class declares string
named classlist
, input()
method declares new variable of type string*
, named classlist
. it's called shadowing, and, while legal, avoided, kind of problems seeing. unintended , confusing.
Comments
Post a Comment