Prevent user from typing keys - consoleapp

This is a discussion on Prevent user from typing keys - consoleapp within the C++ Programming forums, part of the General Programming Boards category; Hi Everyone - I have no idea how to approach this. I was given an assignment to check to see ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Question Prevent user from typing keys - consoleapp

    Hi Everyone -

    I have no idea how to approach this.

    I was given an assignment to check to see if a user has entered a valid ISBN number which is a piece of cake to write. This is a consoleapp program.

    My question is does C++ have any libraries / functions that I can use that will prevent the user from entering anything else besides numerical and alphabetic characters. The thing is I don't want them to allow them to enter any "-" or invalid characters such as "^" , "*", and the like.

    Thanks for any insight,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    Read the input as a string, then validate the input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    There is no such function ( at least not without some weird hacking you won't want to do ). However, you can read the input char by char. If you read it without an echo ( means you get the char, but it does not appear on screen ) you can check if it's an allowed character and echo it yourself. If it's not allowed, ignore it. For the user, it looks like he never typed it.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    something like this:

    Code:
    getline(cin, isonum '\n');
    
    for(int i = 0; i < isonum.length(); i++)
    {
        if(isonum[i] == "") // validate
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You're missing a comma.

    If you read it without an echo ( means you get the char, but it does not appear on screen )
    See the FAQ for how to do this. (You'll have to use a non-standard function like getch().)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanks for some hints!

    I am confused on how to echo a character to myself , and then check if it is valid.

    Here is what I have so far.

    What I want to want to add here is accept 10 characters where the first 1-9 are digits and the last character can be a digit OR an X.

    I think I am using the wrong function here -- getline -- becuase I am trying to get one character at a time and validate it.

    Any clues ???

    Thanks,

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void main()
    {
    	string input;  // user's input for ISBN #
    
    	// Info about program
    	cout << "*** ISBN VALIDATOR ***\n\n"
                    << "Enter an ISBN #.\n\n"
                    << "Remember, it is 10 SEQUENTIAL digits,\n"
                    << "OR 9 SEQUENTIAL digits and then an 'X'.\n\n";
    	cout << "Your ISBN #: ";
        getline(cin, input, '\n');
    
    }
    Last edited by bandul; 05-12-2006 at 12:33 PM. Reason: spellign mistakes

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    use a for loop:

    Code:
    char input[10]; // for one character!
    
    for(int i = 11; i < 11; i++)
    {
        cout << "Enter one int: ";
        cin >> input[i];
        if(i < 10)
        // validate using isalpha() and isdigit()
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You could...

    Code:
    cout << "Enter 10-digit ISBN: ";
    getline(cin, str);
    
    //now trim str to first 10 characters
    //if str is not 10 characters you might output an error or something, up to you
    
    //for first nine digits
    //do str[i] - '0' to get the digit and check digit range
    
    //for tenth digit add "X"/"x" handling
    
    //read all ten into some array and perform base-11 check
    Good luck with that.
    Last edited by jafet; 05-12-2006 at 10:17 PM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    You could . . . read the FAQ on why void main() is wrong.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  3. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 05:01 AM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 09:28 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21