Coin program not taking in amount of cents in input

closed account (LEwpfSEw)
I'm currently working on a program to take the user's input as DDDD.CC and then converting it to the least amount of coins using quarters, dimes, nickels, and pennies.

I was told to not use any doubles or floats as they would produce errors. And supposed to the use the library function atoi() after I take the input user's amount as a string to convert it into an integer.

So far the string input has worked and it produced the right number of quarters but it leaves off the cents.

For example, I put in 352.23 and it gives the right amount of quarters but leaves off the 23 cents.

I need help as soon as possible, thank you!

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
#include <iostream> // Allows the program to perform input and output
#include <stdlib.h> // Atoi 
#include <string>


using namespace std ; // Enables a program to use all the names in any Standard C++ header


int main() // Start of program execution
	
{
	
	string line ;
	
	cout << "Enter amount of dollars and cents in the format DDDD.CC:" << endl ; // Prompts user for amount
	getline(cin, line); 
	
	cout << "Input Amount:\t"  << line << endl << endl ;

	
	int amount = atoi(line.c_str()) ;

	int Change = (int) (amount * 100.) ; // Converting the amount into pennies
	
	int Quarters = Change / 25 ; // Finding the amount of Quarters from the amount
	Change = Change % 25 ; // Stating the new remainder left over

	int Dimes = Change / 10 ; // Finding the amount of Dimes from the amount left over
	Change = Change % 10 ; 

	int Nickles = Change / 5 ; // Finding the amount of Nickles in the amount left over
	Change = Change % 5 ;

	int Pennies = Change ; 

	
	// Display of the coins
	cout << "Quarters:\t"        << Quarters << endl ;
	cout << "Dimes:\t\t"		 << Dimes << endl ;
	cout << "Nickels:\t"		 << Nickles << endl ;
	cout << "Pennies:\t"		 << Pennies << endl ;

	return 0 ;

}
the function atoi converts your string to "integer" hence leaving out the decimal part (or the cents part).
why can't you take input in a float or double straight away. if you still want to use string, you will have to call a function like atof.
1) Why do you use atoi(line.c_str()); instead of stoi(line)?
2) Line 21: When you cast 352.23 to int it will truncate it to 352 because int can store only whole numbers. Just change that line to double amount = stod(line); and everything will work fine.
closed account (LEwpfSEw)
Thank you for your help! I have used your suggestions and it worked, I'm just afraid I'll get some points marked off for using "double" and "float" as my professor told me not to due to the errors it creates sometimes, but I went with it anyway. Haha.

Thank you once again!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string line ;

    cout << "Enter amount of dollars and cents in the format DDDD.CC:" << endl ; // Prompts user for amount
    getline(cin, line); 

    unsigned pos = line.find('.') ;         // find the decimal.
    if ( std::string::npos != pos )         // and if it exists,
        line.erase(pos, 1) ;                // remove it.
    else                                    // otherwise, there is no decimal so
        line.append("00") ;                 // "multiply" by 100.

    cout << "Input Amount:\t"  << line << endl << endl ;


    int Change = atoi(line.c_str()) ;
Last edited on
wow! thanx cire.. i think OnceKnown's professor wanted your kind of a solution. Was a great learning experience for me also...
closed account (LEwpfSEw)
Wow. Cire Got it on the spot! Thank you so much for that. Now I know what to do next time if I encounter this type of problem.
Topic archived. No new replies allowed.