Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Input validation

Input validation

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vishwadev
    wrote on last edited by
    #1

    Hi, I need your help plz. I have written a very simple program which prompt a user to enter his name and age. I only want to add some validation so that the user cannot enter numbers when he is asked to enter the name and prevent the user from inputting characters when prompted for age. I am using cin.getline(). Is there any way that i can do that. Please help.Thanks

    A M 2 Replies Last reply
    0
    • V vishwadev

      Hi, I need your help plz. I have written a very simple program which prompt a user to enter his name and age. I only want to add some validation so that the user cannot enter numbers when he is asked to enter the name and prevent the user from inputting characters when prompted for age. I am using cin.getline(). Is there any way that i can do that. Please help.Thanks

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      Regular expressions would be of use. Boost[^] library implements them for C++.

      1 Reply Last reply
      0
      • V vishwadev

        Hi, I need your help plz. I have written a very simple program which prompt a user to enter his name and age. I only want to add some validation so that the user cannot enter numbers when he is asked to enter the name and prevent the user from inputting characters when prompted for age. I am using cin.getline(). Is there any way that i can do that. Please help.Thanks

        M Offline
        M Offline
        Mel Feik
        wrote on last edited by
        #3

        you can also consider using the isdigit, and isalpha routines found in ctype.h Of all things I've lost... I miss my mind the most -mjf

        T 1 Reply Last reply
        0
        • M Mel Feik

          you can also consider using the isdigit, and isalpha routines found in ctype.h Of all things I've lost... I miss my mind the most -mjf

          T Offline
          T Offline
          tgrr
          wrote on last edited by
          #4

          Hi Thanks for the replies. You know am very new to C++, can you people please set a small example for me to elaborate on. Thanks

          M 1 Reply Last reply
          0
          • T tgrr

            Hi Thanks for the replies. You know am very new to C++, can you people please set a small example for me to elaborate on. Thanks

            M Offline
            M Offline
            Mel Feik
            wrote on last edited by
            #5

            okay... skipping the ctype.h and regular expressions libraries, i've typed out a quick, simple example that shows how to validate a users input. It allows you to use cin.getline() and shows how to convert ASCII to int. I didn't intend to write it to do your homework but its more than sufficient for you to figure how to apply the basics to get your assignment done, and you really shouldn't use as is for any decent professor will dock you points for not writing better code - like I said... just to give you some insight. Good Luck #include <string.h> #include <iostream.h> #include <stdlib.h> bool ValidateNumericInput(char* psBuf); int main(int argc, char* argv[]) { char sBuf[255]; do { cout << "enter a number" << endl; cin.getline(sBuf, 255, '\n'); }while ( !ValidateNumericInput(sBuf)); // at this point, the data in sBuf (to the NULL char) is numeric but in ASCII int iConverted = atoi(sBuf); // this converts the ASCII to an int cout<< endl << endl << ++iConverted; return 0; } // both cout statements were include just to display the results bool ValidateNumericInput(char* psBuf) { for (unsigned int index = 0; index < strlen(psBuf); index++) { if( psBuf[index] < 0x30 || psBuf[index] > 0x3F ) { cout << "'" << psBuf << "' - is an invalid entry" << endl << endl; return false; } } cout << "input was all numbers" << endl; return true; }//end of ValidateNumericInput() // there are many ways to achieve your desired result but I thought this might // give you some ideas Of all things I've lost... I miss my mind the most -mjf

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups