why wont you work?!

this code is intended to print out an amount entered for a check into words. however, when a user types something like "19" as the amount, this is what displays:
hundred ten nine 0 cents

hellllppp meeeeee.

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

string textnum[20]=
{" ","one","two","three","four",
"five","six","seven","eight","nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",};

string tensnum[10]=
{" ","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 ";
    if (textnum[hundred] < 100)
		cout << " ";
	cout << tensnum[ten] << " ";
	if (tensnum[ten] > 9 && tensnum[ten] < 20)
		cout << textnum[unit] << " ";
    cout << textnum[unit] << " ";
    cout << dec_part << " cents" << endl;

    cin.get();

	getch();
    return 0;
}
The problems lie in the 'display words' section.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout << textnum[hundred] << " hundred ";//this outputs "hundred" even when there are really no hundreds
//this isn't what you want. this should be
//if(hundred > 0) cout << textnum[hundred] << " hundred";

if (textnum[hundred] < 100) cout << " ";//here you compare string with int. I don't see how did this compile for you
//I also fail to see the purpouse of this part

cout << tensnum[ten] << " ";//you get twenty, because tensnum[1] == "twenty".
if (tensnum[ten] > 9 && tensnum[ten] < 20) cout << textnum[unit] << " ";//again, you can't compare strings with ints
cout << textnum[unit] << " ";

//instead of these three lines there should be something like this:
//if(ten > 1) cout << tensnum[ten-1] << " ";
//if(ten == 1) cout << textnum[10+unit] << " ";
//else cout << textnum[unit] << " ";


cout << dec_part << " cents" << endl;
Last edited on
Topic archived. No new replies allowed.