Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- char toDigit(char c) {
- if(c == '1') return 1;
- if(c == '2') return 2;
- if(c == '3') return 3;
- if(c == '4') return 4;
- if(c == '5') return 5;
- if(c == '6') return 6;
- if(c == '7') return 7;
- if(c == '8') return 8;
- if(c == '9') return 9;
- if(c == '0') return 0;
- return -1;
- }
- int power(int n, int power) {
- if (power == 0) return 1;
- int res = 1;
- for(int i = 0; i < power - 1; i++)
- res = res * n;
- return res;
- }
- int parseInt(std::string s) {
- using namespace std;
- int num = 0;
- for(int i = 0; i < s.length(); i++) {
- if(toDigit(s[i]) == -1)
- return -1;
- num = num + toDigit(s[i]) * power(10, s.length() - i);
- }
- return num;
- }
- int main ( ) {
- using namespace std;
- cout << "Inserire N (0, 130): " << endl;
- string age;
- cin >> age;
- int output = parseInt(age);
- if (output == -1)
- cout << "Numero non valido" << endl;
- else
- if(output < 0 || output > 130)
- cout << "Eta fuori range" << endl;
- else
- cout << output << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment