Thread: How do I check if char contains int

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    23

    How do I check if char contains int

    How do I check if char contains int. I just need to be able to tell if char[256] contains a integer.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You may find std::isdigit() from <cctype> useful.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Do you want to check if the entire string is an int, or just check if any of the characters are integers?

    If you want to see if any characters are integers, then loop through using isdigit.

    However, if you want to see of the string holds a number, use a stringstream. Initialize the istringstream with the string, then attempt to read into an int variable. If the read fails then the string does not contain a valid int, if it succeeds then it does.

    There are variations and additional checks you can do. For example, if the string is "123abc" then you will also want to make sure the streingstream is empty after reading into the int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM