View Single Post
  #3 (permalink)  
Old May 9th, 2004, 12:01 PM
Maxood Maxood is offline
Authorized User
 
Join Date: Feb 2004
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here is the code where i would like to validate user input.I just need to limit my user to input numeric data only:
/***********************PROGRAM TO CALCULATE VOLUME AND WEIGHT OF THE SPHERE******************/
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <math.h>

/*******CONSTANT DECLARATION*******/
const double PI =3.14159;

/***FUNCTION PROTOTYPES***/
double CalcVol(double);
double CalcWeight(double,double);

void main()
{

    double radius=0.0, density = 0.0, volume=0.0;
    char res;


do
{
    clrscr();//Clears the screen
    cout<<"********PROGRAM TO CALCULATE DENSITY AND VOLUME OF SPHERE********"<<"\n\n\n";
    do
    {
    cout<<"Enter radius of the sphere: ";
    cin>>radius;
        if (radius<0)
        {
            cout<<"\nThe radius of the sphere cannot be negative\n\n";
        }
        else
        {
            volume = CalcVol(radius);
            cout<<"\nThe volume of the sphere is "<<volume<<"\n\n";

        }
    }while(radius<0);

    do
    {
    cout<<"Enter density of the sphere: ";
    cin>>density;
        if (density<0)
        {
            cout<<"\nThe density of the sphere cannot be negative\n\n";
        }
        else
        {
            cout<<"\nThe weight of the sphere is "<<CalcWeight(density,volume)<<"\n\n";
        }
    }while(density<0);
    do
    {
        cout<<"Do you want to continue(y or n)?";
        res = getche(); //stores just one character
        if (res!='y' && res!='n')
            cout<<"\nEnter y for YES or n for NO"<<"\n\n";
    }while(res!='y' && res!='n');

}while(res=='y');

cout<<endl;
}


/*****************FUNCTION TO CALCULATE VOLUME OF SPHERE**********/
double CalcVol(double r)
{
    return (4.0/3.0) * PI * pow(r,3.0);
}

/*****************FUNCTION TO CALCULATE WEIGHT OF SPHERE**********/
double CalcWeight(double d,double v)
{
    return d * v;
}


   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
Reply With Quote