@Ozwurld I see that in your code you've used atoi() to convert the string to an integer. But the next thing you want to do is to split the integer into individual digits. My suggestion was only an opinion, that it might be better to keep the value as a string throughout, as this makes it (again in my opinion) easier to access each digit.
Since you will have a vector of strings, then
num[i]
would be a string, you could access each character like this:
num[i][j]
but it might be clearer to introduce a temporary variable
string s
in the example below, and use that for the required operations. I've based my code on your original code, but simplified it a little, notice I have only two vectors and the variable cntr is not needed because the vector maintains its own count of the number of elements.
My apologies for this distraction as you are receiving several different sets of advice, and so I may be adding to the confusion. Anyway, I hope it helps a little.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> alpha;
vector<string> num;
string info, number;
ifstream infile("input.txt");
while(infile>>info>>number)
{
alpha.push_back(info);
num.push_back(number);
}
for (unsigned int i=0; i<num.size(); i++)
{
string s = num[i];
for (unsigned int j=0; j<s.size(); j++)
{
char ch = s[j];
cout << ch << " ";
}
cout << endl;
}
}
|
Output:
1 2 3 4
0 1 8 9
1 2 3 4
1 2 3 4 5 6
9 9 9 9 |
or replace the code at line 26 with this:
26 27 28 29 30
|
for (unsigned int j=0; j<s.size(); j++)
{
int n = s[j] - '0';
cout << n << " ";
}
|
The output looks just the same, but this time using
int
instead of
char
.