I believe this code below works for checking if the user
entered more than 2 decimal places, like if your program
uses money which is always 2 decimal places.
Anyways I tested this code as much as possible and it took
me quite a while to figure out...almost pulled my hair out

.
I can't say that it will work perfect but I tested it and it seems
to be outputting me the right answer every time.
I hope this helps anyone and if anyone wants to find any
bugs in it or something bad about it tell me

.
Code: Select all
/*This little bit of code is for checking user input for
decimals for anything related to money which evaluates to
2 decimal places*/
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
double userInput, checkInput;
string newInput;
cout << "Enter a precision number: ";
cin >> userInput;
cin.ignore();
checkInput = userInput - int(userInput); //Subtracts leaving only the decimal numbers
stringstream output;
output << checkInput;
newInput = output.str(); //Converts newInput into string
//If newInput is for instance 1.0, 1.00, etc, it is valid
if(int(newInput.length()) == 1 || int(newInput.length()) == 2)
cout << "Number is valid\n";
else if(int(newInput.length()) == 3)
cout << "Number is 1 decimal place\n";
else if(int(newInput.length()) == 4)
cout << "Number is 2 decimal places\n";
else
cout << "Invalid entry...number is more than 2 decimals\n";
return 0;
}
Sorry forgot to say thanks to both of you

.