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