-
input validation, kinda
Hey there,
i have this following code i'd like some feedback from if possible(comments, critism, tips)
I'm a newb with C++ and currently trying to grasp the concepts of OOP
two files, main.cpp and a header file:
source file:
Code:
/*
* File: main.cpp
* Author: joe
*
* Created on September 15, 2008, 4:44 PM
*/
#include "scoreLib.h"
int main()
{
float input;
std::cout << "Please enter a score: ";
if(std::cin >> input)
{
Score student(input);
student.gradeResult();
std::cout << "Your Score is: " << input << std::endl;
}
else
{
std::cout << "Please enter a numeric value" << std::endl;
}
return 0;
}
header file:
Code:
/*
* File: scoreLib.h
* Author: joe
*
* Created on September 15, 2008, 4:44 PM
*/
#include <iostream>
#ifndef _SCORELIB_H
#define _SCORELIB_H
class Score
{
float *numGrade;
public:
Score(float);
~Score();
void gradeResult(){
if((*numGrade >= 95) && (*numGrade <= 100))
{
std::cout << "Merit!\n";
}
else if((*numGrade >= 80) && (*numGrade <= 94))
{
std::cout << "Pass!\n";
}
else if((*numGrade >= 65) && (*numGrade <=79))
{
std::cout << "Partial Resit\n";
}
else if((*numGrade >= 50) && (*numGrade <= 64))
{
std::cout << "Full Resit\n";
}
else if((*numGrade >= 0) && (*numGrade <= 49))
{
std::cout << "Fail!\n";
}
else
{
std::cout << "Sorry enter a number between 1 and 100\n";
}
}
};
Score::Score(float defaultScore)
{
numGrade = new float;
*numGrade = defaultScore;
}
Score::~Score()
{
delete numGrade;
}
#endif /* _SCORELIB_H */
I'm currently trying to fix this bug where by if you entered say for instance 3e3, it will still exectute:
Code:
std::cout << "Your Score is: " << input << std::endl;
which it shouldnt, but am having some difficulty trying to figure out why it does this, help MUCH appreciated, thanks in advance
-
I prefer the following technique which you can adapt to your program:
Code:
while (!(std::cin >> value) || std::cin.get() != '\n')
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// error message and re-prompt here
}
Notes:- You need to #include <limits> for numeric_limits and <ios> (I think) for streamsize.
- Instead of std::numeric_limits<std::streamsize>::max(), you can use some large number instead (like 1000). That value is the number of bad characters to ignore, so as long as nobody types in more than 1000 bad characters it should be fine.
- This version includes the check for std::cin.get() != '\n'. This is necessary for your example of 3e3, since the std::cin >> value code happily reads in the 3, but the cin.get() call finds that there is garbage after the input which makes the whole thing invalid.
- The std::cin.get() also has the benefit of clearing the trailing newline, so that if you use this technique for all cin >> input and were also using getline, you wouldn't need the extra ignore() call.
- Putting that whole thing into a separate utility function is not a bad idea (although it's unnecessary for you right now): http://cboard.cprogramming.com/showp...4&postcount=24