Post: [C++] guess Numbers game [TUTORIAL]
03-01-2013, 09:22 PM #1
SC-Wotan
Do a barrel roll!
(adsbygoogle = window.adsbygoogle || []).push({});
    [COLOR="#FFA500"]#include <iostream> [COLOR="#008000"] //  Header that defines the standard input/output stream objects[/COLOR]
#include <conio.h>[COLOR="#008000"] // for getch() - pause[/COLOR]
#include <ctime>[/COLOR] [COLOR="#008000"] // This header file contains definitions of functions to get and manipulate date and time information.[/COLOR]

[COLOR="#0000FF"][B]using namespace[/B][/COLOR] std;

[COLOR="#EE82EE"]int[/COLOR] main([COLOR="#EE82EE"]void[/COLOR])
{
[COLOR="#EE82EE"]int[/COLOR] number = [COLOR="#FFA500"]0[/COLOR];
[COLOR="#EE82EE"]int[/COLOR] random_number = [COLOR="#FFA500"]0[/COLOR];
[COLOR="#EE82EE"]int[/COLOR] number_limit = [COLOR="#FFA500"]100[/COLOR];
[COLOR="#EE82EE"]int[/COLOR] try = [COLOR="#FFA500"]0[/COLOR];
[COLOR="#EE82EE"]int[/COLOR] max_try = [COLOR="#FFA500"]5[/COLOR];

srand(([COLOR="#EE82EE"]unsigned[/COLOR])time([COLOR="#FFA500"]0[/COLOR])); [COLOR="#008000"] // Initialize random number generator[/COLOR]


cout << "[COLOR="#696969"]Welcome to guess Number ![/COLOR]" << endl; [COLOR="#008000"] // cout = Standard output Text[/COLOR]
cout << "[COLOR="#696969"]In this game you have to guess a number between 0 and 100 ![/COLOR]" << endl; [COLOR="#008000"] // endl = Inserts a new-line character[/COLOR]
cout << "[COLOR="#696969"]But that would be too easy, you have just 5 attempts. Good Luck ![/COLOR]" << endl << endl << endl;

random_number [COLOR="#0000FF"]=[/COLOR] [COLOR="#0000FF"]([/COLOR]rand[COLOR="#0000FF"]()%[/COLOR]number_limit[COLOR="#0000FF"])+[/COLOR][COLOR="#FFA500"]1[/COLOR];

for(try = 1; try <= max_try; try++)
{

if(number > 100)
cout << endl << "Your pick is to big" << number ;

if(number < 0)
cout << endl << "Your pick is to small" << number;

if (try > 1 && number > random_number)
cout << endl << "the number is <" << endl;
else if(versuche > 1 && number < random_number)
cout << endl << "the number is >" << endl;

cout << endl << "Try : " << try << endl << endl;
cout << "How is your guess ?" << endl << endl;
cin >> number;

if(number == random_number)
break;
}

if(number == random_number)
cout << endl << endl << "Winner Winner Chicken Dinner, The Number was: " << random_number << endl << endl;
else
cout << endl << endl << "Game Over you lose, The Number was: " << random_number << endl << endl;

_getch();

return 0;
}
03-01-2013, 09:47 PM #2
Pichu
RIP PICHU.
Rather than declaring and initializing each int on all those lines; do this,

int number, random_number, number_limit, try, max_try;
number = random_number = try = 0;
number_limit = 100;
max_try = 5;

It helps contain your int variables.
03-01-2013, 10:14 PM #3
SC-Wotan
Do a barrel roll!
Thanks for your Reply, i know that, but i think for beginners that does not look so complicated
03-02-2013, 01:14 AM #4
haha usually I just do

#include <time.h>

then..

srand(TIME(NULL));

I've never seen the (unsigned)time(0) before :p

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo