[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrIsInt
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrIsInt
(C++) Check if a std::string can be converted to an integer
///Checks whether a std::string can be converted to an integer. ///Returns true if possible, also returning this integer by referencing. ///Returns false otherwise, setting the referenced integer to zero. bool isInt(const std::string& s, int& rInt) { std::istringstream i(s); if (!(i >> rInt)) { rInt = 0; return false; } return true; } int main() { int value = 69; assert( isInt("IamNoInt", value) == false ); assert( isInt("69", value) == true ); if (isInt("123",value)==true) { std::cout << "The value is an integer" << std::endl; std::cout << "Its square is: " << value*value << std::endl; } else { std::cout << "The value is not integer" << std::endl; } }
- include <iostream>
- include <sstream>
- include <cassert>
Other code snippets
(C++ Builder) Check if an AnsiString can be converted to an integer
Topic links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
