Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 22nd, 2004, 01:46 PM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default Input Validation

I want my user to limit to just input numeric data i.e. if the user types non numeric data ..., the cusrsor must not display anything and shouldn't move.
Vear in mind that i'm using cin and cout functions to input and output data.Please let me know how to do this....thanx in advace

   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
__________________
   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
Reply With Quote
  #2 (permalink)  
Old April 28th, 2004, 02:03 AM
Authorized User
 
Join Date: Mar 2004
Location: Ranchi, Jharkhand, India.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

The very first thing is plz. tell me why you are stick with using cin and cout? if wanna do in your way you just overload this with a function containg C type of validation. I think you can do this by trapping the key using c=getchar() and checking the input value for ASCII in alphabets range.

Just tried......
Plz...Don't......
Regards.


Reply With Quote
  #3 (permalink)  
Old May 9th, 2004, 12:01 PM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
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
  #4 (permalink)  
Old June 24th, 2004, 04:04 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

WEll masood,

as kumar said you should use getch() & getche() for it ...

i have that function written with me for this but currently i am out of station so if you want that function you will have to wait for about 10 days ...

that function will input value from the user and return the string just as wanted just numeric ....

Sudhan.

Reply With Quote
  #5 (permalink)  
Old June 24th, 2004, 08:55 AM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Kumar,
Thanx a lot for your kind reply.I'd be keenly interested to look into that function...no matter after 10 dayz..but please do mail me.
Keep in touch...thanx


   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
Reply With Quote
  #6 (permalink)  
Old June 24th, 2004, 09:01 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi maqsood ,

Surely i will post that function as soon as possible but my name is SUDHAN nad not Kumar ...

haha ...

Sudhan.

Reply With Quote
  #7 (permalink)  
Old June 24th, 2004, 09:10 AM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

im sorry...it was a typo error.I'll be verinterested to look in to that function.
Waiting for your reply....
Maxood

   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
Reply With Quote
  #8 (permalink)  
Old July 1st, 2004, 07:24 AM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

Hi Maxood,

R u still interested in that function .. bcoz somehow i am not able to find it ... but since i know the logic i can develop it again if u r interested ...

Sudhan.

--------------------------------------------------
Sudhan Kanitkar.
Everything Is In Our Hands.
Reply With Quote
  #9 (permalink)  
Old July 1st, 2004, 07:12 PM
Authorized User
 
Join Date: Feb 2004
Location: Islamabad, , Pakistan.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm as usual interested to have a look into that function.My primamry development language is VB and i used to validate data entry by using ascii codes for characters.
Now i wonder how we can do the same thing in C++ by using functions...thanx for keeping in touch.
Waiting for your reply....

   CEO
InteliSoft

Maqsood ur Rahman
Life:An Endless Journey towards Perfection
Reply With Quote
  #10 (permalink)  
Old July 3rd, 2004, 12:56 PM
Authorized User
 
Join Date: Jun 2004
Location: , , India.
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to silver_cuts Send a message via Yahoo to silver_cuts
Default

HI maxood,

thats my plan ... to use ascii codes of individual characters.. .. u wait for another two days ... i will have that function ready for you

Sudhan.


--------------------------------------------------
Sudhan Kanitkar.
Everything Is In Our Hands.
Reply With Quote




Similar Threads
Thread Thread Starter Forum Replies Last Post
Input Validation sams ASP.NET 2.0 Professional 4 June 12th, 2007 03:29 AM
Validation to input fields amol_0008 XML 1 May 30th, 2007 04:36 AM
Input validation in JFormattedTextField harini19 Java Basics 2 November 14th, 2006 01:50 AM
Input validation shs BOOK: Beginning ASP.NET 1.0 14 January 6th, 2005 01:03 PM
Validation of TextBox Input bpgadhia ASP.NET 1.0 and 1.1 Professional 1 March 16th, 2004 05:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.