Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- bool toNumberConv(std::string str, int &x)
- {
- bool num = true;
- unsigned int i = 0;
- while(i < str.length() && num)
- {
- if (!isdigit(str[i]))
- {
- num = false;
- }
- i++;
- }
- if(num)
- {
- x = stoi (str);
- }
- else
- {
- x = 0;
- }
- return num;
- }
- bool toNumberConv(std::string str, double &x)
- {
- int dots = 0;
- bool num = true;
- unsigned int i = 0;
- while(i < str.length() && num)
- {
- if ((str[i] == '.') && dots == 0)
- {
- dots = 1;
- }
- else if (!isdigit(str[i]))
- {
- num = false;
- }
- i++;
- }
- if(num && dots <= 1)
- {
- x = stod (str);
- }
- else
- {
- x = 0;
- }
- return num;
- }
- int main()
- {
- std::string str;
- std::cin >> str;
- int toNumInt;
- double toNumDouble;
- bool isNumInt = toNumberConv(str, toNumInt);
- bool isNumDouble = toNumberConv(str, toNumDouble);
- if(isNumInt) std::cout << toNumInt << std::endl;
- else std::cout << "Ez nem egy int szam\n";
- if(isNumDouble) std::cout << toNumDouble << std::endl;
- else std::cout << "Ez nem egy double szam\n";
- return 0;
- }
Add Comment
Please, Sign In to add comment