How do I check if char contains int

This is a discussion on How do I check if char contains int within the C++ Programming forums, part of the General Programming Boards category; How do I check if char contains int. I just need to be able to tell if char[256] contains a ...

  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
    20,973
    You may find std::isdigit() from <cctype> useful.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 03: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, 06:15 AM

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