[Solved] Just input a number in cpp [closed]


I think this is what you are looking for:

#include <iostream>
#include <string>
#include <cctype>

int main () {
  std::string input;
  bool valid;
  do {
    valid = true;
    std::cin >> input;
    for (char c : input)
      if (! std::isdigit( static_cast<unsigned char>(c) ) )
        valid = false;
  } while (! valid);
  // Here the string is guaranteed to be valid
}

Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.

5

solved Just input a number in cpp [closed]