From 3ccc794772060b42e752906cb0430644491b76ae Mon Sep 17 00:00:00 2001 From: Mathias Wagner Date: Sat, 11 Nov 2023 14:05:57 +0100 Subject: [PATCH] Created the base_2_to_10.cpp --- base_2_to_10.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 base_2_to_10.cpp diff --git a/base_2_to_10.cpp b/base_2_to_10.cpp new file mode 100644 index 0000000..b01ab84 --- /dev/null +++ b/base_2_to_10.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +using namespace std; + +int main() { + cout << "Bitte gib deinen Base2 String ein: "; + string input; + cin >> input; + + vector 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; +} \ No newline at end of file