My first program !

Feb 26, 2016 at 10:45pm
Hey ! I have made my first stable program, does somebody have any suggestions or tips to improve my program ?

This is my code :

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
#include <iostream>;
#include <cstdlib>;
#include <ctime>;

using namespace std;

int main()
{

	

	//create a generation's seed by the time
	srand(time(0));
	int choice;

	do
	{
		std::cout << "-=Number Generator=- \n \n 1. Generate truely numbers \n 2. Generate a number between 2 numbers" << "\n ";
		std::cin >> choice;
		std::cout << endl;
	} while (!(choice == 1 || choice == 2));
	
	int gen_num;

	//option 1
	if (choice == 1)
	{

			std::cout << "How many number(s) you want to generate ? ";
			std::cin >> gen_num;
			gen_num += 1;

			for (int x = 1; x < gen_num; x++)
			{
				std::cout << x << ". " << rand() << "\n";
			}
			
		
	} 

	
	int num_min_gen; 
	int num_max_gen;
	
	//option 2
	if (choice == 2)
	{
		std::cout << "\n What is the smaller number you want to generate ? ";
		std::cin >> num_min_gen;
		std::cout << "\n What is the taller number you want to generate ? ";
		std::cin >> num_max_gen;
		std::cout << "\n How many number(s) you want to generate ? ";
		std::cin >> gen_num;
		gen_num += 1;

		for (int x = 1; x < gen_num; x++)
		{
			std::cout << x << ". " <<  num_min_gen + (rand() %num_max_gen) << "\n";
		}
	}
	

	//end of the program
	std::cout << "\nPress ENTER to quit...";
	std::cin.get();
	std::cin.get();

	return 0;
}


Thanks for your time !

-Alex
Feb 26, 2016 at 11:17pm
I have some suggestions for improving your formatting of the code.

- Remove any unnecessary space, make the length (number of lines) as compact as possible.
- Try to always put your variable declaration at the top.
- If you're writing std:: before everything, you don't need to use 'using namespace std;'
Last edited on Feb 26, 2016 at 11:17pm
Feb 26, 2016 at 11:20pm
Try to always put your variable declaration at the top.

Bad advice. Declare variables when they are needed. Clumping them up is just really bad practice, take advantage of the fact that you can declare them anywhere in c++.

http://stackoverflow.com/questions/3773396/declare-variables-at-top-of-function-or-in-separate-scopes

Another suggestion is to drop rand() since it's pretty terrible and not actually random -
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

and use the modern <random> header -
http://www.cplusplus.com/reference/random/


1
2
3
#include <iostream>;
#include <cstdlib>;
#include <ctime>; 


These semicolons are pointless and shouldnt be there.
Last edited on Feb 26, 2016 at 11:23pm
Feb 27, 2016 at 12:34am
How is it bad to organize the variables from everything else in the code? Being able to declare them anyway doesn't mean it's good to declare them anywhere...
Last edited on Feb 27, 2016 at 12:38am
Feb 27, 2016 at 3:32pm
1
2
3
#include <iostream>;
#include <cstdlib>;
#include <ctime>;  
These semicolons are pointless and shouldnt be there.


Why ? I need iostream O_O !
Feb 27, 2016 at 3:39pm
Why ? I need iostream O_O !

You don't need semicolons after including a file... They are part of the preprocessor.

http://stackoverflow.com/questions/10011318/why-include-directive-doesnt-have-a-semi-colon-at-the-end-of-statement
Last edited on Feb 27, 2016 at 3:40pm
Feb 27, 2016 at 3:48pm
Ah! Ok thanks ! but i don't understand how to use <random>

1
2
random_device rd;
   cout << rd();


When i type it, that generate a "random number" but it is the same :|

I have tried to modify the arguments like that :
1
2
random_device rd;
   cout << rd(1, 10);


Error :/
Feb 27, 2016 at 4:06pm
There is an example here showing you how to use it -
http://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution


You can also find youtube videos and other examples on the googles.
Topic archived. No new replies allowed.