Handling Input to Avoid Program Crashes
This program will help you understand how you can avoid crashes for unexpected input.
Introduction
This article is about validating input in order to avoid crashing when invalid input is given by the user.
Background
Everyone has had, at one point of time, experienced a crash due to incorrect input. This is an example of one way it could be handled in C++.
Using the Code
You can create a project using the attached CPP code. Compile the application and try to enter string where number is required. The program will complain instead of crashing. Just play around with it.
/*
Developed by : Dhruvkumar Rangunwala
Email : instrukahero@yahoo.com
*/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
bool CheckIfNumber(string , int, int&);
const int asciiStartForDigit(48);
int main()
{
double celcius, fahrenheit;
string choice, numberEntered;
int numberEnteredVerified, multiplesOf10 = 1, choiceInNumber;
do
{
system("cls");
cout << "This program converts C -> F and F -> C temperature unit" << endl;
cout << "Enter 1 for C -> F" << endl;
cout << "Enter 2 for F -> C" << endl;
cout << "Enter x or X to exit" << endl;
cin >> choice;
if ((choice.at(0) == 'x' || choice.at(0) == 'X') && choice.length() == 1)
{
break;
}
numberEnteredVerified = 0;
if(CheckIfNumber(choice, multiplesOf10, numberEnteredVerified))
{
cout << "Choice entered is :" << numberEnteredVerified << endl;
switch(numberEnteredVerified)
{
case 1:
cout << "Enter temperature to convert from C to F:";
break;
case 2:
cout << "Enter temperature to convert from F to C:";
break;
default:
cout << "You did not enter choice as 1 or 2\n";
system("pause");
continue;
}
cin >> numberEntered;
}
else
{
cout << "\nYou did not enter a number for choice\n\n\n";
system("pause");
continue;
}
choiceInNumber = numberEnteredVerified;
numberEnteredVerified = 0;
if(CheckIfNumber(numberEntered, multiplesOf10, numberEnteredVerified))
{
cout << "Number entered is :" << numberEnteredVerified << endl;
switch(choiceInNumber)
{
case 1:
celcius = numberEnteredVerified;
fahrenheit = (9.0/5) * celcius + 32;
cout << "Celcius " << celcius <<
" when converted to Fahrenheit is " << fahrenheit << endl;
break;
case 2:
fahrenheit = numberEnteredVerified;
celcius = (fahrenheit - 32) * (5.0/9);
cout << "Fahrenheit " << fahrenheit <<
" when converted to Celcius is " << celcius << endl;
break;
}
system("pause");
}
else
{
cout << "\nYou did not enter a number for conversion\n\n\n";
system("pause");
continue;
}
}while (true);
cout << "\n\n\nExiting the program\n\n\n";
}
bool CheckIfNumber(string numberEntered, int multiplesOf10, int& numberEnteredVerified)
{
char numberEnteredToChar[10];
size_t i;
strcpy(numberEnteredToChar, numberEntered.c_str());
for(i = 0; i < strlen(numberEnteredToChar); i++)
{
if(!(isdigit(numberEnteredToChar[i])))
{
return false;
}
else
{
numberEnteredVerified = (numberEnteredVerified * multiplesOf10) +
((int) numberEnteredToChar[i] - asciiStartForDigit);
multiplesOf10 *= 10;
}
}
return true;
}