diff --git a/DisplayController.cpp b/DisplayController.cpp index 3844453..0a24581 100644 --- a/DisplayController.cpp +++ b/DisplayController.cpp @@ -6,27 +6,40 @@ All rights reserved. // Migrated to iostream because i really did not want to manually handle char buffers. #include #include +#include #include #include "DisplayController.h" using std::cin; using std::cout; using std::endl; -using std::ws; // I will try to use the "Single Responsibility Principle" here as much as i can. int DisplayController::readInput(int min, int max) { int userInput = 0; - scanf("%d", &userInput); - while (userInput < min || userInput > max) + while (true) { - cout << "Invalid input, please try again: "; cin >> userInput; + if (cin.fail()) + { + cin.clear(); + cin.ignore(std::numeric_limits::max(), '\n'); + logger("error", "Invalid input, please try again"); + } + else + { + if (userInput < min || userInput > max) + { + logger("error", "Invalid input, please try again"); + } + else + { + cin.ignore(std::numeric_limits::max(), '\n'); + return userInput; + } + } } - cin.ignore(); - - return userInput; } template T DisplayController::readOptionalInput(T defaultValue) diff --git a/main.cpp b/main.cpp index 9ec8634..fed6164 100644 --- a/main.cpp +++ b/main.cpp @@ -3,9 +3,8 @@ This source code is provided only for reference purposes. This is not a public d All rights reserved. */ -#include -#include #include +#include #include "ContactController.h" #include "DisplayController.h"