Nag aaral ako ng c++, about a month na. Hirap pala neto haha pero enjoyable.
So, share ko lang yung guessing game ko in c++.
So, share ko lang yung guessing game ko in c++.
Code:
// guranume.cpp: A numerical game against the computer
#include <iostream>
#include <ctime> // Prototype of time()
#include <cstdlib> // Prototype of srand() and rand()
using namespace std;
int main ()
{
unsigned sec = time(0); // Get the time in seconds
srand(sec); // Seeds the random number generator
cout << "\n\t****** Numerical Guessing Game ******"
<< "\n\tRules;\n"
<< "\t\t I have a number between 1 to 15 in mind,\n"
<< "\t\t You have three chances to guess correctly!" << endl;
char play = 'r'; // be able to replay <r> or quit <q>
int i = 1;
while (play == 'r')
{
int ranNum = rand() % 15 + 1; // random number between 1 to 15
bool foundAnswer = false;
int numTry = 1, answer;
while (!foundAnswer && numTry <= 3)
{
cin.sync(); // clear input buffer for
cin.clear();
cout << "\n\t\t Trial " << numTry++ << ". : ";
cin >> answer;
if ( answer == ranNum) foundAnswer = true;
else if (answer < ranNum) cout << "\t\t Too small!" << endl;
else cout << "\t\t Too big!" << endl;
}
if (!foundAnswer)
{
cout << "\n\t\t I win! You lose! ha ha ha"
<< "\n\t\t The number in question is: " << ranNum << endl;
}
else cout << "\t\t Argh you Win!" << endl;
cout << "\n\t\t Play again?"
<< "\n\t\t Replay -> <r>\t\tQuit -> <q> : ";
cin >> play;
}
return 0;
}
Last edited: