Validate floats

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Theadmin77
    New Member
    • Nov 2006
    • 19

    Validate floats

    I have to validate s floats thru a function , but the validation have to be don with a string of 20 characters ....

    Any idea how to do thatvalidation floats?

    Any help will be very appreciated !!!

    This is the code so far :


    #include <iostream>
    #include <cstdlib>
    using namespace std;

    struct project
    {
    float latitude;
    float longitud;
    float time;
    float distance;
    float altitude;
    // float latitude;
    // float longitude
    };

    struct projectone
    {

    project source;
    project travel;
    project target;
    float speed;
    float azimuthal;
    float equatorial;
    };

    bool getfloat (float *);

    void main()

    {
    projectone projectile [100];
    bool validata;
    float value= 0.0;



    do
    {
    cout<< "Enter the Launch speed :";
    validata=getflo at(projectile.s peed);

    if (!validata)
    {
    cout << "Invalid Input .Try again...\n";
    }
    }while (!validata);

    do
    {
    cout<< "Enter the Launch azimuthal angle :";
    validata=getflo at(projectile.a zimuthal);

    if (!validata)
    {
    cout << "Invalid Input .Try again...\n";
    }
    }while (!validata);

    do
    {
    cout<< "Enter the Launch equatorial angle :";
    validata=getflo at(projectile.e quatorial);

    if (!validata)
    {
    cout << "Invalid Input .Try again...\n";
    }
    }while (!validata);

    ` do
    {
    cout<< "Enter the source latitude :";
    validata=getflo at(projectile.s ource.latitude) ;

    if (!validata)
    {
    cout << "Invalid Input .Try again...\n";
    }
    }while (!validata);

    do
    {
    cout<< "Enter the source longitude :";
    validata=getflo at(projectile.s ource.longitude );

    if (!validata)
    {
    cout << "Invalid Input .Try again...\n";
    }
    }while (!validata);





    }

    // function to validate

    bool getfloat (float *value)
    {
    int i;
    bool noerrors=true;
    i=0;


    if (*value <
    {
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you can validate float input with code such as
    Code:
    float getFloat()
    {
    	  float f;
    	  while(! (cin >> f))		// attempt to read a float
    		  {		     // if error, ask again
    		   cout << "error, please reenter ? ";
    		   cin.clear();		   // clear error
    		   cin.ignore(100,'\n');  // remove faulty characters from line
    		  }
    	  return f;			  // OK, return value
    }
    if the user enters invalid characters the message will be displayed and they can enter again

    Comment

    Working...