I set out to make a Random number generator.
I am still new to C++. I made my first random number generator, then tried making it a different way, and then another way. My reason, is the syntax. I am unsure how it is supposed to be put together.
The reason for this post is for constructive criticism, and advice on how to be better at what I do, and to learn the correct way.
#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <fstream>
#include "time.h"
usingnamespace std;
////////FUNTIONS///////
int SetLevel();
int setvariable();
int levelbegin();
////PUBLIC VARIABLES///
bool gamerun = true;
int levelnumber; //starts at 1
int outof;//starts at 10
int randomnumber;
int playeranswer;
int tries; //starts at 2 //HOW MANY GUESSES YOU GET IN THE LEVEL
int tried; //starts at 0 // HOW MANY GUESSES YOU GUESSED
int main()
{
srand (time(NULL));
setvariable();
while(gamerun == true)
{
for(;;)
{
//////////////////////ANSWER CHECKER//////////////////////////////
if(tried >= tries) //IF YOU LOSE//
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n THE NUMBER WAS = " << randomnumber << " YOU LOST\n\n\n";
Sleep(3000);
setvariable();
break;
}
cin >> playeranswer;
//NUMBER IS TO SMALL
if(playeranswer < randomnumber)
{
cout << "TOO LOW, tries left: " << (tries-tried-1) << "\n\n";
tried +=1;
}
//NUMBER IS TO BIG
if(playeranswer > randomnumber)
{
cout << "TOO HIGH, tries left: " << (tries-tried-1) << "\n\n";
tried +=1;
}
//NUMBER IS CORRECT-NEXT LEVEL
if(playeranswer == randomnumber)
{
cout << "\n\n\n\n great Job, Next Level!\n\n";
Sleep(500);
SetLevel();
levelbegin();
}
}
}
}
/////////////SETS VARIABLES/BEGINS LEVEL//////////////////
int setvariable()
{
levelnumber = 1;tried = 0;
outof = 10;
tries = 4;
tried = 0;
randomnumber = (rand()%(outof-1))+1;
levelbegin();
}
////////////SETS LEVEL PARAMETERS/////////////////////////
int SetLevel()
{
levelnumber+=1; //ADDS A LEVEL
outof = levelnumber*10; //TIME LEVEL # BY 10 TO = THE NUMBER YOU GUESS FROM
randomnumber = (rand()%(outof-1))+1; //gives you random number 1 through a number
tries = tries+1; //gives you +1 try each level
tried = 0; //resets tried for next level
}
///////////BEGINS EACH LEVEL////////////////////////////
int levelbegin()
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" << "Guess the number I am thinking of!\n"; //more neat in-game experience
cout << " LEVEL= " << levelnumber << "\n Number range between 1-" << outof << "\n Tries= " << tries << "\n\n";
}
I think firstly would be the time header shouldn't it be #include <ctime> for c++ standard or #include <time.h> for non-standard c library. Secondly you are using all of std namespace in the global scope but only actually using std::cout in your functions..and thirdly don't use global variables...which you called public variables which I guess technically is correct because public means you can use outside of its scope. Then you actually never set the default values for tries and tried like you said like palauan said. ANd on line 46 and 52 if you are ever incrementing a number by 1 instead of number = number +1 you can just do number++; so on line 46 you can put tried++; By the way the formula for getting a pseudo random number with the rand() function is rand() % ( HIGH - LOW + 1 ) + LOW;
palauan73-
Oh yes, sorry bout that,some of the //comments were from yesterday, and I have changed a few things.
giblit-
I am learning C++ myself, and Ive googled it many times, and couldnt find a definite answer on the time thing. thanks for clearing this up, I will note, #include <ctime> or #include <time.h> (whats the difference between standard and non standard?/pros and cons?)
number = number +1;
number++;
^^ I learned this in java script, but I didnt think about it, thanks for that info, ill note that also.
rand() % ( HIGH - LOW + 1 ) + LOW;
^^^^^ this will give me a "random" number each time I call it?
if so, I dont need ctime.
standard is c++ modern and non-standard is outdated c header AFAIK
Rougeace4 wrote:
rand() % ( HIGH - LOW + 1 ) + LOW;
^^^^^ this will give me a "random" number each time I call it?
if so, I dont need ctime.
You will still need to call the random seed srand( time( NULL ) ) atleast once preferably at the top of your main function so that way you will generate a new seed. Otherwise each time you try and call a random number it will more than likely be the same number.
looks okay to me but I would put a parameter in the prototypes you declared then make all your variables in the main function and run those variables in the functions you are calling.
ex:
So, make the variables I need inside the parameters of the functions?
Also, I notice everyone else using; std::
instead of how I do it; usingnamespace std;
is the way I do it, not correct, and can it lead to difficulties in the future?
it can lead to slowing down your program because there are a ton of things in the namespace of std if you do unsing std::cout in the function you are using cout it is better but it is best to just do std::cout each time you use it and don't bother with the using std::whatever
Does your code generate a random number? I suspect std::rand() doing this instead.
Anyway, I just wanted to add that you may try other (objectifiable) random number generators such as the mersenne twister engine. Documentation: http://en.cppreference.com/w/cpp/numeric/random