4life159, ну так в ссылке, которую я скинул, вопрос «как проверить если c++ строка содержит только цифры», разве что-то не то?
#include <iostream> #include <string> bool isValidYear(const std::string& year) { // Проверить, все ли символы являются цифрами for (char c : year) { if (!std::isdigit(c)) { return false; } } // Преобразование строки в целое число int yearAsInt = std::stoi(year); // Проверить, находится ли целое число в допустимом диапазоне if (yearAsInt < 1000 || yearAsInt > 9999) { return false; } return true; } int main() { std::cout << std::boolalpha; std::cout << isValidYear("2022") << std::endl; // true std::cout << isValidYear("2k22") << std::endl; // false std::cout << isValidYear("10000") << std::endl; // false return 0; } C #include <iostream> #include <string> bool isValidYear(const std::string& year) { // Проверить, все ли символы являются цифрами for (char c : year) { if (!std::isdigit(c)) { return false; } } // Преобразование строки в целое число int yearAsInt = std::stoi(year); // Проверить, находится ли целое число в допустимом диапазоне if (yearAsInt < 1000 || yearAsInt > 9999) { return false; } return true; } int main() { std::cout << std::boolalpha; std::cout << isValidYear("2022") << std::endl; // true std::cout << isValidYear("2k22") << std::endl; // false std::cout << isValidYear("10000") << std::endl; // false return 0; } либо #include <iostream> #include <string> bool isNumberLine(const std::string& text) { // Проверить, все ли символы являются цифрами for (char c : text) { if (!std::isdigit(c)) { return false; } } return true; } int main() { std::cout << std::boolalpha; std::cout << isNumberLine("1234567890") << std::endl; // true std::cout << isNumberLine("1234567890abc") << std::endl; // false std::cout << isNumberLine("123456 7890") << std::endl; // false return 0; } C #include <iostream> #include <string> bool isNumberLine(const std::string& text) { // Проверить, все ли символы являются цифрами for (char c : text) { if (!std::isdigit(c)) { return false; } } return true; } int main() { std::cout << std::boolalpha; std::cout << isNumberLine("1234567890") << std::endl; // true std::cout << isNumberLine("1234567890abc") << std::endl; // false std::cout << isNumberLine("123456 7890") << std::endl; // false return 0; } надеюсь, поможет.
#include <iostream> #include <cctype> using namespace std; bool is_numeric(const string& s) { for (char c : s) if (!isdigit(c)) return false; return true; } int main() { cout << boolalpha; cout << is_numeric("2022") << endl; // true cout << is_numeric("2k22") << endl; // false return 0; } C #include <iostream> #include <cctype> using namespace std; bool is_numeric(const string& s) { for (char c : s) if (!isdigit(c)) return false; return true; } int main() { cout << boolalpha; cout << is_numeric("2022") << endl; // true cout << is_numeric("2k22") << endl; // false return 0; }