- 1

How to add more int var into this program

In this programm i added only one int, A,in witch I have to write a combiniations of zeros or ones to it, but what if I want to add something like int A,B, C, D; Each can eather have 1 or 0, how can I write switch command using more int vagiables e.g. A =1 B=0 C=0 D=0 Output: 8 #include <iostream> using namespace std; int main () { int A; cin >> A ;  switch (A) {     case 0 : cout << "0"; break;     case 1 : cout << "1"; break;     case 10 : cout << "2"; break;     case 11 : cout << "3"; break;     case 100 : cout << "4"; break;     case 101 : cout << "5"; break;     case 110 : cout << "6"; break;     case 111 : cout << "7"; break;     case 1000 : cout << "8"; break;     case 1001 : cout << "9"; break;     case 1010 : cout << "10"; break;     case 1011 : cout << "11"; break;     case 1100 : cout << "12"; break;     case 1101 : cout << "13"; break;     case 1110 : cout << "14"; break;     case 1111 : cout << "15"; break;      default : cout << "Error";  }  cout << endl; system("pause"); return 0; }

11th Nov 2020, 5:46 PM
Dr. Alien
Dr. Alien - avatar
8 Answers
+ 3
You don't want to hardcode that, at least if you want to remain sane. For a binary number, there are algorithms for converting it to decimal that you can implement, which I'd suggest you to look into a little, e.g. here: https://www.tutorialspoint.com/cplusplus-program-to-convert-binary-number-to-decimal-and-vice-versa https://www.programiz.com/cpp-programming/examples/binary-decimal-convert If you already have the single "bits" (I'd suggest you to use an array over multiple independant variables) layed out, the conversion just becomes easier, because now you only have to sum the bits multiplied by 2 to the power of their respective position (zero-indexed). For example, you multiply the least significant bit by 2^0, the next by 2^1, and so on, and simply sum the results of those multiplications.
11th Nov 2020, 6:26 PM
Shadow
Shadow - avatar
+ 3
Both are very alike, except that the second keeps track of the position with 'i' and calculates the power 2^i with the help of the pow() function, which is just a standard function used to calculate powers: https://en.cppreference.com/w/cpp/numeric/math/pow Meanwhile, the first one keeps track of the power directly by starting with 1 = 2^0 and then multiplying by 2 every iteration, so you get 2 = 2^1, then 4 = 2^2, and so on. This is probably more efficient than recalculating the power every iteration, so I'd stick with this. By the way, if you get stuck in the code and can't find a solution on your own, feel free to copy it into a SL project and link that here, so that we can analyze the code and help you. Good luck!
11th Nov 2020, 7:00 PM
Shadow
Shadow - avatar
+ 2
Yes, that is correct. Integral types don't allow leading zeros, they are automatically removed. That is why data such as credit card numbers or similar examples, which can contain leading zeros, are usually stored as strings, not as integers.
11th Nov 2020, 7:25 PM
Shadow
Shadow - avatar
+ 1
No problem, feel free to DM me if you deem it necessary.
11th Nov 2020, 7:41 PM
Shadow
Shadow - avatar
0
Thank you for anwser ! Second link code is intersting, but I dont understand it yet, Il take a time to write it and analyze it
11th Nov 2020, 6:48 PM
Dr. Alien
Dr. Alien - avatar
0
Hey, one more thing about my code, why doesn't case from number 2-7 work if I have zeros before ones, as you can see i had to remove them, is it because normaln numbers don't have zero at the beggining or? What am I missing
11th Nov 2020, 7:21 PM
Dr. Alien
Dr. Alien - avatar
0
Hello. A tip is trie to do it by yourself, use string for example, is easy, I am not a expert I am a student, and so long ago that I can do it. You could storage the value in to a string using cin>> and with a iterator of string (if you dont want use int) you can reconvert it to a decimal number, just see the binary base and how obtain it, the number on decimal base you can storage in other string or int. However, good luck. sory for bad english.
11th Nov 2020, 7:27 PM
MICAEL MIRANDA
MICAEL MIRANDA - avatar
0
Ty for your time! Btw Shadow, would you mind if I contact you in the future if I will have any questions, u seem like a nice guy that know those stuff, I started to learn programming 7 weeks ago throught collage, but I'am so behind in those lessons need to chatch up. Have a nice day.
11th Nov 2020, 7:38 PM
Dr. Alien
Dr. Alien - avatar