I am writing some code that checks for a string input and finds if it is made up of all digits. There is a function that validates if the string is 10 digits or below and another one to validate if they are all numbers.
Here is my code below:
#include <iostream> #include <ctime> #include <time.h> #include <iomanip> #include <math.h> using namespace std; void inputString(string s); void checkDigit(); int main() { string s; cout<<"Enter a string: "<< endl; getline(cin, s); checkDigit(); return 0; } void inputString(string s){ do{ if(s.length() > 10 ){ getline(cin, s); } } while(s.length() <= 10); } void checkDigit(){ string s; inputString(s); for(int i = 0; i < s. length(); i++){ if (s[i] >= '0' && s[i] <= '9'){ cout<<"Valid string"<< endl; } else{ cout<<"Not all digits"<< endl; } } }
https://stackoverflow.com/questions/66809652/validate-string-using-functions-c March 26, 2021 at 09:05AM
没有评论:
发表评论