\" I can getthis to work with everything except making a number with more than o
ID: 3536244 • Letter: #
Question
" I can getthis to work with everything except making a number with more than one '.' invalid. What am I doing wrong? Help please.
[#include <iostream>
#include <iomanip>
#include<cstring>
#include<cctype>
using namespace std;
bool isValidDouble(char[],int);
void readDouble (char[],int);
const int Size = 10;
char Numbers [Size];
bool isValidDouble (char numbers [], int size)
{
int count;
int Deccount = 0;
for (count=0;count <Size; count ++)
{ if (isdigit(Numbers[count]))
return true;
else if (((Numbers[count]) == '.') && (Deccount == 0))
{
Deccount++;
return true;
}
else
return false;
}
}
void readDouble (char Numbers[], int Size)
{
if (isValidDouble(Numbers, Size))
cout <<"That is a valid number"<<endl;
else {cout<< "That is not valid. Please retry"<<endl;}
}
void main ()
{cout<<"Please enter a real number:"<<endl;
cin.getline(Numbers,Size);
readDouble(Numbers,Size);
}
Explanation / Answer
else if (((Numbers[count]) == '.') && (Deccount == 0))
is where your problem lies in. As the first "." is found and deccount is 0 it'll return true. so it doesn't matter how many "."s you have as long as theres one it'll return true. A way to fix that is to remove that return and check it after looping through the array and seeing if only more than 1 "." appeared.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.