How to read a number by separate digits

Hi,
im working on a program that can read number by separate digits
ex: if user put in 32, then it will read as 3 and 2.

my english is not very gud so i hope u can understand wat im trying to say

You can read it in as a string and parse character by character.
u mean declare number as string?? how am i suppose to do that? sorry im a totally noob in programming.. btw im using borland c++5.02
Last edited on
closed account (D80DSL3A)
Like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string numStr;
	cout << "Enter a number: "; 
	cin >> numStr;// store it as a string

	for(size_t i=0; i<numStr.length(); ++i)
		cout << numStr[i] << " ";// output each character

	cout << endl;
	return 0;
}
If I am not mistaken Borland Turbo C++ doesn't support C++ strings (though it does have the string library).
It uses an array of chars to works as a string!
Or you could start it as an int, and just type_cast it to a string when you need to pull out individual digits. This way you can still use it in arithemetics and what not. I think if you start it as a string and convert it to an int it will create issues due to ASCII codes. I may be mistaken though
Topic archived. No new replies allowed.