Please someone clarify the issue I am having right now with user input in C++ [VS 2015].
I have a simple class StudentEntry
const int MAX_STUDENT = 50;
int entryCount = -1;
class StudentEntry {
public:
StudentEntry(const std::string &s, std::vector<int> li) : name(s), marks(li) {}
private:
std::string name;
std::vector<int> marks;
}
* entryList[MAX_STUDENT];
Two non-member functions ask user to input name [string
] and marks [vector<int>
]:
std::string getName() {
std::string input;
std::cout << "Enter student name: ";
std::cin >> input;
return input;
}
std::vector<int> getMarks() {
std::string line;
int number;
std::vector<int> input;
std::cout << "Enter student marks separated by spaces: ";
getline(std::cin, line);
std::istringstream ss(line);
while (ss >> number) {
input.push_back(number);
}
return input;
}
My goal is to add a new entry using a function like addRecord()
. What I plan to do, is:
int main()
{
std::string in_name = getName();
std::vector<int> in_marks = getMarks();
entryList[entryCount] = new StudentEntry(in_name, in_marks);
...
}
However, I can't read a vector from user input. Basically, if I comment the line
std::string in_name = getName();
I can enter some marks and get them saved into in_marks
. But after I read the in_name
, the in_marks
vector is not being read from the prompt.
Why is this happening?