Hello fellow coders!
This is my first time and post on forum but hope to stay here seems like a good friendly place
Anyway the following code is to count the number of digits on user input. This includes decimal numbers. So far the code does what it does
1. Can this code be written more better, or possibly using different better objects etc?
2. How to get rid of system("pause")? I'm in the habit of using it and i dunno how to write in a way to halt program so user can see answer. I'm aware of using cin.get etc but don't know how to use it here.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
// define user input number and counter
long double number; int DigitCount = 0;
cout << "Enter a large number (can include decimal points) : ";
cin >> number;
// split the number into non decimal and decimal part
int number1;
long double number2;
number1 = (int)floor(number); // store the non-decimal part
number2 = number - number1; // store the decimal part
// count the number of non-decimal integers
while (number1 > 0) {
number1 /= 10;
DigitCount++;
}
// count the number of decimal integers
while (number2 - floor(number2+0.0001) > 0.001) {
// the while condition ensures that we have the difference
// as zero once the decimal places are all zero
// for some reason if the computer complies to do
// number2 - floor(number2)
// and we know that number2 variable has no decimal places
// the output is not zero, hence the tolerance condition
number2 *= 10;
DigitCount++;
}
// output number of digits for user
cout << "\n\n Your number is " << DigitCount
<< " digits long\n";
system("pause");
return 0;
}