validation on double and string

Oct 16, 2017 at 2:56pm
how to i validate the user's input is double or string?
i want know how to validate double from the user's input.

#include<iostream>
double dInput;


cout<<"Enter a decimal value";
cin>>dInput;

if(dInput!=double)//i see someone is writing in another web but is an error
{
cout<<"invalid input";
}
Oct 16, 2017 at 3:32pm
What do you consider invalid input?

If all you want to know is that you retrieved a valid number then just testing the stream state should be sufficient.

Oct 16, 2017 at 3:52pm
this is just an example i want to know how to i validate the user's input so that i can continue doing my calculation. if user's input is string or invalid input, how i know and how i use if and else to do the validation.
Oct 16, 2017 at 4:01pm
Then, as I said previously, simply testing the stream state should be sufficient.

Do you know how to test the stream state?

Oct 16, 2017 at 4:09pm
how to test, i want test angle is double or char or string from user's input?
Oct 16, 2017 at 4:23pm
Yes, and do you know what happens if you try to force cin to take invalid input?

If, before the stream encounters any numeric digit, it encounters a non-numeric digit the stream will fail. This failure can be detected by testing the stream state. So for this simple program something like:

1
2
3
4
5
6
7
double number;

cin >> number;

if(!cin) // Error trying to read the number.
   return 1;  // Return to the operating system because of bad input.


Oct 17, 2017 at 9:15am
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
#include <iostream>
#include <string>
using namespace std;


int main()
{
   double x;
   bool ok;

   cout << "Enter a double: ";                   

   do
   {
      ok = true;
      cin >> x;
      if ( !cin )                           // Check state of stream to check that there is something numerical FIRST
      {
         ok = false;
         cin.clear();                       // Clear error flags
         cin.ignore( 1000, '\n' );          // Empty any reasonable input buffer
      }
      else    
      {
         string dummy;
         getline( cin, dummy );             // Check rest of stream to check that there is nothing non-numerical AFTER
         if ( dummy.find_first_not_of( " " ) != string::npos ) ok = false;
      }

      if ( !ok ) cout << "Invalid double; try again\n";
   } while ( !ok );

   cout << "Value is " << x;
}


Enter a double: r3.14
Invalid double; try again
3.14r
Invalid double; try again
3.14   Hello
Invalid double; try again
3.14
Value is 3.14



This is for demonstration only. If you are doing much more than this, then make sure the input and validation goes in a separate function; otherwise it will clutter up main() terribly.
Last edited on Oct 17, 2017 at 2:33pm
Oct 17, 2017 at 2:28pm
thks, can work already.
Oct 17, 2017 at 3:05pm
With some templating you can get it to work for other types. Better as a separate function, I think.

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
#include <iostream>
#include <string>
using namespace std;

//======================================================================

template <typename T> void getInput( string prompt, T &x )
{
   bool ok;

   cout << prompt;
   do
   {
      ok = true;
      cin >> x;
      if ( !cin )                           // Check state of stream to check that there is something of the required type FIRST
      {
         ok = false;
         cin.clear();                       // Clear error flags
      }
      string dummy;
      getline( cin, dummy );                // Check rest of stream to check that there is nothing else AFTER
      if ( dummy.find_first_not_of( " " ) != string::npos ) ok = false;

      if ( !ok ) cout << "Invalid input; try again\n";
   } while ( !ok );
}

//======================================================================

int main()
{
   int n;
   double x;


   getInput( "Enter an int\n", n );
   getInput( "Enter a double\n", x );

   cout << "int value is " << n << '\n';
   cout << "double value is " << x << '\n';   
}

//====================================================================== 

Last edited on Oct 17, 2017 at 3:12pm
Topic archived. No new replies allowed.