user2840470
user2840470

Reputation: 979

Test out of range

I am trying to test if accessing an element in a string is out of range. I know how to catch(std::out_of_range) but what im trying to do is if(out_of_range) Example:

string test;
int a;
test="123456789";
if(test.at(9)==out_of_range) //this isnt proper code but this is what im trying to accomplish
{
a=0;}
else
a=1;

I am programming in C++ VS10

Upvotes: 0

Views: 204

Answers (2)

Esteban Filardi
Esteban Filardi

Reputation: 736

Why don't you just check the length of the string ?

Upvotes: 1

kfsone
kfsone

Reputation: 24269

The 'range' of a string is it's size. if there is 1 character in it, then it's size() is 1. So you could do

if(test.size() < 9)

Upvotes: 1

Related Questions