how to tell if a number is octal

Pages: 12
Sorry for double-post, but I got it to work. Here:
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
// multi_input.cpp : Defines the entry point for the console application.
// Osman Zakir
// 3 / 4 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 11 Exercise 4
// Exercise Specifications:
/**
* Write a program called multi_input.cpp that prompts the user to enter
* several integers in any combination of octal, decimal, or hexadecimal,
* using the 0 and 0x base suffixes; interprets the numbers correctly; and
* converts them to decimal form. Then your program should output the
* values in properly spaced columns like this:
*		0x43 hexadecimal converts to 67 decimal
*		0123 octal       converts to 83 decimal
*		65 decimal       converts to 65 decimal
*/

#include "../../cust_std_lib_facilities.h"
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <vector>

int base_of(std::string &num_s);
std::vector<std::string> split(const std::string &line);
void print_results(const int base, const int number, const std::string num_s);

int main()
{
	using namespace std;
	cout << "Please enter some numbers in any combination of decimal, hexadecimal, "
		<< "or octal.\nUse the '0x' and '0' suffixes "
		<< "for hexadecimal and octal as needed.\n";
	
	string input;
	getline(cin, input);
	for (string num_s : split(input))
	{
		try
		{
			const int base = base_of(num_s);
			size_t position;
			int number = stoi(num_s, addressof(position), base);
			print_results(base, number, num_s);
		}
		catch (const invalid_argument &inv_arg)
		{
			cerr << "invalid argument error: " << inv_arg.what() << '\n';
		}
		catch (const out_of_range &oor)
		{
			cerr << "out of range error: " << oor.what() << '\n';
		}
	}

	keep_window_open();
}

int base_of(std::string &num_s)
{
	using namespace std;
	if (num_s.find_first_of("+-") == 0)
	{
		num_s = num_s.substr(1);
	}
	if (num_s.empty() || !isdigit(num_s.front()))
	{
		throw invalid_argument("");
	}
	if (num_s.front() == '0')
	{
		if (num_s.size() > 1 && tolower(num_s[1]) == 'x')
		{
			return 16;
		}
		else
		{
			return 8;
		}
	}
	return 10;
}

std::vector<std::string> split(const std::string &line)
{
	using namespace std;
	vector<string> result;

	istringstream ist{ line };
	string str;
	while (ist >> str)
	{
		result.push_back(str);
	}
	return result;
}

void print_results(const int base, const int number, const std::string num_s)
{
	using namespace std;
	switch (base)
	{
	case 8:
		cout << right << setw(5) << num_s << right << setw(8) << " octal "
			<< setw(19) << right << " converts to "
			<< right << setw(4) << number << " decimal\n";
		break;
	case 10:
		cout << right << setw(5) << num_s << right << setw(10) << " decimal "
			<< setw(17) << right << " converts to "
			<< right << setw(4) << number << " decimal\n";
		break;
	case 16:
		cout << right << setw(5) << num_s << right << setw(14) << " hexadecimal "
			<< setw(11) << right << " converts to "
			<< right << setw(4) << number << " decimal\n";
		break;
	}
}
Topic archived. No new replies allowed.
Pages: 12