0

I know it is stupid question but it does not resolve, and I am googling but that didn't help either. I want to select the sub string from my string and then convert it to ASCII value, but it shows me an error:

int a=char(S.substr(i-1,1));
        int b=S.substr(i ,1);
      if (( a== 13) && (b== 10))
        break;  

this is my error :

pdusms.cpp:1020: error: invalid cast from type 'std::basic_string' to type 'char' int a=char(S.substr(i-1,1));
pdusms.cpp:1021: error: invalid conversion from 'const char*' to 'int' [-fpermissive] int b=S.substr(i ,1).c_str(); ^
How can i do that? how can i change the char to int (show ASCII value)

3
  • Try int b=S.substr(i ,1).c_str(); Commented Nov 2, 2015 at 9:16
  • I do that. but has error Commented Nov 2, 2015 at 9:38
  • @OzairKafray c_str returns a pointer. That's not convertible to char. Commented Nov 2, 2015 at 9:57

1 Answer 1

2

The result of substr is a string, not a character.
A one-character string is not a character.

Use indexing to get the characters.

You want

int a = S[i-1];
int b = S[i];

but if you're looking for line delimiters you shouldn't compare to ASCII values - use

if (S[i-1] == '\r' && S[i] == '\n')

which is portable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.