#include <iostream> #include<cmath> #include<ctime> #include<string> #include <iomanip> #include <fstream> using namespace std; int octal2Dec(string& octal); bool validity(int number); int stringToNum(string number); int main() { string number; getline (cin , number); cout << octal2Dec(number); return 0; } int octal2Dec(string& octal) { int digits, decimal = 0, count = 0,oct = stringToNum(octal); while (oct != 0) { digits = oct % 10; if(validity(digits) == true) decimal += digits* pow(8,count); else { return NAN; } oct /= 10; count++; } return decimal; } bool validity(int number) { return number % 8 == 0 || number % 9 == 0? false : true; } int stringToNum(string number) { int convertedNumber = 0; for (int i = 0; i < number.length(); i++) { convertedNumber = (convertedNumber * 10) + static_cast<int>(number[i] - '0'); } return convertedNumber; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter