fntasticccc

binary game part 1

Mar 1st, 2021
713
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <random>
  3. #include <string>
  4.  
  5. int randomNumber(int);
  6. std::string convertToBinaryString(int);
  7.  
  8.  
  9. int main()
  10. {
  11.     std::random_device dev;
  12.     std::mt19937 rng(dev());
  13.     std::uniform_int_distribution<std::mt19937::result_type> dist(1, 15);
  14.     std::string userInput;
  15.     int iRandomNumber = 0; // current random number gets saved after function runs
  16.     std::string sCorrectBinaryString; // current correct binary string
  17.  
  18.  
  19.     while (1) {
  20.  
  21.         iRandomNumber = randomNumber(dist(rng));
  22.         sCorrectBinaryString = convertToBinaryString(iRandomNumber);
  23.         std::cout << iRandomNumber;
  24.         if (iRandomNumber < 10) std::cout << " ";
  25.         std::cout << " in Binary eingeben: ";
  26.         std::cin >> userInput;
  27.  
  28.         if (userInput == sCorrectBinaryString) {
  29.             std::cout << "RICHTIG" << std::endl;
  30.         }
  31.         else {
  32.             std::cout << "FALSCH" << std::endl;
  33.         }
  34.     }  
  35.     return 0;
  36. }
  37.  
  38. int randomNumber(int d) {
  39.  
  40.     int rN = 0;
  41.     rN = d;
  42.     return rN;
  43. }
  44.  
  45. std::string convertToBinaryString(int n) {
  46.  
  47.     std::string r;
  48.     while (n != 0) {
  49.         r = (n % 2 == 0 ? "0" : "1") + r; n /= 2;
  50.     }
  51.  
  52.     while (r.size() < 4) {
  53.         r = "0" + r;
  54.     }
  55.  
  56.     return r;
  57.  
  58. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×