Thread: bulletproofing question

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    16

    bulletproofing question

    The cin command is problematic, because there is no
    checking for variable type. If you specify an integer and
    the user inputs a character, you get garbage. The
    solution seems to be to use gets() and then atoi(), but
    I'm not sure how to do that with two-digit numbers. Any
    suggestions?

    For a simple yes/no or menu input function, it seems to be
    enough to define a char array of no specified length,
    gets() the char array, then do something like this:

    while (strlen(charArray) > 2 || charArray[0] != 'y'
    && charArray[0] != 'n')
    {
    cout << "Pl

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Search the board, this was from a day or two ago.

    http://www.cprogramming.com/cboard/s...threadid=11365

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Well you can use cin but after accepting it . You can get the ascii value of it and determine whether it is a number or not. This way you can eliminate everything else other than that is numeric.

  4. #4
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    or perhaps you could use scanf or just DIY formatting [which i don't recommend, but it is an option in case you have special formatting]
    hasafraggin shizigishin oppashigger...

  5. #5
    Unregistered
    Guest
    in the MSVC cin class, I use:

    int x;
    cin>>x;
    if(cin.fail()){return 0; /* or make a loop or some other stuff */}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM