Usually you would use std::getline(std::string&,std::istream&)
for line orientated input and parse the number with an istringstream
. However, since you aren't allowed to use neither std::string
nor std::stringstream
you need to parse the integer yourself.
But first the explanation for your errors.
Explanation
Something is not working correctly though as I am either unable to input into name field
When you enter a correct integer the newline token \n
is not extracted from the string, that's why cin.getline(student.name, 50);
won't extract anything: the next character in std::cin
is \n
, the line ends and the std::getline
fills student.name
with the extractet line, which is empty.
or I get the remaining decimal as the name (if I enter 123.6, the name will be .6).
Again, this is the same: you only extract the integer, but you don't extract the rest of the line.
Temporary solution
Ignore the rest of the line in your validInt
:
bool validInt(int& val)
{
bool valid = true;
if(!(cin >> val))
{
cout << "\nERROR: Please enter a Positive Whole Number" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
valid = false;
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return valid;
}
Correct solution
The correct solution is a little bit more elaborate, since you would have to write something that extracts single characters, checks whether they are digits (or a minus or plus), saves those as integer and ignores the rest of the line. Don't worry, this is possible with only <iostream>