-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNC_main.cpp
169 lines (118 loc) · 5.78 KB
/
NC_main.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
NAME: Talike Bennett
PROJECT: Number Converter
FILE: NC_main.cpp
*/
#include <iostream>
#include <ios> // streamsize
#include <limits> // numeric_limits
#include "NC_functions.hpp" // BaseToDec(), DecToBase(), HexToDec(), DecToHex()
#include "NC_misc.hpp" // windowWidth, wordList, PrintSeparator(), PrintHeader(), ValidInput()
using namespace std;
/*
Input Stream Error Handling Sources:
https://www.cplusplus.com/reference/istream/istream/ignore/
https://www.tutorialspoint.com/what-is-the-use-of-cin-ignore-in-cplusplus
*/
int main() {
while(true) {
do {
int sourceBase; // Holds the value of the source number system base
int targetBase; // Holds the value of the target number system base
string sourceNum; // Holds the value of the source number
string targetNum; // Holds the value of the target number
// SOURCE NUMBER SYSTEM SELECTION
PrintHeader(0);
cout << endl;
cout << "Choose your source number system:" << endl;
for(int i = 2; i <= 16; i++) { // Prints out the options for the main menu
if(i >= 11 && i <= 15) continue; // Bases 11-15 are not used in this program, so they are not printed
cout << "(" << i << ") " << wordList[i] << endl;
}
cout << endl;
cout << "(Input any other number to exit the program.)" << endl;
cout << "Enter number: ";
cin >> sourceBase;
while(cin.fail()) { // Handles the case where the user enters invalid input for sourceBase
cout << "Invalid input!" << endl;
cin.clear(); // Removes stream error
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clears stream buffer
cout << "Enter number: ";
cin >> sourceBase;
}
PrintSeparator(); // Prints the bottom row of the window
cout << endl << endl;
if(!(sourceBase >= 2 && sourceBase <= 10) && !(sourceBase == 16)) break;
// TARGET NUMBER SYSTEM SELECTION
PrintHeader(1, sourceBase);
cout << endl;
cout << "Choose your target number system: " << endl;
for(int i = 2; i <= 16; i++) { // Prints out the options for the target number system selection menu
if(i == sourceBase) continue; // You cannot convert to the same number system
if(i >= 11 && i <= 15) continue; // Bases 11-15 are not used here, so they are not printed
cout << "(" << i << ") " << wordList[i] << endl;
}
cout << endl;
cout << "(Input any other number to exit the program.)" << endl;
cout << "Enter number: ";
cin >> targetBase;
while(cin.fail() || sourceBase == targetBase) { // Handles the case where the user enters invalid input for targetBase
cout << "Invalid input!" << endl;
cin.clear(); // Removes stream error
cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clears stream buffer
cout << "Enter number: ";
cin >> targetBase;
}
PrintSeparator(); // Prints the bottom row of the window
cout << endl << endl;
if(!(targetBase >= 2 && targetBase <= 10) && !(targetBase == 16)) break;
// CONVERSION WINDOW
PrintHeader(2, sourceBase, targetBase);
cout << endl;
cout << "Input your number in source number system:" << endl;
cout << wordList[sourceBase] << ": ";
if(sourceBase == 16) cout << "0x";
cin >> sourceNum;
while(!ValidInput(sourceNum, sourceBase)) { // Handles the case where the user enters invalid input for sourceNum
cout << "Invalid input!" << endl;
cout << wordList[sourceBase] << ": ";
if(sourceBase == 16) cout << "0x";
cin >> sourceNum;
}
if(sourceBase < 10) {
if(targetBase < 10) targetNum = DecToBase(BaseToDec(sourceNum, sourceBase), targetBase); // base 2-9 to base 2-9
else if(targetBase == 10) targetNum = BaseToDec(sourceNum, sourceBase); // base 2-9 to decimal
else if(targetBase == 16) targetNum = DecToHex(BaseToDec(sourceNum, sourceBase)); // base 2-9 to hexadecimal
} else if(sourceBase == 10) {
if(targetBase < 10) targetNum = DecToBase(sourceNum, targetBase); // decimal to base 2-9
else if(targetBase == 16) targetNum = DecToHex(sourceNum); // decimal to hexadecimal
} else if(sourceBase == 16) {
if(targetBase < 10) targetNum = DecToBase(HexToDec(sourceNum), targetBase); // hexadecimal to base 2-9
else if(targetBase == 10) targetNum = HexToDec(sourceNum); // hexadecimal to decimal
}
cout << wordList[targetBase] << ": " << targetNum << endl;
cout << "You will be returned to the main menu." << endl;
PrintSeparator(); // Prints the bottom row of the window
cout << endl << endl;
} while(true);
// CONFIRM CLOSE WINDOW
char yesOrNo; // Holds the user's 'Y' and 'N' inputs
PrintHeader(3);
cout << endl;
cout << "Are you sure you want to quit the program? (Y/N)" << endl;
cin >> yesOrNo;
if(yesOrNo == 'Y' || yesOrNo == 'y') {
cout << "Bye!" << endl;
PrintSeparator(); // Prints the bottom row of the window
cout << endl << endl;
break;
// The program ends at this point
} else {
cout << "You will be returned to the main menu." << endl;
PrintSeparator(); // Prints the bottom row of the window
cout << endl << endl;
// The program returns to the DO-WHILE loop
}
}
return 0;
}