Handling user input of a type double variable

I am currently taking a course in C++ programming at college. I wrote the function getDoubleInput to better handle user input of a variable of type double. I would appreciate any feedback on possible flaws or ways to improve the code. Thanks!

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
47
48
49
50
51
52
53
54
#include <iostream>
#include <cctype>
#include <conio.h>
#include <cmath>
#include <string>

using namespace std;

void getDoubleInput(double &number, string parameter)
{
	char keypress (NULL);
	bool pointSet (false);
	int decimalPlace (1), numChars (0), count;

	number = 0.0;

	cout << "Please enter the " << parameter << ": ";

	while (keypress != 13)
	{
		keypress = _getch();

		if (isdigit(keypress) && !pointSet)
		{
			number = (number * 10) + (static_cast<double>(keypress - 48));
			cout << keypress;
			numChars++;
		}
		else if (keypress == 46 && !pointSet)
		{
			pointSet = true;
			cout << keypress;
			numChars++;
		}
		else if(isdigit(keypress) && pointSet)
		{
			number = number + (static_cast<double>(keypress - 48) / pow(10, decimalPlace));
			decimalPlace++;
			cout << keypress;
			numChars++;
		}
		else if (keypress == 8 && numChars > 0)
		{
			for (count = 0; count < numChars; count++)
			{
				cout << "\b \b";
			}
			pointSet = false;
			number = 0.0;
			numChars = 0;
			decimalPlace = 1;
		}
	}
}
Hey, I don' t know if this is the start of your code but, I don't see a "main()" function?
There's no main() function because this function is meant to be called from main(). The #includes listed are the ones the function requires. You call the function from main with:

getDoubleInput(number, parameter);

where number is a variable of type double and parameter is a variable of type string.
Topic archived. No new replies allowed.