Thread: basic input

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    Arrow basic input

    i have this code:
    Code:
    cout<<"level number:";
    cin>>levnum;
    it goes it loops of itself if u input wildcards or chars
    how would i stop it doing this ie return an error saying incorrect levnum?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Read a string instead of whatever levnum is now and validate it by checking the length of the string and that each element matches a digit and that the number is within your range for levels, then convert it to an integer with atoi.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post Use isdigit()!

    After you get the input do this:

    #include <ctype.h>

    if(isdigit(levelnum))
    {
    // Is a digit
    }
    else
    {
    cout << "Must be digit!";
    }
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM