School-Coding-Cpp/sfusi/OCT to DEC.cpp

37 lines
628 B
C++

#include <iostream>
using namespace std;
int main(void) {
int ottale;
cout << "Inserisci un numero in base ottale: ";
cin >> ottale;
int decimale = 0;
int base = 1;
while (ottale > 0) {
int cifra = ottale % 10;
if (cifra < 0 || cifra > 7) {
cout << "Numero ottale non valido!" << endl;
return 0;
}
decimale = decimale + (cifra * base);
base = base * 8;
ottale = ottale / 10;
}
if (decimale < 0 || decimale > 255) {
cout << "Numero ottale non valido!" << endl;
return 0;
}
cout << "Numero decimale: " << decimale << endl;
return 0;
}