471,612 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

string --> int[]

Hello NG!

Though Im not new to programming at all, im new to C++.

got the following question/problem with its behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
return 0;
}
----------------------------------------

doesnt work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
whats my failure?

greetings,
Frank
Jul 19 '05 #1
13 2438
On Wed, 15 Oct 2003 23:03:13 +0200, Frank Wagner
<ne*****************@nofusion.de> wrote:
Hello NG!

Though I´m not new to programming at all, i´m new to C++.

got the following question/problem with it´s behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
return 0;
}
----------------------------------------

doesn´t work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
what´s my failure?
atoi takes const char* and ends conversion when it encounters non digit
character,

To convert char to integer - you should just use expression digit -'0'
b[i] = a[i]-'0';
instead of atoi in the loop above.


greetings,
Frank


--
grzegorz
Jul 19 '05 #2
Frank Wagner wrote:
Hello NG!

Though Im not new to programming at all, im new to C++.

got the following question/problem with its behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
- then calculate average value and variety of digits

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <String.h>

using namespace std;

int main(int argc, char *argv[])
{

string a;

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];

for (byte i = 0; i < sizeof(a)-1; i++) {
b[i] = atoi(&a[i]);

Your failure is here.

Assuming that a std::string keeps its characters in a byte array (which
I don't know - experts?)...What you are doing is passing atoi your
entire string minus the first 'i' characters. Therefor your first
iteration passes "123", your second "23", your third "3"...and you get
the results you are seing.

You could:

a) Assume that you are working with ASCII and "b[i] = a[i] - '0'". This
will break on systems that use a character set where 0-9 is not concurrent.
b) create a char[2] array and fill with a[i], '\0' and pass this to
atoi....this will always work.

NR

Jul 19 '05 #3
On Wed, 15 Oct 2003 23:03:13 +0200, Frank Wagner <ne*****************@nofusion.de> wrote:
Hello NG!

Though Im not new to programming at all, im new to C++.

got the following question/problem with its behaviour:

- read number with *arbitrary number of digits* from keyboard (done)
Concentrate on the digits, which is a string of characters.

- then calculate average value and variety of digits
Variety?

the second sould be no problem, too, i think.

the problem is as follows:

how can i create a int[] from my read string with each element
representing *one* digit from string?

std::vector<int> v;
for( std::size_t i = 0; i < s.size(); ++i )
{
v.push_back( s[i] - '\0' );
}

i tried this:
----------------------------------------
#include <iostream>
#include <stdlib.h>
Use
#include <cstdlib>
#include <math.h>
Use
#include <cmath>

#include <String.h>
<string.h> is not the same header as <string>


using namespace std;

int main(int argc, char *argv[])
{

string a;
You're unlucky if this compiles (it might, depending on what <vector> drags
in).

cout << "Beliebige Ziffernfolge eingeben" << endl;
cin >> a;

int b[sizeof(a)-1];
You need a.size(), not sizeof(a). The former tells you the number of characters
stored in the string, the latter the size in bytes of the container variable
(which typically holds just a pointer to the character buffer, plus a few other
values such as the current length returned by the size() member function).


for (byte i = 0; i < sizeof(a)-1; i++) {
There's no predefined data type 'byte' in standard C++.

Also, use '++i', not 'i++'.
b[i] = atoi(&a[i]);
'atoi' expects a zero-terminated string of digits, which you're giving
it, namely a[i] and all following characters until the terminating zero...
}

for (byte i = 0; i < sizeof(a)-1; i++) {
See above.

cout << b[i] << endl;
}

cout << endl;
system("PAUSE");
This will only work on a system with a "PAUSE" command (e.g. Windows), and
it will make it slightly more difficult to test the program automatically, or
to use it in automation.

return 0;
OK, but you don't need it (a special feature of 'main' is that it returns 0
by default).
}
----------------------------------------

doesnt work! for a="123" i get:
b[0] = 123
b[1] = 23
b[2] = 3

instead of (what i would need):
b[0] = 1
b[1] = 2
b[2] = 3
whats my failure?


That, I don't know... ;-)

Jul 19 '05 #4
Alf P. Steinbach wrote:
for (byte i = 0; i < sizeof(a)-1; i++) {


Also, use '++i', not 'i++'.


Why?

--
Unforgiven

A: Top Posting!
Q: What is the most annoying thing on Usenet?
Jul 19 '05 #5
On Thu, 16 Oct 2003 00:22:59 +0200, "Unforgiven" <ja*******@hotmail.com> wrote:
Alf P. Steinbach wrote:
for (byte i = 0; i < sizeof(a)-1; i++) {


Also, use '++i', not 'i++'.


Why?


Several reasons, all having to do with good coding practice.

The most important reason is that "++i" works with most kind of data types
providing an increment operation, whereas "i++" does not. In particular
for iterators. So getting used to writing "++i" as default is a good idea.

The most often encountered reason is that "++i" has a less error-prone
side-effect than "i++", in other words, is a tad less unsafe.

The most religious reason is that with "i++" extra work is requested, which
is only meaningful if that work is contributing towards the code's overall
purpose; and meaningless code should be avoided.

The most easily grasped reason, for complete novices (if you have some
experience this can be very difficult to grasp since it's only technically
true, not usually relevant to anything), is that "++i" can be more efficient
than "i++", e.g. see the FAQ 13.12.

Jul 19 '05 #6
Thank you for your help! Did it, but now got another problem:

if i have:
int b[3];

its:
sizeof(b) = 12; // cause sizeof(int) = 4

but if i do sth like this:
-----------------------------------
void my_function(int arr[])
{
....
sizeof(arr);
// sizeof(arr) only returns 4!!! whats happened?
....
}

void main()
{
....
int b[3];
// sizeof(b) returns 12 - so everythings alright...
my_function(b);
....
}
-----------------------------------

theres inside of my_function:
sizeof(arr) = 4;

Whats happening here, and how can i receive the "correct" value for
sizeof(b) in my functions? ("correct" now means 12 to me ;) )

thanks for your time!

greetings,
Frank

Jul 19 '05 #7
On Wed, 15 Oct 2003 21:35:00 GMT, al***@start.no (Alf P. Steinbach) wrote:
std::vector<int> v;
for( std::size_t i = 0; i < s.size(); ++i )
{
v.push_back( s[i] - '\0' );
}


Oops, keyboard error: that should be '0' not '\0'.

Jul 19 '05 #8
You could also use stringstreams instead of atoi to convert your string
into an int, something like this

template<typename RT, typename T, typename Trait, typename Alloc>
RT ss_atoi( std::basic_string<T, Trait, Alloc>& the_string )
{
std::basic_istringstream< T, Trait, Alloc> temp_ss(the_string);
RT num;
temp_ss >> num;
return num;
}

Or use boost::lexical_cast
Jul 19 '05 #9
Frank Wagner wrote:
Thank you for your help! Did it, but now got another problem:

if i have:
int b[3];

its:
sizeof(b) = 12; // cause sizeof(int) = 4

but if i do sth like this:
-----------------------------------
void my_function(int arr[])
{
...
sizeof(arr);
// sizeof(arr) only returns 4!!! whats happened?
...
}

void main()
{
...
int b[3];
// sizeof(b) returns 12 - so everythings alright...
my_function(b);
...
}
-----------------------------------

theres inside of my_function:
sizeof(arr) = 4;

Whats happening here, and how can i receive the "correct" value for
sizeof(b) in my functions? ("correct" now means 12 to me ;) )


This is always a bit tricky. You have to remember that when you pass an
array into a function, you are actually passing a pointer to the array.
Therefor when you try and get its size, you end up with the size of the
pointer.

The way around it is to pass the length of the array in as a second
parameter, or even easier, pass it in as a std::vector<int>&.

<code>

#include <vector>

void my_function(std::vector<int>& arr)
{
arr.size(); // 3
}

int main()
{
std::vector<int> b(3);
b.size() // 3

my_function(b);
}

</code>

Ryan

Jul 19 '05 #10
You could also use stringstreams instead of atoi() to convert an
std::string into an int, something like this

template<typename RT, typename T, typename Trait, typename Alloc>
RT ss_atoi( std::basic_string<T, Trait, Alloc>& the_string )
{
std::basic_istringstream< T, Trait, Alloc> temp_ss(the_string);
RT num;
temp_ss >> num;
return num;
}
Or you could use boost::lexical_cast.

Jul 19 '05 #11
Noah Roberts <nr******@dontemailme.com> wrote in message news:<vo************@corp.supernews.com>...
You could:

a) Assume that you are working with ASCII and "b[i] = a[i] - '0'". This
will break on systems that use a character set where 0-9 is not concurrent.


I thought that there was a guarantee that subtracting '0' from any
char '0', '1', '2', ... '9' had to give the results 0, 1, 2, ... 9,
even if those characters were not contiguous in the underlying
character set. A few things occur:

I can't remember where I got this idea from.

I have no idea how it would work when the characters are not
contiguous - there would need to be some extra level of interpretation
somewhere.

I can't find it in the standard, though if it were guaranteed I
suppose it could be inherited from the C standard and I don't have
that.

While I can't think how it would work, I'm still sure I've come across
this guarantee somewhere reputable. Can anyone enlighten me?

GJD
Jul 19 '05 #12


Noah Roberts wrote:

You could:

a) Assume that you are working with ASCII and "b[i] = a[i] - '0'". This
will break on systems that use a character set where 0-9 is not concurrent.


The characters '0' to '9' are concurrent in C++ by definition.
So there is no problem in using the above. It will work everywhere.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #13


Gavin Deane wrote:

Noah Roberts <nr******@dontemailme.com> wrote in message news:<vo************@corp.supernews.com>...
You could:

a) Assume that you are working with ASCII and "b[i] = a[i] - '0'". This
will break on systems that use a character set where 0-9 is not concurrent.


I thought that there was a guarantee that subtracting '0' from any
char '0', '1', '2', ... '9' had to give the results 0, 1, 2, ... 9,
even if those characters were not contiguous in the underlying
character set. A few things occur:

I can't remember where I got this idea from.


From the C++ standard. Although not mentioned explicitely
in the C++ standard, it follows from other definitions.
In C, the above is mentioned explicitely and as far as I know
the non-mentioning in C++ is considered a defect in the C++
standard and will be corrected.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

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.