I got the program to accept integers, now I just need it to accept only one decimal and one slash. I need help making it accept a decimal or a slash
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
bool getAreal(string);
string value;
float number;
cout << "Enter a real number\n";
getline(cin, value);
if(!getAreal(value))
cout << "The number you entered is not valid\n";
else
{
number = atoi(value.c_str());
cout << "The number you entered is " << value << endl;
}
system("pause");
return 0;
}
bool getAreal(string str)
{
int start = 0;
int i;
bool valid = true;
bool sign = false;
if (str.length() == 0) valid = false;
if(str.at(0) == '-' || str.at(0) == '+')
{
sign = true;
start = 1;
}
if (str.at(1) == '.') valid == true;
if (sign && str.length() == 1) valid = false;
i = start;
while(valid && i < str.length())
{
if(!isdigit(str.at(i))) valid = false;
i++;
}
return valid;
}