Turning birthdays into a binary number

Oct 3, 2019 at 11:46pm
I'm making a code for class. We're supposed to make a game-ish thing where the user sees a set of birthdays, and if their birthday is in the set, says Yes or No, then sees a new set, then answers Yes or No. The computer is supposed to guess their birthday

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

int main()
{
  int a = 0, b = 0, c = 0, d = 0, e = 0;
  int Yes = 1, No = 0;
  std::cout << "State Y or N if your birthday is in the set\n";
  std::cout << "Set 1: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31\n";
    std::cin >> a;
    int f = a*2^0;
  std::cout << "State Y or N if your birthday is in the set\n";
  std::cout << "Set 2: 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31\n";
    std::cin >> b;
    int g = b*2^1;
  std::cout << "State Y or N if your birthday is in the set\n";
  std::cout << "Set 3: 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31\n";
    std::cin >> c ;
    int h = c*2^2;
  std::cout << "State Y or N if your birthday is in the set\n";
  std::cout << "Set 4: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31\n";
    std::cin >> d;
    int i = d*2^3;
  std::cout << "State Y or N if your birthday is in the set\n";
  std::cout << "Set 5: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n";
    std::cin >> e;
    int j = e*2^4;
  int K = f+g+h+i+j;
  std::cout << " Your number is " << K << "!\n";
}
Last edited on Oct 3, 2019 at 11:58pm
Oct 4, 2019 at 12:30am
@Mob kun

Problems!!

Problem #1.. You ask for a Y or N, but are looking for an int 1 or 0..

Problem #2.. When I run your program, I get the wrong answer. I was born on the 11th, but the program says 12.

Not a bad program, otherwise. Though you never do ask a question or state a problem you're having.

Oct 4, 2019 at 12:36am
@whitenite1

the problem is that it wont let me get it so that the user can input multiple answers. Like the have to see a set, input yes or no, then get a new set and say yes or no.

I was thinking that if the user was to input Yes, it would become registered as a 1 and if no, it would register as a zero, then input it into the equation in place of a, then continuing with b, c, d, and e.
Oct 4, 2019 at 12:40am
A decimal to binary converter:
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
#include <iostream>

int main()
{
   std::cout << "Enter the number to convert: ";
   int n { };
   std::cin >> n;
   std::cout << '\n';

   int a[10];
   int i = 0;

   for (i = 0; n > 0; i++)
   {
      a[i] = n % 2;
      n = n / 2;
   }

   std::cout << "Binary of the given number = ";
   for (i = i - 1; i >= 0; i--)
   {
      std::cout << a[i];
   }
   std::cout << '\n';
}

Enter the number to convert: 15

Binary of the given number = 1111

Oct 4, 2019 at 12:46am
@Furry Guy

the code is supposed to get either a 1 or 0 from each set based on the user's input, then receive a binary number. That binary number is then supposed to be converted to a decimal that is the user's birthday.

thanks tho
Last edited on Oct 4, 2019 at 12:54am
Oct 4, 2019 at 12:48am
Change each of your input requests, to:
std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";

But, even after changing it to look for 1's and 0's, the program still does not deliver a correct answer. I think you may need to use POW function, as I don't think '^' is doing what you are intending.

{EDIT}

I used the pow function, and things worked out very well. Here it is..
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
#include <iostream>

int main()
{
	int a = 0, b = 0, c = 0, d = 0, e = 0, K = 0;
	int Yes = 1, No = 0;
	std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";
	std::cout << "Set 1: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31\n";
	std::cin >> a;
	int f = pow(a*2,0);
	std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";
	std::cout << "Set 2: 2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31\n";
	std::cin >> b;
	int g = pow(b*2,1);
	std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";
	std::cout << "Set 3: 4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31\n";
	std::cin >> c ;
	int h = pow(c*2,2);;
	std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";
	std::cout << "Set 4: 8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31\n";
	std::cin >> d;
	int i= pow(d*2,3);
	std::cout << "State 1 for 'Yes' or 0 for 'No', if your birthday is in the set\n";
	std::cout << "Set 5: 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n";
	std::cin >> e;
	int j = pow(e*2,4);;
	K = (f+g+h+i+j);
	
	std::cout << " Your number is " << K << "!\n";
}
Last edited on Oct 4, 2019 at 12:52am
Oct 4, 2019 at 12:50am
@whitenite1

thanks! i never knew that. I'll go change it and see if it works.
Oct 4, 2019 at 1:04am
@whitenite1

thank you so much

it worked perfectly!

again, thanks for helping me so much!
Topic archived. No new replies allowed.