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:
Example Code
The following code compares basic input validation for integers with and without SplashKit.
int read_integer (string prompt )
// This function converts user input into an integer with input validation using SplashKit functions.
// Prompt user with message in terminal and read input from user
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. " );
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);
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)
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
if ( s . empty () || (( ! isdigit ( s [ 0 ])) && ( s [ 0 ] != ' - ' ) && ( s [ 0 ] != ' + ' )))
strtol ( s . c_str (), & p, 10 );
int read_integer (string prompt )
// This function converts user input into an integer with input validation using non-SplashKit functions.
// Prompt user with message in terminal and read input from user
// Holds program in a loop if user enters a valid integer
while ( ! is_integer (input_string))
cout << " Please enter a valid integer. " << endl;
// 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;