Oct 1, 2015 at 2:45am UTC
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 Oct 1, 2015 at 2:46am UTC
Oct 1, 2015 at 3:19am UTC
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.
Oct 1, 2015 at 3:28am UTC
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 Oct 1, 2015 at 3:28am UTC
Oct 1, 2015 at 3:38am UTC
Please don't post twice on same problem !! It's a time waster
Oct 1, 2015 at 3:40am UTC
it's two different questions though.
Oct 1, 2015 at 3:41am UTC
No it's not and I've deleted my help. You miss out unfortunately
Oct 1, 2015 at 3:44am UTC
Not sure why you're being rude... But LB's solution worked. Thanks for your attempt to help though.
Oct 1, 2015 at 3:51am UTC
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.
Oct 1, 2015 at 4:04am UTC
@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 Oct 1, 2015 at 4:04am UTC
Oct 1, 2015 at 4:25am UTC
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.
Oct 1, 2015 at 4:39am UTC
Whatever that's supposed to mean. I guess we'll have to put it down to the actions of a callow nosey parker.