How to check if string is a negative integer in C++

1 Answer

0 votes
#include <iostream>
  
int main() {
    std::string str = "-19";
      
    try {
        
        int n = stoi(str);
        
        std::cout << (n < 0 ? "yes" : "no") << "\n";
     
        std::cout << (n < 0) << "\n";
    } catch(...) {
        std::cout << "Not a number" << "\n";
    }  
}


  
  
/*
run:
  
yes
1
  
*/

 



answered Jun 25, 2022 by avibootz

Related questions

1 answer 273 views
1 answer 297 views
1 answer 205 views
2 answers 236 views
1 answer 204 views
1 answer 190 views
...