-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
53 lines (41 loc) · 1.49 KB
/
Copy pathmain.cpp
File metadata and controls
53 lines (41 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main() {
//Variables
string userInput = "";
while (true) {
//We ask the user
cout << "What type of number system you want to convert?" << endl;
cout << "1. Binary" << endl;
cout << "2. Decimal" << endl;
cout << "3. Exit" << endl;
//We recieve the input
cin >> userInput;
if (userInput == "1") {
cout << "What binary number you want to convert to decimal?" << endl;
cin >> userInput;
//Converts the binary to decimal
int changedNumber = stoi(userInput, 0, 2);
//Print the result
cout << "The converted number is " << to_string(changedNumber) << endl;
cout << "" << endl;
}
if (userInput == "2") {
cout << "What decimal number you want to convert to binary?" << endl;
int decimalInput;
cin >> decimalInput;
//Print the result
string binaryStr = bitset<32>(decimalInput).to_string();
binaryStr.erase(0, min(binaryStr.find_first_not_of('0'), binaryStr.size() - 1));
cout << "The converted number is " << binaryStr << endl;
cout << "" << endl;
}
if (userInput == "3") {
cout << "Goodbye!" << endl;
break;
}
}
return 0;
}