how can i make my code accept 2 digit numbers?

So my code works for the most part. It is supposed to take in 8 numbers from the user and then pack them into a single integer. Then it is supposed to allow the user to choose which number to return and then it unpacks the number that is chosen. This all works except when i enter a number like 10 it changes it to zero and doesn't return it correctly. Any help with this issue is appreciated!

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
#include <iostream>
using namespace std;

unsigned int pack(int a, int b, int c, int d, int e, int f, int g, int h)
{
	unsigned int abcdefgh;
	abcdefgh = 10000000 * a + 1000000 * b + 100000 * c + 10000 * d + 1000 * e + 100 * f + 10 * g + h;
	return abcdefgh;
}

int getDigit(unsigned int packthem, int choice) {
	for (int i = 0; i<choice - 1; i++) {
			packthem /= 10; 
		}
		return packthem % 10;
}

int main() {
	unsigned int final = 0;
	int a, b, c, d, e, f, g, h;
	int packthem;
	int choice;
		cout << "Enter a value: " << endl;
		cin >> a;
		cout << "Enter a value: " << endl;
		cin >> b;
		cout << "Enter a value: " << endl;
		cin >> c;
		cout << "Enter a value: " << endl;
		cin >> d;
		cout << "Enter a value: " << endl;
		cin >> e;
		cout << "Enter a value: " << endl;
		cin >> f;
		cout << "Enter a value: " << endl;
		cin >> g;
		cout << "Enter a value: " << endl;
		cin >> h;
		
		packthem = pack(a, b, c, d, e, f, g, h);

		cout << packthem << endl;
	cout << "Which numbers would you like to recover?" << endl;
	cin >> choice;

	final = getDigit(packthem, choice);
	cout << final << endl;
	cout << endl;
	system("pause");
	}
I would approach this differently.

1.) Create an array of ten integers to store the entered values.
2.) Accept user input to populate the array in a for loop.
3.) Create a std::stringstream object (found in <sstream>).
4.) Insert the numbers into the stringstream.
5.) Create an integer packed.
6.) Extract the numbers into packed.

This will yield a "packed" number.
Once the user requests to retrieve a certain number:

1.) Find the requested number in the original array and print it.
2.) If the requested number doesn't exist, generate an error

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
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>

int main() {

	const int num_numbers = 3;
	int numbers[num_numbers] = {1, 30, 56};

	std::stringstream stream;

	for (auto n : numbers) {
		stream << n;
	}

	int packed = 0;
	stream >> packed;

	std::cout << "packed:\t" << packed << std::endl;;

	int wanted = 30;

	auto begin = std::begin(numbers);
	auto end = std::end(numbers);

	auto it = std::find(begin, end, wanted);

	if (it == end) {
		std::cout << "The requested number wasn't found" << std::endl;
	}
	else {
		std::cout << "wanted:\t" << numbers[std::distance(std::begin(numbers), it)] << std::endl;
	}

	return 0;

}
Topic archived. No new replies allowed.