why do i get letters when multiplying numbers?

how do i make it so that i don't get letters when i multiply two numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
#include <cmath>
using namespace std;
int main()
{
	char binary[5];
	double decimal;
	int dec0, dec1, dec2, dec3;
	cout << "Enter a 4 digit binary number: ";
	cin.getline(binary, 5, '\n');
	binary[0] *= 1;
	binary[1] *= 2;
	cout << binary[0] << endl;
	cout << binary[1] << endl;
	cout << binary[2] << endl;
	cout << binary[3] << endl;
	cout << binary << endl;

	system("pause");
	return 0;
}
Last edited on
On lines 13-16, just cast it to an integer: static_cast<int>(binary[0])

Remember that the char type is treated as text, not numbers, by C++ streams.
like so?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char binary[5];
	double decimal;
	int dec0, dec1, dec2, dec3;
	cout << "Enter a 4 digit binary number: ";
	cin.getline(binary, 5, '\n');
	binary[0] *= 1;
	binary[1] *= 2;
	cout << static_cast<int>(binary[0]) << endl;
	cout << static_cast<int>(binary[1]) << endl;
	cout << static_cast<int>(binary[2]) << endl;
	cout << static_cast<int>(binary[3]) << endl;
	cout << binary << endl;

	system("pause");
	return 0;
Last edited on
closed account (48T7M4Gy)
Please don't post twice on same problem !! It's a time waster
it's two different questions though.
closed account (48T7M4Gy)
No it's not and I've deleted my help. You miss out unfortunately
Not sure why you're being rude... But LB's solution worked. Thanks for your attempt to help though.
closed account (48T7M4Gy)
I'm not being rude but since you want to argue, I'm just pointing out that double posting is a waste of people's time. We are volunteers and luckily see very few selfish and thoughtless shits like you but feel obliged to point it out politely when it occurs.
@kemort: if you're going to claim that someone double posted, at least link to the other post. The only other post I could find was with similar code but an entirely different question.

BTW I didn't report your post but I can see why someone would.
Last edited on
closed account (48T7M4Gy)
I couldn't care less if I have been reported. The claim he is making is wrong and I don't plan getting into an argument with you. I suggest you MYOB.
kemort wrote:
I suggest you MYOB.
Use private messages if you are not comfortable with everyone seeing your business.
closed account (48T7M4Gy)
Whatever that's supposed to mean. I guess we'll have to put it down to the actions of a callow nosey parker.
Topic archived. No new replies allowed.