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
#include<iostream> //Preprocessor commands
#include<cmath>
usingnamespace 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;
elsethrow 1;
}
}
elsethrow 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;
}
#include<iostream> //Preprocessor commands
#include<cmath>
usingnamespace 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;
elsethrow 1;
}
}
elsethrow 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;
}
#include<iostream> //Preprocessor commands
#include<cmath>
#include<string>
usingnamespace 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;
elsethrow 1;
}
}
elsethrow 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;
}
#include<iostream> //Preprocessor commands
#include<string>
usingnamespace 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;
elsethrow 1;
}
}
elsethrow 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;
}