displaying strings

Ok, so i have a few questions. There's something wrong with my validation loop because if you enter anything that isn't an int (anything with decimal places), it goes into an infinite loop; However, when there is no decimal places it does as intended. To add to this, i need to get this program to print out a string formatted dollar amount, just as you would on a check.

For example, lets say i had $876.65.
it needs to say, "you have eight hundred and seventy six dollars and 65 cents".

i need to do all this using arrays. i can't use for loops.

Here's my code:


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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	cout << "Welcome to CheckWriter!\n" << endl << endl;

	const int SIZE = 60;
	const int CDATE_SIZE = 60;
	const int WAGE = 7;
	char payeeName[SIZE];
	char payDate[CDATE_SIZE];
	double  payCheck;
	char input[WAGE];

	cout << "Enter Payee's name: \n";
	cin.getline(payeeName, SIZE);
	cout << "Enter pay date (mm/dd/yy): \n";
	cin.getline(payDate, CDATE_SIZE);
	
	cout << "Enter ammount: \n";
	cin.getline(input, WAGE);
	payCheck = atof(input);

	while (payCheck <= 0 || payCheck > 999.99)
	{
	   cout << "ERROR, cannot be a negative number or Amount more than $999.99 \n" << endl;
	   cout << "Please enter a valid amount: " << endl;
	   cout << "Enter ammount: \n";
	   cin.getline(input, WAGE);
	   payCheck = atof(input);
	}

		cout << fixed << showpoint << setprecision(2);

		 cout << right << setw(55) << "Date: " << payDate << endl;
		 cout << " "<< endl;
		 cout << "Pay to the order of: " << payeeName << setw(16) << "$" << payCheck << endl;
		 cout <<  " \n";
   
	system("pause");
	return 0;
}
If I pay myself 12.00 it's OK. But not if I pay myself 1234.50; then it goes ape.

I think you need to rethink how you deal with reading the wage. While you tell people 999.99 is the most they can enter, you do nothing to handle the case if they enter a higher/longer number. As you're extracting only 6 chars from cin, if they enter more that that, cin's fail bit will be set.

Try adding the following after you get the wage the first time.

1
2
3
4
5
6
7
	if(cin.fail())
	{
		cerr << "fail!" << endl; // just for illustration
		cin.clear();
		cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
		// std::numeric_limits is from <limits>
	}


If that works, you should factor out the code to get the wage -- inc the error handling -- into a helper function.

Note that this code does not attempt to handle all error conditions. For example, if I enter 1.w.h.a.t as my wage, I see "fail" but am then paid $1. Ideally you'd check the wage string before converting it.

Andy
Last edited on
Okay i fixed it. The problem was in your WAGE limitation. You set it to accept only 7 "symbols in console" now i've set it to accept 10000 symbols...

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;

int main()
{
	cout << "Welcome to CheckWriter!\n" << endl << endl;

	const int SIZE = 60;
	const int CDATE_SIZE = 60;
	const int WAGE = 10000;
	char payeeName[SIZE];
	char payDate[CDATE_SIZE];
	double  payCheck;
	char input[WAGE];

	cout << "Enter Payee's name: \n";
	cin.getline(payeeName, SIZE);
	cout << "Enter pay date (mm/dd/yy): \n";
	cin.getline(payDate, CDATE_SIZE);
	
	cout << "Enter ammount: \n";
	cin.getline(input, WAGE);
	payCheck = atof(input);

	while (payCheck <= 0 || payCheck > 999)
	{
	   cout << "ERROR, cannot be a negative number or Amount more than $999.99 \n" << endl;
	   cout << "Please enter a valid amount: " << endl;
	   cout << "Enter ammount: \n";
	   cin.getline(input, WAGE);
	   payCheck = atof(input);
	}

		cout << fixed << showpoint << setprecision(2);

		 cout << right << setw(55) << "Date: " << payDate << endl;
		 cout << " "<< endl;
		 cout << "Pay to the order of: " << payeeName << setw(16) << "$" << payCheck << endl;
		 cout <<  " \n";
   
	system("pause");
	return 0;
}
Thank you very much, that fixed the problem!

now, for my other problem. I know i need to set up some string arrays in order to display out a word form of the number the user enters. However, i do not know at all how to go about doing this. See my example at the top. Can anyone help?
Last edited on
bump
This is prob the point of this exercise, so giving you too much help won't help! (in the longer run).

But the floor function should help.
http://www.cplusplus.com/reference/clibrary/cmath/floor/

1
2
3
4
5
double wage = 423.59;

double value = floor(wage / 100.0);

cout << value << endl;

Topic archived. No new replies allowed.