Wrox Programmer Forums
|
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 25th, 2008, 12:38 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default Validation in C++

I want to do validation. I need to make sure that input is an integer. What is the function in C++ that determines that the given input is an integer. And if the input is not an integer then the cin>> statement remains there.
Plz guide me with a piece of code eg. lets say the user has to enter his age or marks obtained in a subject.

MAXOOD!

Life is an endless journey towards perfection
__________________
MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #2 (permalink)  
Old July 12th, 2008, 12:20 AM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i've found this working example you can investigate used functions to understand the code in detail.

#include <iostream>
using namespace std;

int main()
{
    int nAge;

    while (1)
    {
        cout << "Enter your age: ";
        cin >> nAge;

        if (cin.fail()) // no extraction took place
        {
            cin.clear(); // reset the state bits back to goodbit so we can use ignore()
            cin.ignore(1000, '\n'); // clear out the bad input from the stream
            continue; // try again
        }

        cin.ignore(1000, '\n'); // clear out any additional input from the stream
        if (cin.gcount() > 1) // if we cleared out more than one additional character
            continue; // we'll consider this input to be invalid

        if (nAge <= 0) // make sure nAge is positive
            continue;

    break;
    }

    cout << "You entered: " << nAge << endl;
    return 0;
}

Reply With Quote
  #3 (permalink)  
Old July 22nd, 2008, 01:18 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default

Thankyou so much for this useful code.I think the same technique we would use for strings or characters.Please comment, lets say i want the user to enter names and the user is entering numbers or special characters.Please provide an example.
Again thanks a lot!

MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #4 (permalink)  
Old July 23rd, 2008, 01:26 AM
Authorized User
 
Join Date: Jan 2008
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have an idea..but i am not sure if it is the best solution..I thought if you dont need any spaces for the user to enter his/her name and surname.You can test each of the pressed keys with standart character functions like isalpha() so you are sure entered character is alphabetic and if it is alphabetic you write it on screen and save it.Otherwise you do nothing..Another solution is simple but not pretty..You can read all the line to a string variable and then in a for loop or with the help of a standart function like find_first_not_of you can search for the first character that is not alphabetic and if you find such a character you either terminate the program or reset the string and ask again..First solution is prettier but i couldn't manage to provide an example because i could not control console output everything that i write number or not displays in console output.but cin.get() function with combination isalpha() works pretty well but since i could not control the output using string class to read whole line is a better idea..But if you want to go C-Style here is your code:

#include <cstdio>
#include <cctype>

int main ()
{
  char c;
  char name[20]={};
  int i = 0;

  puts ("Enter yourname");

  do {
    c=getchar();
    if(isalpha(c))
        name[i++]=c;
  } while (c!='\n');

  puts(name);

  return 0;
}

only the alphabetic characters will be read into character array other characters is just ignored here..




Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
validation mikedeepak ASP.NET 2.0 Basics 0 May 28th, 2007 03:20 AM
Validation g_vamsi_krish ASP.NET 1.0 and 1.1 Professional 2 January 11th, 2006 06:46 AM
Validation koneruvijay ADO.NET 1 January 6th, 2004 09:22 AM





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