decimal places

this program is supposed to split an inputted number into 3 decimal places (hundreds, tens, ones) and also the remainder, like the amount of a check. (ex/$123.45) What am I doing wrong for the ones place? And how do I get the cents?

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//Include Files
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

//Function Prototypes

//Main Line

int main()
{
	string textnum[11] = { " ",
						"one",
						"two ",
						"three",
						"four",
						"five",
						"six",
						"seven",
						"eight",
						"nine" };
	string tensnum[11] = { " ",
							" ",
							"ten",
							"twenty",
							"thirty",
							"forty",
							"fifty",
							"sixty",
							"seventy",
							"eighty",
							"ninety",};
	
	float amount;

	cout << "What is the amount?";
	cin >> amount;
	
	int intamt = amount; // intamt contains 123. It is an int, and cannot hold the decimal 
	int hundred = intamt/100; // 123/100 =1.23, but since hundred is an int, it will hold 1
	int remainder = intamt%100;
    int ten = remainder%10;
	int unit = ten%1;
	
	cout << textnum[hundred] << " hundred ";
	cout << tensnum[ten] << " ";
	cout << textnum[unit] << " ";

	getch();
	return 0;
}
madelineosman wrote:
What am I doing wrong for the ones place?

You should do it like this:

1
2
3
int remainder = intamt%100;
int ten = remainder/10;
int unit = remainder%10;

And remove the first blank entry from your tensnum array.

madelineosman wrote:
And how do I get the cents?

http://cplusplus.com/forum/beginner/26112
thanks that helps a lot. the only thing is that the decimal part is off by one every time.
Ah, I see... You see, dec_part=(input-int_part)*100; takes for granted that the number of decimal digits is two. If there are three or more, they'll get chopped off. And it there's only one it'll not show up properly. Let's try a different approach using stringstream:

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
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    string input;

    int int_part;
    int dec_part;

    cout << "enter a number: ";

    //getting integer part
    getline(cin,input,'.');
    stringstream(input)>>int_part;

    //getting decimal part
    getline(cin,input,'\n');
    stringstream(input)>>dec_part;

    //let's see what we got...
    cout << int_part << endl;
    cout << dec_part << endl;

    cin.get();
    return 0;
}
Last edited on
thank you so much. you've gotten me much farther in this program than i've been able to do myself. but i'm still having problems. I don't know why it wont advance past the first string inputs...
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Include Files
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

//Function Prototypes
void dollarFORMAT(string &);

//Main Line

int main()
{
	string textnum[11] = { " ",
						"one",
						"two ",
						"three",
						"four",
						"five",
						"six",
						"seven",
						"eight",
						"nine" };
	string tensnum[11] = { " ",
							"ten",
							"twenty",
							"thirty",
							"forty",
							"fifty",
							"sixty",
							"seventy",
							"eighty",
							"ninety",};
	
	string date, payee, amount;

	cout << "What's the date?";
	cin >> date;
	cout << "Who is the payee?";
	cin >> payee;
	cout << "What is the amount?";
	cin >> amount;

	//payee
	cout << "Pay to the Order of: " << payee << endl;

	//date
	cout << "Date: " << date << endl;

	//dollar format
	dollarFORMAT(amount);
	cout << " " << amount << endl;

	int amount2;
	amount2 = atoi("amount");
	
	//words	
	int intamt = amount2;
	int hundred = intamt/100; 
	int remainder = intamt%100;
	int ten = remainder/10;
	int unit = remainder%10;
	
	//cents
	int dec_part;
    getline(cin, amount, '\n');
    stringstream(amount) >> dec_part;


	//display words
	cout << textnum[hundred] << " hundred ";
	cout << tensnum[ten] << " ";
	cout << textnum[unit] << " ";
    cout << dec_part << " cents" << endl;


	getch();
	return 0;
}

//Function Declarations
void dollarFORMAT(string &currency)
{
	int dp;

	dp = currency.find('.');
	if (dp > 3)
	{
		for (int x = dp - 3; x > 0; x -= 3)
			currency.insert(x, ",");
	}
	currency.insert(0, "$");
}
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

string textnum[10]=
{" ","one","two","three","four",
"five","six","seven","eight","nine"};

string tensnum[10]=
{" ","ten","twenty","thirty","forty",
"fifty","sixty","seventy","eighty","ninety",};

int main()
{
    string date, payee, amount;

    cout << "What's the date?";
    getline(cin,date,'\n');

    cout << "Who is the payee?";
    getline(cin,payee,'\n');

    cout << "What is the amount?";
    getline(cin, amount,'\n');

    //payee
    cout << "Pay to the Order of: " << payee << endl;

    //date
    cout << "Date: " << date << endl;

    //dollar format
    cout << "$" << amount << endl;

    istringstream buffer(amount);

    int int_part;
    buffer>>int_part;

    //words
    int hundred = int_part/100;
    int remainder = int_part%100;
    int ten = remainder/10;
    int unit = remainder%10;

    //cents
    double dec_part=0;
    buffer>> dec_part;
    dec_part*=100;

    //display words
    cout << textnum[hundred] << " hundred ";
    cout << tensnum[ten] << " ";
    cout << textnum[unit] << " ";
    cout << dec_part << " cents" << endl;

    cin.get();
    return 0;
}
i hope i'm not being too forward, but i love you.
you are my definition of jesus right now.
lol, it would still be a good idea to see how getline, strings and stringstreams work. Take a look at these links when you find the time:

http://cplusplus.com/reference/string/getline/ (the getline we used here)
http://cplusplus.com/reference/iostream/istream/getline/ (not the getline we used here)

http://cplusplus.com/reference/string/string/

http://cplusplus.com/reference/iostream/stringstream/

Also, notice that the program will crash if the integer part has more than three digits, because in that case the variable hundred will hold something that is not a valid index for your textnum array.
Topic archived. No new replies allowed.