SYNTAX- I'm a beginner and need some help.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  #include <iostream>
#include <windows.h>
#include <cstdlib>
#include <fstream>
#include "time.h"

using namespace 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";
}

There is a lot wrong, so please try and help me. thanks guys!
first quickly you never initialize tries to 2 or tried to 0.
You should try
int tries=2;
int tried= 0;
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.


thanks both of you,
Id love anything I can get!
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.
So, I am assuming I should use standard?

Okay, yes I do need the time, I forgot about the seeding at the beginning thanks.


what do you think about the syntax?
how I use functions ect...


Also thanks so much for your help, I really appreciate it.
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const int add( const int , const int );

int main( int argc , char **argv )
{
    int number1 = 1 , number2 = 1;
    std::cout << "Please enter number 1\n> " << std::flush;
    std::cin >> number1;
    std::cout << "Please enter number 2\n> " << std::flush;
    std::cin >> number2;
    std::cout << number1 << " + " << number2 << " = " << add( number1 , number2 ) << std::endl;
    return( 0 );
}

const int add( const int num1 , const int num2 )
{
    return( num1 + num2 );
}

And yeah I would suggest using the standard library
Last edited on
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;
using namespace std;
is the way I do it, not correct, and can it lead to difficulties in the future?

I am sorry for all the questions.


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <chrono>
#include <random>
#include <iostream>
#include <set>

int main()
{
    // std::mt19937 en(std::time(nullptr)); // include <ctime>, only seconds.
    std::mt19937 en(std::chrono::steady_clock::now().time_since_epoch().count());
    std::set<int> s;
    for (;;)
    {
        int i = en();
        if (s.find(i) != s.end())
        {
            std::cout << s.size() << " elemements before reoccurrance";
            return 0;
        }
        s.insert(i);
    }
}
My code generates a random number from 1 too the int variable, which relies onthe level number.

I will look at that.
I was really looking for advice on syntax. or how I set up the functions, and everything.
Topic archived. No new replies allowed.