Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions intValidCheck.cpp

This file was deleted.

119 changes: 49 additions & 70 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,11 @@

#include <iostream>
#include <random>
#include <climits>
#include <string>
#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) {
Expand All @@ -58,29 +38,29 @@ 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."<<std::endl;
std::cout<<"1. Start Langton's Ant simulation"<<std::endl;
std::cout<<"2. Quit the simulation"<<std::endl;
std::cin>>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!"<<std::endl;
break;
case 2:
std::cout<<"You chose 2. Quit the simulation. Good buy!"<<std::endl;
break;
default:
break;
std::cout<<"You didn't choose 1 or 2"<<std::endl;
}

return choice;
return menu;
}

int main() {
Expand All @@ -92,33 +72,31 @@ int main() {
choiceResult = chooseMenu1();

if(choiceResult == 1) {
int widthRow =0, lengthCol=0, numOfStep=0, startRow, startCol, choiceForRanNum;
int lengthCol, widthRow, numOfStep, choiceForRanNum, startRow, startCol;
std::string length, width, steps, choice, userRow, userCol;

do {
//Ask user to input Board's Length and Width and call input validation function for two inputs.
std::cout << "\nPlease choose a board size, input positive integer number for length and width: "
<< std::endl;
std::cin >> 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) {
Expand All @@ -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: "<<std::endl;
std::getline(std::cin, userCol);
startCol =validCh(userCol)-1;
}
}
}
Expand Down
39 changes: 39 additions & 0 deletions validCh.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/****************************************************************************************************
* * Program name: CS162 Project1
* * Author: Taekyoung Kim
* * Date: 01/10/2019
* * Description: This is validCh.cpp file for CS162 project1.
* * This function check users' input validation using string stream.
* * So, if the input is not valid, it keeps asking users to input the correct data.
******************************************************************************************************/


#include "validCh.h"
#include <iostream>
#include <string>
#include <sstream>

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;
}
11 changes: 6 additions & 5 deletions intValidCheck.h → validCh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>

int intValidCheck(int input);
int validCh(std::string input);

#endif
#endif //PROJECT1_1_VALIDCH_H