So, I'm trying to figure out how to put a decimal point in my balance.
My program has increments for cents, dimes, dollars, and tens of dollars.
For example,
If 'a1' (cents) is entered = $.01
If 's3' (dimes) is entered = $.31
If 'd6' (dollars) is entered = $6.31
#include <iostream>
#include "redcounter.h"
//* Library needed *//
usingnamespace std;
// Start of main function //
int main()
{
GroceryCounter counter; // class of red little counter
int x = counter.count; // count for the digits entered
int y = counter.count;
char input[2], choice; // input and choice for the increment
cout << "Welcome to Mom's Grocery Counter!" << endl;
cout << "Enter your money using letters a, s, d, f, or o. ";
cout << "This follows by the number 1-9." << endl;
cout << "Use a for cents, s for dimes, d for dollars, and f for tens of dollars." << endl;
cout << "Enter x to do another calculation." << endl;
cout << "Example: s8" << endl;
do {
cout << endl;
cout << "Enter input: "; // asked user to enter input
cin >> input;
int x = 0;
y = input[1] - '0';
switch (input[0]) // switch statement to input cents, dimes, dollars and tens of dollars
{
case'a':
case'A':
for (x = 0; x < y; x++)
{
counter.increment1(); // cents
}
if (counter.overflow())
{
counter.reset();
}
break;
case's':
case'S':
for (x = 0; x < y; x++)
{
counter.increment10(); // dimes
}
if (counter.overflow())
{
counter.reset();
}
break;
case'd':
case'D':
for (x = 0; x < y; x++)
{
counter.increment100(); // dollars
}
if (counter.overflow())
{
counter.reset();
}
break;
case'f':
case'F':
for (x = 0; x < y; x++)
{
counter.increment1000();
} // tens of dollars
if (counter.overflow())
{
counter.reset();
}
break;
case'r':
case'R':
{
counter.reset(); // resets balance
}
default:
break;
}
cout << "Your balance is $" << counter.count << endl;
cin >> choice;
}while (choice == 'x' || choice == 'X'); // press x to do another
system("pause");
return 0;
}
GroceryCounter::GroceryCounter()
{
count = 0;
maxCount = 9999;
}
GroceryCounter::GroceryCounter(int value)
{
maxCount = value;
count = 0;
}
void GroceryCounter::reset() // reset function
{
count = 0;
}
void GroceryCounter::increment1() // increase cents by 1
{
count += 1;
}
void GroceryCounter::increment10() // increase dimes by 10
{
count += 10;
}
void GroceryCounter::increment100() // increase dollars by 100
{
count += 100;
}
void GroceryCounter::increment1000() // increase tens of dollars by 1000
{
count += 1000;
}
bool GroceryCounter::overflow() // overflow function
{
if (count > maxCount)
{
cout << "Overflow has occured. Your balance has been resetted. Press x to start over or press any key to exit." << endl;
returntrue;
}
returnfalse;
}
Enter input: a1
Your balance is $1
x
Enter input: s3
Your balance is $31
x
Shouldn't this output $0.31?
When you're calling incrementN(), you're treating count as if it were in cents. To convert cents to dollars, you'll need to divide by 100. This will give the correct output. Note that integer division will truncate the fractional part.