characters & strings

I'm supposed to create a program that displays a check. We're working with Characters and Strings. My question is, How can I take a number someone has inputted and make it read out in words (ex. Input = 99.99, Output = Ninety Nine Dollars and Ninety Nine cents)? My teachers suggests using arrays to hold data, but I don't see how that's necessary if we're only supposed to display one check.

Also, why doesn't my input validation work?

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
//Include Files
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

//Function Prototypes
void dollarFORMAT(string &);

//Main Line

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

	//Gather Data
	cout << "What is today's date?" << endl;
	cin >> date;
	cout << "What is the payee's name?" << endl;
	cin >> name;
	cout << "What is the amount of the check?" << endl;
	cin >> amount;
	
	//Format Output
	cout << " " << date << right << endl;
	cout << "Pay to the Order of: " << name << left;
	dollarFORMAT(amount);
	cout << " " << amount << right << endl;
if (amount > 999.99)
	{
		cout << "Please type in a number less than 999.99" << endl;
		cin >> amount;
	}

	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, "$");
Last edited on
1
2
3
4
5
6
7
if (amount > 999.99)
{
	cout << "Please type in a number less than 999.99" << endl;
	cin >> amount;
}
getch();	
return 0;


If amount is bigger than 999.99... hmm...

haha thanks. my brain is not where it should be, apparently.
You might want to use std::getline() to read the date and the name, because the date has non-alpha chars or spaces and the name may have spaces:

1
2
3
4
5
6
	cout << "What is today's date?" << endl;
	std::getline(cin, date);
	cout << "What is the payee's name?" << endl;
	std::getline(cin, name);
	cout << "What is the amount of the check?" << endl;
	cin >> amount;


EDIT: As you are reading the amount in as a string you may want to use std::getline() with that also.
Last edited on
Topic archived. No new replies allowed.