-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-input.cpp
77 lines (74 loc) · 1.89 KB
/
multi-input.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
#include <iostream>
#include <string>
#include <cctype>
#include <iomanip>
#include <stdexcept>
enum Base {decimal,octal,hexadecimal};
void write (Base b, int i)
{
switch(b)
{
case hexadecimal:
std::cout << std::hex << i << "hexadecimal " << std::setw(8);
break;
case octal:
std::cout << std::oct << i << "octal " << std::setw(8);
break;
case decimal:
std::cout << std::dec << i << "decimal " << std::setw(8);
break;
default:
throw std::runtime_error("unknown type");
}
std::cout << "converts to " << std::dec << i << " decimal \n";
}
int main()
{
try
{
int i;
std::cin.unsetf(std::ios::dec);
std::cin.unsetf(std::ios::hex);
std::cin.unsetf(std::ios::oct);
std::cout << std::showbase;
std::cout << "Enter numbers (octal,hexadecimal,decimal) \n";
char c;
while(std::cin.get(c))
{
if(c == '0')
{
std::cin.get(c);
if(c == 'x' || c == 'X')
{
std::cin >> std::hex >> i;
write(hexadecimal,i);
}
else if(c == '8'|| c == '9')
{
throw std::runtime_error("unknown octal value");
}
else if(c != '8'|| c != '9')
{
std::cin.unget();
write(octal,i);
}
else if(isdigit(c)) //zero
{
std::cin.unget();
std::cin >> std::oct >> i;
write(octal,i);
}
}
else if(isdigit(c))
{
std::cin.unget();
std::cin >> std::dec >> i;
write(decimal,i);
}
}
}
catch(std::runtime_error &e)
{
std::cout << "runtime error: " << e.what() << '\n';
}
}