Hi I am working on some code for my class that requires me to validate a floating point number. Once the user enters the number I have to check to see if it is valid, and it is only required that I check the first digit. so if -80 was entered it would be invalid, if something non numeric was entered it would be invalid.

I know of a bunch of ways to do this, but I can only use certain things for the assignment, so all 5+ ways I figured out how to do this aren't valid answers. Basically I have to do this with only float/char data types, loops, strings, arrays, cin/cin.peek()(this one is recommended to use by the teacher, though it doesn't seem to work on floats), and cin.get(), and of course cout. That is all I have to work with to get this to work. Any feedback would be appreciated as this has been puzzling me for some time as it seems impossible to isolate the first digit from the float number to check if it is valid.

Your professor says you have to accept user input as a floating point number, but he has no requirement for storing the variable as a float; therefore, I would simply accept all user input into a c-string (char array) and perform your validation. The following code uses isdigit() from the <cctype> library:

char user_input[100];

cout << "Enter a floating decimal number:  ";
cin >> user_input;

if(isdigit(user_input[0])
{
     cout << "\n\nValidation Correct. ";
}
else
{
     cout << "\n\n\aInvalid Float.  ";
}

yes, that does work, but I cannot use the library you suggested or use isdigit, and I need to be able to use the number after it is validated. Any ideas?

how about getting the input as string( or char array if you can't use string).

string num = "";
cin >> num;
if( validate(num)  == false ) //Error ...

Validate function :

bool validate(string num)
{
    if( num[0] < '0' || num[0] > '9') return false;
    return true;
}

Haven't tried it but it should work.

how about getting the input as string( or char array if you can't use string).

string num = "";
cin >> num;
if( validate(num)  == false ) //Error ...

Validate function :

bool validate(string num)
{
    if( num[0] < '0' || num[0] > '9') return false;
    return true;
}

Haven't tried it but it should work.

I agree that it should work.
I need to use the input data after it is validated in float form, is it possible to make the string data a float number after it is validated?

>>I need to use the input data after it is validated in float form, is it possible to make the string data a float number after it is validated?

Can you use sstream library?

or do you have to do it manually?

Q: are you allowed to use the atof( ) function? (part of the <cstdlib> library)

Q: are you allowd to use the <sstream> library?

If not, it would take some pretty decent math power to turn a c-string into a float.. perhaps the math would be less intense isolating the first digit of a float... but what if the first digit input by the user was not actually a digit, thus throwing the input stream into utter chaos...

>>If not, it would take a pretty decent about of math power to turn a c-string into a float.. perhaps the math would be less intense isolating the first digit of a float...

Won't be that hard, just need to know two things,
1) The isolated Number
2) The degree of that Number;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.