You are mixing C with C ++ and I think this is the problem, says that you were told to use substr()
, it only makes sense to use the native of C ++ and not use the C to create something like a string . If it were pure C there is substr()
. It would be like this:
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "123456789";
for (int c = 0; c < 10; c++) cout << a.substr(c, 1);
}
See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .
If you did not need this method you could do better:
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "123456789";
for (int c = 0; c < 10; c++) cout << a[c];
}
See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .
But in C ++ it would look even better:
#include <iostream>
#include <string>
using namespace std;
int main() {
string a = "123456789";
for (char& c : a) cout << c;
}
See running on ideone . And no Coding Ground . Also I put in GitHub for future reference .