How to accept numbers only

I am a complete beginner at this program and has no experience but trying my best to catch up. So what I want to do with my program is I want my length, width,volume and price to accept number values only. This is what I have so far.

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
55
56
57
58
59
60
61
62
63
64
65
// This program calculates and displays the pool's volume
// the amount of water needed and the cost.
#include <iostream>
#include <iomanip>
using namespace std;


int main()

{
	// the double type accepts numbers including fractions for the following inputs.
	double length, width, depth, poolvolume, watervolume, price; 
	cout << "The program calculates and displays the pool's volume," << endl; // Description of the project
	cout << "the amount of water needed and the cost." << endl;
	cout << " " << endl;

	// Sets the number with two decimal places.
	cout << fixed << showpoint << setprecision(2); 

	// Input for length
	cout << "Enter the length: "; 
	cin >> length;

	// Input for width
	cout << "Enter the width: "; 
	cin >> width;

	// Input for depth
	cout << "Enter the depth: "; 
	cin >> depth;
	cout << " " << endl;

	// Displays the numbers entered for the inputs.
	cout << "With the pool that has the length: " 
		<< length << "," << " width: " << width 
		<< "," << " and depth: " << depth << "." <<  endl; 

	// Calculates the pool's volume.
	poolvolume = length * width * depth; 
	cout << "The pool's volume is " << poolvolume << " cubic feet." << endl;

	// Formula for solving the water volume of the pool.
	watervolume = length * width * ( depth - .25 ); 
	cout << "The amount of water needed is " << watervolume << " cubic feet." << endl;

	// Calculates the price for the pool.
	price = ( watervolume * .77 ) + 100; 
	cout << "The cost is $" << price << endl;

	// Name of the programmer.
	cout << endl;
	cout << "Programmer: Hue hue hue" << endl; 

	

	// A query for the user.
	cout << " " << endl;
	cout << "Run the program again (y/n)?";


	fflush(stdin);
	cin.get();
	return 0;

}


Any help is greatly appreciated
Last edited on
You can check the std::cin fail bit to see if the input failed. Here is a function that you can just use in your program to do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <limits>

double getDoubleNum() {
    double val;
    // While the fail bit is set
    while (!std::cin >> val) {
        // clear the fail bit
        std::cin.clear();
        // Ignore the rest of the contents of that line
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

        // Try to get the number again
        std::cout << "Please enter a valid number: ";
    }

    // We got a number, return it
    return val;
}


Use this code simply by calling the function and setting your value to it, like so:
1
2
3
4
double length;
//...
std::cout << "Enter the length: ";
length = getDoubleNum();
Topic archived. No new replies allowed.