validate the text as a double data type and convert the string to that double.

I need to validate the text as a double data type. After trying to run the program I wrote , Dev is giving me an error at shown location **** on code. I feel stuck and am asking for hints on what direction to go in next. Thank you - Maria

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
66
67
68
69
70
71
72
73
74
#include<iostream>                          //Preprocessor commands
#include<cmath>
using namespace std;                        //Library location

int get_double();

int main()                                                 //Driver function header
{
    char ch;
        
    cout<<"               ****Double Validation Check Program****\n\n";
    
    cout<<"Enter a double data type number: ";
      
    cout<<"...You entered the double: "<<get_double()<<endl<<endl;
    
    cout<<"\n\nEnter 'e' to exit... ";                      //Hold screen for //viewing
    cin>>ch;
    return 0;                                               //End of program.
}

int get_double()
{
    //This function does a character-by-character check for a valid double
    //Check includes:  first character must be '+', '-', '.' or a digit //including 0. 
    //The remaining characters must all be digits.  No scientific or exponential
    //notation is allowed for entry.  Maximum string length is 9.
    
    double d;
    string line;
        
    while (true)
    {
       try
       {
           getline(cin,line); 
           
           //Check for zero length string or max value for double
           if ((line.length() == 0)||(line.length()) > 9)  throw 0;
           
           //Check for first +, - or digit (non-zero)    
           if ((line.at(0)=='+')||(line.at(0)=='-')||(isdigit(line.at(0)|| (line.at(0)=='.')))
           {//******error showing here: (In function `int get_double()': //expected `)' before '{' token) 
              //Check remaining //characters                                                                
              if (line.length() > 1)
              {
                                 
                  for (d=1;d<line.length();d++)    if (!(isdigit(line.at(d))))   throw 0;
                    
                  //String is OK double 
                  throw 1;   
              }
              //String must be of one character that is +, -, . or a digit
              else
              {
                  //A single + or - is not valid
                  if ( (line.at(0) == '+') ||(line.at(0) == '-') ) throw 0;
                  else throw 1;
              }    
           }
           else   throw 0;
       }    
       catch(double e)  
       {
           //Error has been detected      
           if (e == 0)  cout<<"...Invalid double entered...enter a valid double: ";
            
           //Covert valid string to an actual double
           if (e == 1) return atoi(line.c_str());
       } 
    }          
    return 0;
}          
You missed a closing parenthesis on line 42:
/* ... */ || (isdigit(line.at(0) || /* ...*/
ok, I have found some mistakes and corrected them but still getting same error.

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
66
67
68
69
70
71
72
73
74
#include<iostream>                          //Preprocessor commands
#include<cmath>
using namespace std;                        //Library location

double get_double();

int main()                                                 //Driver function header
{
    char ch;
        
    cout<<"               ****Double Validation Check Program****\n\n";
    
    cout<<"Enter a double data type number: ";
      
    cout<<"...You entered the double: "<<get_double()<<endl<<endl;
    
    cout<<"\n\nEnter 'e' to exit... ";                      //Hold screen for viewing
    cin>>ch;
    return 0;                                               //End of program.
}

double get_double()
{
    //This function does a character-by-character check for a valid double
    //Check includes:  first character must be '+', '-', '.' or a digit inclduing 0. 
    //The remaining characters must all be digits.  No scientific or exponential
    //notation is allowed for entry.  Maximum string length is 9.
    
    double d;
    string line;
        
    while (true)
    {
       try
       {
           getline(cin,line); 
           
           //Check for zero length string or max value for double
           if ((line.length() == 0)||(line.length()) > 9)  throw 0;
           
           //Check for first +, -, . or digit (non-zero)    
           if ((line.at(0)=='+')||(line.at(0)=='-')||(isdigit(line.at(0)|| (line.at(0)=='.')))
           {
              //Check remaining characters                                                                
              if (line.length() > 1)
              {
                                 
                  for (d=1;d<line.length();d++)    if (!(isdigit(line.at(d))))   throw 0;
                    
                  //String is OK double 
                  throw 1;   
              }
              //String must be of one character that is +, -, . or a digit
              else
              {
                  //A single + or - is not valid
                  if ( (line.at(0) == '+') ||(line.at(0) == '-') ) throw 0;
                  else throw 1;
              }    
           }
           else   throw 0;
       }    
       catch(double e)  
       {
           //Error has been detected      
           if (e == 0)  cout<<"...Invalid double entered...enter a valid double: ";
            
           //Covert valid string to an actual double
           if (e == 1) return atoi(line.c_str());
       } 
    }          
    return 0;
}          
if ((line.at(0) == '+') || (line.at(0) == '-') || (isdigit(line.at(0) || (line.at(0) == '.'))) You forgot a closing parenthese.

Also you need to include the string header file
Thank you, I made the corrections you advised. still not working... any other thoughts?
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
66
67
68
69
70
71
72
73
74
75
#include<iostream>                          //Preprocessor commands
#include<cmath>
#include<string>
using namespace std;                        //Library location

double get_double();

int main()                                                 //Driver function header
{
    char ch;
        
    cout<<"               ****Double Validation Check Program****\n\n";
    
    cout<<"Enter a double data type number: ";
      
    cout<<"...You entered the double: "<<get_double()<<endl<<endl;
    
    cout<<"\n\nEnter 'e' to exit... ";                      //Hold screen for viewing
    cin>>ch;
    return 0;                                               //End of program.
}

double get_double()
{
    //This function does a character-by-character check for a valid double
    //Check includes:  first character must be '+', '-', '.' or a digit inclduing 0. 
    //The remaining characters must all be digits.  No scientific or exponential
    //notation is allowed for entry.  Maximum string length is 9.
    
    double d;
    string line;
        
    while (true)
    {
       try
       {
           getline(cin,line); 
           
           //Check for zero length string or max value for double
           if ((line.length() == 0)||(line.length()) > 9)  throw 0;
           
           //Check for first +, -, . or digit (non-zero)    
           if ((line.at(0)=='+')||(line.at(0)=='-')||(isdigit(line.at(0)|| (line.at(0)=='.'))))
           {
              //Check remaining characters                                                                
              if (line.length() > 1)
              {
                                 
                  for (d=1;d<line.length();d++)    if (!(isdigit(line.at(d))))   throw 0;
                    
                  //String is OK double 
                  throw 1;   
              }
              //String must be of one character that is +, -, . or a digit
              else
              {
                  //A single + or - is not valid
                  if ( (line.at(0) == '+') ||(line.at(0) == '-') ) throw 0;
                  else throw 1;
              }    
           }
           else   throw 0;
       }    
       catch(double e)  
       {
           //Error has been detected      
           if (e == 0)  cout<<"...Invalid double entered...enter a valid double: ";
            
           //Covert valid string to an actual double
           if (e == 1) return atoi(line.c_str());
       } 
    }          
    return 0;
}          
You still haven't changed line 43...

You have
1
2
if (/* ... */ || (isdigit(line.at(0) || /* ... */)
// Note: missing parentheses here   ^ 
What you should have is
1
2
if (/* ... */ || (isdigit(line.at(0))) || /* ... */ )
// Added closing parentheses here   ^^ 
Thank you for your help
At this point this is what I have..... but I when running it I can not put a decimal in any place but 0 (index) or it advises not a double. Any ideas?
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
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>                          //Preprocessor commands
#include<string>
using namespace std;                        //Library location

double get_double();

int main()                                                 //Driver function header
{
    char ch;
        
    cout<<"               ****Double Validation Check Program****\n\n";
    
    cout<<"Enter a double data type number: ";
      
    cout<<"...You entered the double: "<<get_double()<<endl<<endl;
    
    cout<<"\n\nEnter 'e' to exit... ";                      //Hold screen for viewing
    cin>>ch;
    return 0;                                               //End of program.
}

double get_double()
{
    //This function does a character-by-character check for a valid double
    //Check includes:  first character must be '+', '-', '.' or a digit inclduing 0. 
    //The remaining characters must all be digits.  No scientific or exponential
    //notation is allowed for entry.  Maximum string length is 9.
    
    double d;
    string line;
        
    while (true)
    {
       try
       {
           getline(cin,line); 
           
           //Check for zero length string or max value for double
           if ((line.length() == 0)||(line.length()) > 9)  throw 0;
          
           //Check for first +, -, . or digit (non-zero)    
           if ((line.at(0)=='+')||(line.at(0)=='-')||(line.at(0)=='.')||(isdigit(line.at(0))))
          
           {
              //Check remaining characters                                                                
              if (line.length() > 1)
              {
                                 
                  for (d=1;d<line.length();d++)    if (!(isdigit(line.at(d))))   throw 0;
                    
                  //String is OK double 
                  throw 1;   
              }
              //String must be of one character that is +, -, . or a digit
              else
              {
                  //A single + or - is not valid
                  if ( (line.at(0) == '+') ||(line.at(0) == '-') ) throw 0;
                  else throw 1;
                  
              }    
           }
           else   throw 0;
       }    
       catch(int e)  
       {
           //Error has been detected      
           if (e == 0)  cout<<"...Invalid double entered...enter a valid double: ";
            
           //Covert valid string to an actual double
           if (e == 1) return atof(line.c_str());
       } 
    }          
    return 0;
}          
Topic archived. No new replies allowed.