What's new

C & C++ Numerical Guessing Game

babita14

Honorary Poster
Joined
Oct 2, 2016
Posts
371
Reaction
102
Points
165
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++.

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:
Ganun siguro pag na interest at walang magawa. Wala kasing pressure pag inanaaral nalng, diko to magugustuhan kung studyante ako
 
any motivation sir para ma enjoy ang programming, 2nd year na ako di padin magaling mag programming hahahaha
 
Huwag mo sukuan kasi hindi nasa gitna yung kasiyahan at tagumpay. Try and try lang na aralin.
 
Hindi naman ako magaling, antagal ko yang inupuan. Almost a month haha

Trust the process lang. Matoto para sa ikabubuti ng sarili mo, wag kang matoto para sa ikalalamang mo sa iba. Don't campare yourself with others. Iba ka, iba ako at higit sa lahat, iba sila.
 
Sige lang lods.
may bug pala yan. di ko nalaman ko pano e debug. Balik trabaho na kasi. busy na.

Bug:
Nagiging infinite loop
Diba integer lang dapat answer. kunwari nagkamali ka ng na input para sa answer at na input mo ay letter(s) nagiging infinite loop.
 
I found a warning in it pero hindi naman ata bug kasi yung hinihingi mo number hindi letters. Yes I tried it at talagang hindi na humihinto. Still new sa c++ rin eh.

Screenshot 2021-01-24 213058.png
 

Attachments

nakaka enjoy po kapag hindi limited yung oras mo. like hindi mo need magpasa on time. kapag nappressure ka po kasi, ang hirap talaga matuto
 
[XX='crxz619, c: 885293, m: 948334'][/XX]Tama. Pero trust the process lang mga paps and time management. Know your Priorities ika nga😊
 

Similar threads

Back
Top