string --> int[]

F

Frank Wagner

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 = atoi(&a);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b << 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?

greetings,
Frank
 
G

Guest

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 = atoi(&a);
}

for (byte i = 0; i < sizeof(a)-1; i++) {
cout << b << 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 = a-'0';
instead of atoi in the loop above.
 
N

Noah Roberts

Frank said:
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 = atoi(&a);

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 = a - '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, '\0' and pass this to
atoi....this will always work.

NR
 
A

Alf P. Steinbach

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)

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 - '\0' );
}



i tried this:

Use
#include <cstdlib>


#include <math.h>
Use

#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 = atoi(&a);


'atoi' expects a zero-terminated string of digits, which you're giving
it, namely a and all following characters until the terminating zero...


}

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

See above.

cout << b << 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).
}
----------------------------------------

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?

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

Alf P. Steinbach


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.
 
F

Frank Wagner

Thank you for your help! Did it, but now got another problem:

if i have:
int b[3];

it´s:
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!!! what´s happened?
....
}

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

there´s inside of my_function:
sizeof(arr) = 4;

What´s 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
 
T

Tim Threlfall

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
 
R

Ryan Winter

Frank said:
Thank you for your help! Did it, but now got another problem:

if i have:
int b[3];

it´s:
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!!! what´s happened?
...
}

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

there´s inside of my_function:
sizeof(arr) = 4;

What´s 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
 
T

Tim Threlfall

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.
 
G

Gavin Deane

Noah Roberts said:
You could:

a) Assume that you are working with ASCII and "b = a - '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
 
K

Karl Heinz Buchegger

Noah said:
You could:

a) Assume that you are working with ASCII and "b = a - '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.
 
K

Karl Heinz Buchegger

Gavin said:
Noah Roberts said:
You could:

a) Assume that you are working with ASCII and "b = a - '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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,824
Members
47,370
Latest member
desertedtyro29

Latest Threads

Top