# Day 16 Exceptions - String to Integer Hackerrank Solution

Day 16 Exceptions - String to Integer Hackerrank Solutions in C++. Today, we're getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video!

Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String.

Note: You must use the String-to-Integer and exception-handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score.

Input Format

A single string, S.

Constraints

• 1 <= |S| <= 6, where |S| is the length of string S.
• S is composed of either lowercase letters (a- z) or decimal digits (0 - 9).

Output Format

Print the parsed integer value of S, or Bad String if S cannot be converted to an integer.

Sample Input 0

3

Sample Output 0

3

Sample Input 1

za

Sample Output 1

Explanation

Sample Case 0 contains an integer, so it should not raise an exception when we attempt to convert it to an integer. Thus, we print the 3.
Sample Case 1 does not contain any integers, so an attempt to convert it to an integer will raise an exception. Thus, our exception handler prints a Bad String.

## Day 16 Exceptions - String to Integer Hackerrank Solutions in C++

``````#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int main()
{
string S;
cin >> S;

int n;

try
{
n = stoi(S);
cout << n << endl;
}

catch (exception e)
{
}

return 0;
}``````

There is no logic for this problem we have to use a try-catch block(same as Java) as we know if the condition in the try block is true then the program is working fine else the catch part will be executed. In this problem, we are taking a string input and printing its string value or string can't be converted then it should print a Bad String.

We have to use the Try Catch block if the program converts a string into an integer then it is Ok. if not then the Catch block will be executed and we put a message on the catch block.

stoi - The stoi() function takes a string as an argument and returns its value.

Syntax of Try-Catch

``````try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}

catch (exception)
{
statements	//message
}``````

### Similar to Exceptions - String to Integer

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.