How can I make my program more efficient?

I am doing a project where I have to make a random number guessing game. The program works fine but just for habit learning, how can I make this program more efficient?
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
  #include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>
#include <time.h>

int master(int username, int max);

using namespace std;

int main()
{
	srand (time(NULL)); //seeds random number generator

	int guessNum = rand() %100+1;
	int attempt = 0;
	int guess;
	bool winner = false;
	bool goOn = true;
	string username;
	string answer;

	cout << "Please type name" << endl;//introduction
	cin >> username;
	cout << "Hello " <<username<< "! I am thinking of a number between 1 and 100!\nYour job is to guess what number I am thinking of!" <<endl;
	cout << "You can begin guessing below" << endl << endl;

	while(winner == false)//guessing
	{
		cin >> guess;
		attempt++;

		if (guess > guessNum)
		{
			cout << "To high!\n\n";
		}
		else if (guess < guessNum)
		{
			cout << "To low!\n\n";
		}
		else
		{
			cout << "You won! You got it in " << attempt << " guesses!\n";
			winner = true;
		}
		
	}

	while(goOn) //continue or note
	{
		cout << "Would you like to play again? [y/n]\n";
		cin >> answer;

		if (answer == "y")
		{
			goOn = false;
			main();
		}
		else if (answer == "n")
		{
			return 0;
		}
		else
		{
			cout << "Make sure to type in either lowercase y or lowercase n" <<endl;
		}
	}

	
	system ("pause");

}
For starters, main() shouldn't call itself - use a do..while loop to restart the game.

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

#include <iostream>
#include <time.h>
using namespace std;

int main()
{

	bool playing= true, guessing = true;
	int guess, number;

	srand(time(0));

	do
	{
		number = rand() % 100 + 1;
		cout << "Im thinking of a number between 1 and 100, try guess it: ";
		do
		{
			cin >> guess;
			if (guess == number)
				guessing = false; // we guessed it
			else
				if (guess > number)
					cout << "Too High!" << endl;
				else
					cout << "Too Low!" << endl;
			
		} while(guessing);

		cout << "Well done you got it, it was " << number << endl;

		// here you could ask the question do they want to play again
		// and if no, just set playing to false.

	} while (playing);

	return 0;
}
This is the second time in the last week or so I've seen someone post code calling main(). It's not even legal C++! What compiler are you using that allows this?

Anyways, on topic now:
OP, your program is too short and doesn't really do anything of significance, so there won't be anything to improve on efficiency wise.

Thinking about efficiency is a good habit to be in, but right now just continue writing code and improving yourself. You will know when you write something needs to be more efficient, and you likely will have ideas of how to improve it.
I am using Microsoft visual c++ 2010 express

Although Visual Studio lets you place main() as a function call you will end up with a warning telling you that this would result in a runtime stack overflow - its not legal as ResidentBiscuit said and shouldnt be used.

Warnings are just as important as errors, you should take note of them and act on them. Anyway, Visual Studio would show something like this:

warning C4717: 'main' : recursive on all control paths, function will cause runtime stack overflow


Topic archived. No new replies allowed.