diff --git a/intValidCheck.cpp b/intValidCheck.cpp deleted file mode 100644 index 6a5e935..0000000 --- a/intValidCheck.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/**************************************************************************************************** - * * Program name: CS162 Project1 - * * Author: Taekyoung Kim - * * Date: 01/10/2019 - * * Description: This is intValidCheck.cpp file for CS162 project1. - * * This function check users' input validation, so if the input is not integer, keeps asking users - * * to input the correct input. - ******************************************************************************************************/ - -#include "intValidCheck.h" -#include - -int intValidCheck(int input) { - - // Check type validation first using std::cin.fail() - - if(std::cin.fail()) { - - do { - //std::cout << "Input" << input << std::endl; - std::cout << "Wrong input! You need to input proper value!" << std::endl; - - std::cin.clear(); - std::cin.ignore(INT_MAX, '\n'); - - std::cout << "Please try again. Input here: " << std::endl; - std::cin >> input; - - } while(std::cin.fail()); - } - else { - return input; - } - return input; -} \ No newline at end of file diff --git a/main.cpp b/main.cpp index ef44c08..1821c48 100644 --- a/main.cpp +++ b/main.cpp @@ -8,31 +8,11 @@ #include #include +#include +#include #include "intValidCheck.h" #include "AntStep.h" - - -//function positNumCheck checks if the input is positive number. -int positNumCheck(int input){ - - if(input > 0) { - return input; - } - else { - do { - //std::cout << "Input" << input << std::endl; - std::cout << "Wrong input! You need to input positive integer!" << std::endl; - - std::cin.clear(); - std::cin.ignore(INT_MAX, '\n'); - - std::cout << "Please try again. Input here: " << std::endl; - std::cin >> input; - - } while(input<=0); - } - return input; -} +#include "validCh.h" //function inValidOneorTwo check if the input is 1 or 2. int inValidOneOrTwo(int input) { @@ -58,18 +38,18 @@ int inValidOneOrTwo(int input) { //Function to show users menus for choosing to quit or start and get user's choice. int chooseMenu1(){ - int choice; + std::string option; + int menu; std::cout<<"Welcome to Langton's Ant simulation. Choose your option."<>choice; + //std::cin>>choice; + std::getline(std::cin, option); + menu = validCh(option); + menu = inValidOneOrTwo(menu); - //Call input validation functions. - choice = intValidCheck(choice); - choice = inValidOneOrTwo(choice); - - switch(choice){ + switch(menu){ case 1: std::cout<<"You chose 1. Start the simulation. Let's start!"<> lengthCol; - std::cin >> widthRow; + std::cin.clear(); + std::cin.ignore(INT_MAX, '\n'); + std::cout <<"Please choose a board size, input positive integer number for length: "<< std::endl; + std::getline(std::cin, length); + lengthCol = validCh(length); - lengthCol = intValidCheck(lengthCol); - widthRow = intValidCheck(widthRow); - lengthCol = positNumCheck(lengthCol); - widthRow = positNumCheck(widthRow); + std::cout <<"Input number for width with a positive integer number: "<< std::endl; + std::getline(std::cin, width); + widthRow = validCh(width) //Ask user to input the number of steps of ant and call input validation function. - std::cout << "How many steps do you want the ant goes? Please input positive integer number for steps: "<< std::endl; - std::cin >> numOfStep; - numOfStep = intValidCheck(numOfStep); - numOfStep = positNumCheck(numOfStep); - - std::cout << "Do you want to choose a starting point for the ant or make it start at a random location?"<< + std::cout <<"How many steps do you want the ant goes? Please input positive integer number for steps: "<< std::endl; + std::getline(std::cin, steps); + numOfStep = validCh(steps); + + std::cout <<"Do you want to choose a starting point for the ant or make it start at a random location?"<< "\nIf you want a random location, input number 1. Or, if you want to choose it by yourself input 2.: " << std::endl; - std::cin >> choiceForRanNum; - //valid check for random choice - choiceForRanNum = intValidCheck(choiceForRanNum); - //one or check for random choice + std::getline(std::cin, choice); + choiceForRanNum =validCh(choice); choiceForRanNum = inValidOneOrTwo(choiceForRanNum); if (choiceForRanNum == 1) { @@ -133,26 +111,27 @@ int main() { } else { // choiceForRanNum == 2 - //Ask user to input the start point and then call the input validation function. - std::cout << "Please choose the start point, input starting Row and Column number: " << std::endl; - std::cin >> startRow; - std::cin >> startCol; - startRow = intValidCheck(startRow); - startCol = intValidCheck(startCol); - - if (!((startRow >= 0 && startRow < widthRow) && (startCol >= 0 && startCol < lengthCol))) { - while (!((startRow >= 0 && startRow < widthRow) && (startCol >= 0 && startCol < lengthCol))) { - std::cout << "Wrong input! You need to input between the board size" << std::endl; - - std::cin.clear(); - std::cin.ignore(INT_MAX, '\n'); - - std::cout << "Please try again. Input here: " << std::endl; - std::cin >> startRow; - std::cin >> startCol; - startRow = intValidCheck(startRow); - startCol = intValidCheck(startCol); + //Ask user to input the start point and then call the input validation function. + std::cout <<"Please choose a start point with positive numbers, input starting Row: " << std::endl; + std::getline(std::cin, userRow); + startRow = validCh(userRow)-1; + + std::cout <<"Input starting Column here: " << std::endl; + std::getline(std::cin, userCol); + startCol =validCh(userCol)-1; + + if (!((startRow < widthRow) && (startCol < lengthCol))){ + + while (!((startRow < widthRow) && (startCol < lengthCol))) { + std::cout <<"Wrong input! You need to input between the board size!"<< std::endl; + std::cout <<"Please try again. Input start Row: " << std::endl; + std::getline(std::cin, userRow); + startRow = validCh(userRow)-1; + + std::cout <<"Start Column number here: "< +#include +#include + +int validCh(std::string info) { + + + int num = 0; + bool cond = true; + + while (true) { + //std::cout << "Enter num: " << std::endl; + //getline(std::cin, info); + std::stringstream sstr(info); + if (sstr >> num && num >0) { + break; + } + + //std::cin.clear(); + //std::cin.ignore(INT_MAX, '\n'); + std::cout << "Invalid input! please try again" << std::endl; + getline(std::cin, info); + + + } + + return num; +} diff --git a/intValidCheck.h b/validCh.h similarity index 57% rename from intValidCheck.h rename to validCh.h index 9feb2b0..ccfe458 100644 --- a/intValidCheck.h +++ b/validCh.h @@ -2,12 +2,13 @@ * * Program name: CS162 Project1 * * Author: Taekyoung Kim * * Date: 01/10/2019 - * * Description: This is intValidCheck.h header file of intValidCheck.cpp file for CS162 project1. + * * Description: This is validCh.h header file of intValidCheck.cpp file for CS162 project1. ***************************************************************************************************/ -#ifndef INTVALIDCHECK_H -#define INTVALIDCHECK_H +#ifndef VALIDCH_H +#define VALIDCH_H +#include -int intValidCheck(int input); +int validCh(std::string input); -#endif \ No newline at end of file +#endif //PROJECT1_1_VALIDCH_H