DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Lego_Boy Guest

## some questions

I have been programming in QuickBacic for a long time (unfourtionetly). I
was wondering the c++ equivalent of STR\$(), VAL(), MID\$(), LEFT\$, and RIGHT\$().

With STR\$() you give it a string and ir returns it as a number, so "He has
\$50." would get rewurned as 50.

VAL() turns a number into a string (it is the opposite of STR\$).

MID\$(a\$, b, c) a\$ is a string, b is the legnth of the string that you want
and c is where it starts, so MID\$("Hello world!", 5, 7) would return "world".

LEFT\$(a\$, b) a\$ is the string and b is how much of the string off of the
left side you want, so LEFT\$("Hello world!", 5) would return "Hello"

RIGHT\$(a\$, b) does the same as LEFT\$(a\$, b) except it copies off of the right
side. RIGHT\$("Hello world!", 6) returns "world!"

I hope this is understandable. Thank you. Lego_Boy@juno.com

2. jonnin Guest

## Re: some questions

C++ does not do this much stuff for you (unless the stl strings can, I haven't
used them much). Look at those first, or try these to re-create the functions
you need. email me if you need a lot of help.

"Lego_Boy" <Lego_Boy@juno.com> wrote:
>
>I have been programming in QuickBacic for a long time (unfourtionetly).

I
>was wondering the c++ equivalent of STR\$(), VAL(), MID\$(), LEFT\$, and RIGHT\$().
>
>With STR\$() you give it a string and ir returns it as a number, so "He has
>\$50." would get rewurned as 50.

atoi connverts a string that is a number to an int, atof converts to
float. (ascii to int, ascii to float);
but you would send "50" or "50.0" to this function, not the full string.
A loop to find the numbers or sscanf or something will make the full basic
function.

>
>VAL() turns a number into a string (it is the opposite of STR\$).

sprintf will convert almost anything into a string.
sprintf(astring, "%i", num);
%i is int, %f is float, etc.

>
>MID\$(a\$, b, c) a\$ is a string, b is the legnth of the string that you want
>and c is where it starts, so MID\$("Hello world!", 5, 7) would return "world".

Not sure, I think you would have to build this. I would just take the
address of 'w' &(astring[6]) and quit on whitespace or something.

>
>LEFT\$(a\$, b) a\$ is the string and b is how much of the string off of the
>left side you want, so LEFT\$("Hello world!", 5) would return "Hello"

same as above, find the start and end points.

>
>RIGHT\$(a\$, b) does the same as LEFT\$(a\$, b) except it copies off of the

right
>side. RIGHT\$("Hello world!", 6) returns "world!"

...

>
>I hope this is understandable. Thank you. Lego_Boy@juno.com

3. Danny Kalev Guest

## Re: some questions

Some of these functions have equivalents in both C and C++. You can use
atof(), atoi() atol() etc. to convert a string to a float, int or long ,
respectively. In C++, you would normally use a stringstream to perform
the conversion automatically. As for left() and right() -- you'd have to
write these functions by yourself or use a stringstream object to read
the first word from a string (a blank is treated as a string delimiter
in most cases so breaking a string into individual substrings is rather
easy).

http://gethelp.devx.com/techtips/cpp.../10min0401.asp
this stuff may be too advanced for a beginner but you can see that the
code is rather straightforward

Danny

Lego_Boy wrote:
>
> I have been programming in QuickBacic for a long time (unfourtionetly). I
> was wondering the c++ equivalent of STR\$(), VAL(), MID\$(), LEFT\$, and RIGHT\$().
>
> With STR\$() you give it a string and ir returns it as a number, so "He has
> \$50." would get rewurned as 50.
>
> VAL() turns a number into a string (it is the opposite of STR\$).
>
> MID\$(a\$, b, c) a\$ is a string, b is the legnth of the string that you want
> and c is where it starts, so MID\$("Hello world!", 5, 7) would return "world".
>
> LEFT\$(a\$, b) a\$ is the string and b is how much of the string off of the
> left side you want, so LEFT\$("Hello world!", 5) would return "Hello"
>
> RIGHT\$(a\$, b) does the same as LEFT\$(a\$, b) except it copies off of the right
> side. RIGHT\$("Hello world!", 6) returns "world!"
>
> I hope this is understandable. Thank you. Lego_Boy@juno.com

4. ralph Guest

## Re: some questions

Danny Kalev <dannykk@inter.net.il> wrote:
>Some of these functions have equivalents in both C and C++. You can use
>atof(), atoi() atol() etc. to convert a string to a float, int or long ,
>respectively. In C++, you would normally use a stringstream to perform
>the conversion automatically. As for left() and right() -- you'd have to
>write these functions by yourself or use a stringstream object to read
>the first word from a string (a blank is treated as a string delimiter
>in most cases so breaking a string into individual substrings is rather
>easy).
>
>http://gethelp.devx.com/techtips/cpp.../10min0401.asp
>this stuff may be too advanced for a beginner but you can see that the
>code is rather straightforward
>
>Danny
>
>
>
>Lego_Boy wrote:
>>
>> I have been programming in QuickBacic for a long time (unfourtionetly).

I
>> was wondering the c++ equivalent of STR\$(), VAL(), MID\$(), LEFT\$, and

RIGHT\$().
>>
>> With STR\$() you give it a string and ir returns it as a number, so "He

has
>> \$50." would get rewurned as 50.
>>
>> VAL() turns a number into a string (it is the opposite of STR\$).
>>
>> MID\$(a\$, b, c) a\$ is a string, b is the legnth of the string that you

want
>> and c is where it starts, so MID\$("Hello world!", 5, 7) would return "world".
>>
>> LEFT\$(a\$, b) a\$ is the string and b is how much of the string off of the
>> left side you want, so LEFT\$("Hello world!", 5) would return "Hello"
>>
>> RIGHT\$(a\$, b) does the same as LEFT\$(a\$, b) except it copies off of the

right
>> side. RIGHT\$("Hello world!", 6) returns "world!"
>>
>> I hope this is understandable. Thank you. Lego_Boy@juno.com

You can also make it easier with some additional libraries (depending on
environment). For example, CString and SuperString (String) both have Mid,
Right, & Left methods. They work identically to the VB functions except they
count from 0 not 1.

Another reason that makes it difficult to give a good quick answer, is because
you tend to handle things differently in C/C++. For example, VB's

Print( Right\$("Hello world!", 6)) might be ...

char str[] = "Hello world!";
string sWorld( str + strlen(str) - 6 );
cout << sWorld << endl;

5. Danny Kalev Guest

## Re: some questions

ralph wrote:
>
> Danny Kalev <dannykk@inter.net.il> wrote:
> >Some of these functions have equivalents in both C and C++. You can use
> >atof(), atoi() atol() etc. to convert a string to a float, int or long ,
> >respectively. In C++, you would normally use a stringstream to perform
> >the conversion automatically. As for left() and right() -- you'd have to
> >write these functions by yourself or use a stringstream object to read
> >the first word from a string (a blank is treated as a string delimiter
> >in most cases so breaking a string into individual substrings is rather
> >easy).
> >
> >http://gethelp.devx.com/techtips/cpp.../10min0401.asp
> >this stuff may be too advanced for a beginner but you can see that the
> >code is rather straightforward
> >
> >Danny
> >
> >
> >
> >Lego_Boy wrote:
> >>
> >> I have been programming in QuickBacic for a long time (unfourtionetly).

> I
> >> was wondering the c++ equivalent of STR\$(), VAL(), MID\$(), LEFT\$, and

> RIGHT\$().
> >>
> >> With STR\$() you give it a string and ir returns it as a number, so "He

> has
> >> \$50." would get rewurned as 50.
> >>
> >> VAL() turns a number into a string (it is the opposite of STR\$).
> >>
> >> MID\$(a\$, b, c) a\$ is a string, b is the legnth of the string that you

> want
> >> and c is where it starts, so MID\$("Hello world!", 5, 7) would return "world".
> >>
> >> LEFT\$(a\$, b) a\$ is the string and b is how much of the string off of the
> >> left side you want, so LEFT\$("Hello world!", 5) would return "Hello"
> >>
> >> RIGHT\$(a\$, b) does the same as LEFT\$(a\$, b) except it copies off of the

> right
> >> side. RIGHT\$("Hello world!", 6) returns "world!"
> >>
> >> I hope this is understandable. Thank you. Lego_Boy@juno.com

>
> You can also make it easier with some additional libraries (depending on
> environment). For example, CString and SuperString (String) both have Mid,
> Right, & Left methods. They work identically to the VB functions except they
> count from 0 not 1.
>
> Another reason that makes it difficult to give a good quick answer, is because
> you tend to handle things differently in C/C++. For example, VB's
>
> Print( Right\$("Hello world!", 6)) might be ...
>
> char str[] = "Hello world!";
> string sWorld( str + strlen(str) - 6 );
> cout << sWorld << endl;

Yes, I think we can generalize that C and C++ don't have such functions
because they are much less needed in these languages; the very notion of
a string in these in C is radically different from VB's view of strings.
Since a string is a mere array, accessing arbitrary portions thereof to
a resolution of a single character makes such functions almost useless.
In C++ a string is represented differently but you still have the
overloaded [] that provides the same functionality.

Danny

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise