Basic Integer Input Validation Without SplashKit
This brief guide provides code to compare how to implement basic input validation for integers with and without SplashKit.
Written by: Simon Rhook and Olivia McKeon
Last updated: December 2024
Getting Started without SplashKit
Compiling Your Code
With C++ programs, you will need to adjust the compiling command to link to the libraries being used. Below you will see the different commands to compile with and without SplashKit.
Now, assuming the code filename is program.cpp
.
As usual, you will compile the SplashKit C++ code using the following command:
g++ program.cpp -o test -l SplashKit
You can use this command to compile the code below:
g++ program.cpp -o test
Example Code
The following code compares basic input validation for integers with and without SplashKit.
#include <splashkit.h>
int read_integer(string prompt){ // This function converts user input into an integer with input validation using SplashKit functions.
string input_string;
// Prompt user with message in terminal and read input from user write(prompt); input_string = read_line();
// Holds program in a loop if user enters a valid integer while (!is_integer(input_string)) { write_line("Please enter a valid integer."); write(prompt); input_string = read_line(); }
// Convert to integer and return return convert_to_integer(input_string);}
int main(int argc, char *argv[]){ int first_num = read_integer("Please enter first number (whole number): "); int second_num = read_integer("Please enter second number (whole number): ");
write("The sum of the two integers is: "); write_line(first_num + second_num); return 0;}
#include <iostream>
using namespace std;
string trim(const string &text){ // Function to check trim whitespace from string // This is what SplashKit abstracts from the user when calling trim
size_t first = text.find_first_not_of(' '); if (string::npos == first) { return text; } size_t last = text.find_last_not_of(' '); return text.substr(first, (last - first + 1));}
bool is_integer(string text){ // Function to check if input is a valid integer // This is what SplashKit abstracts from the user when calling is_integer
string s = trim(text); if (s.empty() || ((!isdigit(s[0])) && (s[0] != '-') && (s[0] != '+'))) return false;
char *p; strtol(s.c_str(), &p, 10);
return (*p == 0);}
int read_integer(string prompt){ // This function converts user input into an integer with input validation using non-SplashKit functions.
string input_string;
// Prompt user with message in terminal and read input from user cout << prompt; cin >> input_string;
// Holds program in a loop if user enters a valid integer while (!is_integer(input_string)) { cout << "Please enter a valid integer." << endl; cout << prompt; cin >> input_string; }
// Convert to integer and return return stoi(input_string);}
int main(int argc, char *argv[]){ int first_num = read_integer("Please enter first number (whole number): "); int second_num = read_integer("Please enter second number (whole number): ");
cout << "The sum of the two integers is: "; cout << first_num + second_num << endl;
return 0;}