Created the base_2_to_10.cpp

This commit is contained in:
Mathias Wagner 2023-11-11 14:05:57 +01:00
parent e351845c17
commit 3ccc794772
Signed by: Mathias
GPG Key ID: B8DC354B0A1F5B44

37
base_2_to_10.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
cout << "Bitte gib deinen Base2 String ein: ";
string input;
cin >> input;
vector<int> bits;
for (char c : input) {
int bit = c - '0';
bits.push_back(bit);
if (!(bit == 0 || bit == 1)) {
cout << "Du kannst in Base2 nur 1 oder 0 verwenden. Bitte versuche es erneut." << endl;
return main();
}
}
int num = 0;
int modifier = 1;
for (auto i = bits.begin(); i < bits.end(); i++) {
num += modifier * bits[bits.end()- i - 1];
modifier *= 2;
}
cout << input << " lautet als Dezimalzahl " << num;
return 0;
}