I got the idea for this program from this site.
functions.cpp:
#include <iostream>
#include <cstdlib>
#include <iomanip>
namespace my
{
int getOneOrZero()
{
return (rand() >> 14); // >> the bitwise operator
}
// getArray leaves the numbers ( 0-99 ) that have in their bit position represented by bigFlag the num ( 0 or 1 )
void printNums(int8_t bitFlag, int num) // num is from getOneOrZero() so it's 1 or 0;
{
for (int counter = 0; counter < 100; ++counter) // there are quite a few implicit conversions to one byte integers
{
if ( (counter & bitFlag) != num*bitFlag )
std::cout << std::setw(8) << " ";
else
std::cout << std::setw(8) << counter;
if ( (counter % 9) == 0)
std::cout << std::endl;
}
}
char getAnswer()
{
while (1)
{
std::cout << "\n Is your number shown above ?\n\n 'y' for yes , 'n' for no , 'r' for reset : ";
char answer;
std::cin >> answer;
std::cin.ignore(32767,'\n');
if (std::cin.fail())
std::cin.clear();
if (answer == 'y' || answer == 'n' || answer == 'r') // I could use switch but nevermind
return answer;
}
}
void turnsPassed(int turns)
{
std::cout << "\n This is turn " << turns << "\n\n";
}
int swapZeroOrOne(int num)
{
switch (num)
{
case 0 :
return 1;
case 1 :
return 0;
default:
std::cout << "\nSwapZeroOrOne ERROR !\n";
break;
}
}
int getUpdateForGuessNum(int8_t flag, int num, char answer)
{
switch (answer)
{
case 'n' :
{ int newNum = swapZeroOrOne(num);
return newNum*flag;
}
case 'y' :
return num*flag;
case 'r' :
break;
default :
std::cout << "\n ERROR ! In getUpdateForGuessNum\n";
break;
}
}
}
main.cpp:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include "functions.hpp"
#include "constants.hpp"
#include <stdlib.h> // for system() commands >.<
int main()
{
srand( static_cast<unsigned int>(time(0)));
Reset : // goto !
system("cls");
int guessNum = 0;
// guessing loop !
for (int counter = 0; counter < 7; ++counter)
{
if (counter == 0)
std::cout << "\n Think of a number between 0 to 99\n";
my::turnsPassed(counter + 1); // +1 cause counter starts from 0
int num {my::getOneOrZero()}; // this gives a randomness to the numbers shown each time you run the programm
my::printNums(myVar::bitFlag[counter],num);
char answer { my::getAnswer() };
if (answer == 'r')
goto Reset;
guessNum |= my::getUpdateForGuessNum(myVar::bitFlag[counter],num,answer);
system("cls");
}
std::cout << "\n Your number is \n";
std::cout << "\n" << std::setfill('-') << std::setw(81) << "\n";
std::cout << std::setfill(' ') << std::setw(41) << guessNum << "\n";
std::cout << "\n" << std::setfill('-') << std::setw(81) << "\n";
system("pause"); // I should fix this
return 0;
}
functions.hpp:
#pragma once
namespace my
{
int getOneOrZero();
void printNums(int8_t,int);
char getAnswer();
void turnsPassed(int);
int getUpdateForGuessNum(int8_t,int,char);
int swapZeroOrOne(int);
}
constants.hpp:
#pragma once
namespace myVar
{
const int8_t bitFlag[] {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80};
}
Please suggest ways to improve this program. Explain the suggestions you make because I need to know why I should change something.