splitting input into digits.

For example if someone enters $999.99

how do i split it into the 900 part , 99 , and the 99 cents?
so it will read out like a check.
(nine hundred ninety nine dollars and 99 cents)
Last edited on
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
#include <iostream>
using namespace std;

int main()
{
    double input;
    int int_part;
    int dec_part;

    input=123.45;

    int_part=input;
    dec_part=(input-int_part)*100;

    cout << "int_part: ";
    cout << int_part << endl;

    cout << "dec_part: ";
    cout << dec_part << endl;

    //now that you have them separated
    //and stored in ints, you can use
    //the modulo operator (%) to get
    //the digits you want

    cout << (int_part-int_part%100) << endl;
    cout << int_part%100 << endl;
    cout << dec_part<< endl;

    cin.get();
    return 0;
}
Last edited on
There are better ways of doing this, but this is just for concept..

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
char GetDigit(int iInput, int iDigit) {
	char *str;

	itoa(iInput, str, 10);

	// Don't access anything outside of our array
	if(strlen(str) <= (iDigit - 1)) {
		return 0;
	}

	return str[iDigit - 1];
}

int main() {
	cout << "Enter in a number\n> ";

	double *input = new double;

	cin >> input;
	cin.ignore();

	int whole = floor(*input);
	int fraction = *input - whole;

	delete input;
	input = NULL;

	// TODO: Use "GetDigit" i.e:
	// cout << "The 2nd whole digit is: " << GetDigit(whole, 2);

	return 0;
}
Line 19:
cin >> (*input);
1
2
3
4
double *input = new double;
[...]
delete input;
input = NULL;


You're joking... right?
Topic archived. No new replies allowed.