C++ 10.17 Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs Yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: Yes Ex: If the input is: 42,000 or 1995! the output is: No Hint: Use a loop and the isdigit() function (don't forget to include the cctype library). CODE PROVIDED: #include #include #include using namespace std; string solve(string userStr) { int start; if(userStr[0]=='-' && isdigit(userStr[1])){ start=1; } else{ start=0; } for(int i=start; i
C++ 10.17
Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs Yes if every character is a digit 0-9.
Ex: If the input is:
1995
the output is:
Yes
Ex: If the input is:
42,000
or
1995!
the output is:
No
Hint: Use a loop and the isdigit() function (don't forget to include the cctype library).
CODE PROVIDED:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string solve(string userStr) {
int start;
if(userStr[0]=='-' && isdigit(userStr[1])){
start=1;
}
else{
start=0;
}
for(int i=start; i<userStr.length();i++){
if(!isdigit(userStr[i])){
return "No";
}
return "Yes"
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps