Silly Question

Hello, my question is, if you have a string of numbers, such as 12345, how can you separate the string so that your program reads each number individually?

exp.

double n;

cout << "enter number" << endl;
cin >> n;

//how do I cout << 3rd number?
If it is a integer , then you can simply do

num%10 in a loop , till num>0 , where in each loop you divide num/10 .
So in every loop you will get each digit from right to left .
Here's a algorithm for you .
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
34
35
36
37
#include <iostream>

using namespace std;



int Count_Index(long ,int );

int main()
{
	long Number;
	int index;
	cout<<"Please entered the Number:"<<endl;
	cin>>Number;
	cout<<"Please tell which number you want (The index) :"<<endl;
	cin>>index;
	cout<<"the "<<index<<"rd number is: "<<Count_Index(Number,index)<<endl;
	return 0;
}


int Count_Index(long Number,int index)
{
	int* array=new int[10];
	int remainder;
	int i=0,answer=0;
	while(Number!=0)
	{
		remainder =Number%10;
		array[i]=remainder;
		i++;
		Number/=10;
	}
	answer=array[i-index];
	delete array;
		return answer;
}


If you can't understand how it works,just post it
Last edited on
Topic archived. No new replies allowed.