473,378 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

string lengths as integers.

I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-
Jul 19 '05 #1
3 2913
"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

1) Did you mean:
pStr.length()
?
std::string::length is a member function, not a data member.

2) in C++, char is not equivalent to a 1-character string.
To get the n-th character of a string, you need to
use the [] operator instead of substr.
And character indexes are 0-based, so the last character
is at position length()-1 :
char b = pStr[ pStr.length()-1 ];
hth
--
http://ivan.vecerina.com
Jul 19 '05 #2

Length is a function, not a scalar.
Try pStr.substr(pStr.length(), 1)

--
Andy Kashyap
to reply, sed -e 's/nospam//g' emailAddress

"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-

Jul 19 '05 #3
Try
char b = *pstr.rbegin();
Catalin

"Chris Schumacher" <ke*****@hotmail.com> wrote in message
news:kn********************************@4ax.com...
I've been trying to use the following line of code to take a character
from the end of the string:

char b = pStr.substr(pStr.length, 1);

However the compiler (the Visual C++ one) returns the following error:
'substr' : cannot convert parameter 1 from 'unsigned int
(void) const' to 'unsigned int' Conversion is a valid
standard conversion, which can be performed implicitly or by
use of static_cast, C-style cast or function-style cast

I tried static_casting, C-Style and function-style to assign an int
variable the value of the string length, but I got the exact same
error every time.
I also tried use strlen(pStr) and got a similar error (only the data
type of the original was different).

Is there any way to make this work?
Any help is greatly appreciated!

-==Kensu==-

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Kamilche | last post by:
I was looking for a way to speed up detecting invalid characters in my TCP string, and thought of yet another use for the translate function! If you were to 'translate out' the bad characters, and...
8
by: Hal Vaughan | last post by:
Is there a maximum length for Javascript program lines? What about strings? Is there a limit on string length? I found some references that said the maximum string length was 256 characters,...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
7
by: Amittai Aviram | last post by:
Hi! Is there a convenient way to create a null-terminated string out of an array of type int? Is there a standard library function I can use? If so, how? This seems elementary, but I can't...
4
by: Pokerkook | last post by:
Hello, If anybody could help me with this I would greatly appreciate it. Or at least tell me why I get the output of this garbage: 49 49 10 49 52
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
23
by: Rogers | last post by:
I want to compare strings of numbers that have a circular boundary condition. This means that the string is arranged in a loop without an end-of-string. The comparaison of two strings now...
1
by: Lilith | last post by:
I'm looking at practical string lengths and the capacity of a text box. I'm using a string to progressively add the status of a process periodically and dumping that text into a text box. I don't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.