(Useful utility) a. Write a C++ function named isvalidReal() that checks for a v
ID: 3572433 • Letter: #
Question
(Useful utility) a. Write a C++ function named isvalidReal() that checks for a valid double- precision number. This kind of number can have an optional + or - sign, at most one decimal point (which can be the first character), and at least one digit between 0 and 9. The function should return a Boolean value of true if the entered number is a real number; otherwise, it should return a Boolean value of false. b. Modify the isvalidReal() function written for Exercise 9a to remove all leading and trailing blank spaces from its string argument before determining whether the string corresponds to a valid real number.
Explanation / Answer
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int isvalidReal(string);
int main()
{
string A;
bool TOF;
cout<<"Please type in any real number with an optional +/- sign in front: ";
cin>>A;
TOF= isvalidReal(A);
if(TOF==1)
cout<<"The number is a real number"<<endl;
else
cout<<"The number is not a real number"<<endl;
system("pause");
return 0;
}
int isvalidReal(string a)
{
bool tof; int i= 0, j= 1; string str="+-.";
for(i= 0; i<=a.length(); i++){
if(isdigit(a[i])){
tof= 1;
}
else if(a[0]==(str[0]||str[1]) && a[1]==isdigit(a[1])){
tof= 1;
}
else if((a[i]==str[2]) && j==1)
{
tof= 1;
j= 0;
}
Else
{
tof= 0;
break;
}
}
return tof;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.