Code Efficiency

Let me start by saying hello and that I am new to C++. I am a systems administrator and have been for years however I have always loved coding but never really got into until recently. I have only been coding a few weeks, reading mostly, but finally wrote my first program. A simple guessing game application.

Although the code works as intended I am wondering if there is a more efficient way to write it. I can write out the code on paper and I know what I want it to do but have trouble turning that into syntax.

If anyone can analyze this code and possibly give me some pointers in making it more efficient I would really appreciate it. Would it be better to use functions instead of doing everything in main()? What are the benefits to using separate functions over main for a program this small?

I also have trouble coming up with projects I would like to create on my own. Are there any sites that offer "quizzes" or "projects" that are good to try and learn on?

Thanks in advance.

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

using namespace std;


// Main function starts program
int main()
{
    srand(static_cast<unsigned int>(time(0)));  //seed random number generator
	
	int num = rand() % 100 + 1;		// random number between 1 and 100
	int tries = 10;					// set number of tries
	int count = 1;					// set counter
	int guess = 0;					// set number of guesses
	
	cout << "You have 10 tries to guess the number." << endl;
	
	
	while (guess != num)
	{
		if (tries == 0)		// ran out of tries
		{
			break;
			cout << endl;
		}
		
			cout << "Guess a number between 1 - 100: ";
			cin >> guess;
			cout << "\n";
			if (guess > 100 || guess < 1)
			{	
				cout << "You have entered a number outside the range." << endl;
				cout << "You will not be charged with a guess." << endl;
				continue;
			}
		
			else if (guess == num)
			{
				cout << "Correct, " << guess << " is the magic number." << endl;
				if (count == 1)
					cout << "You guessed the number in " << count << " try." << endl << endl;
				else
					cout << "You guessed the number in " << count << " tries." << endl << endl;
				return 0;
			}
			
			else if (guess > num)
			{
				count++;
				tries--;
				cout << "The number you have entered is too high." << endl;
				cout << "You have " << tries << " tries left." << endl;
			}
		
			else if (guess < num)
			{
				count++;
				tries--;
				cout << "The numbered you have entered is too low." << endl;
				cout << "You have " << tries << " tries left." << endl;
			}
	}
   
	cout << "\nSorry you failed to guess the number." << endl;
	cout << "The number was " << num << "." << endl;
	
	
	return 0;
}
A couple of pointers:

-It's bad style to have multiple return statements in main, there are exceptions when dealing with other functions.

-You could get rid of the if block inside the loop if you made the while condition the following: while (tries != 0) if you still use a break statement when the user guesses the number, I don't like break statements though, as it can be confusing to read.

-You could declare guess within the while loop, keeping the scope of variables as limited as possible is advised.

-While your code is readable, functions can make it easier to read.

Some projects or challenges:

http://www.dreamincode.net/forums/topic/78802-martyr2s-mega-project-ideas-list/

https://www.hackerrank.com/categories/algorithms/warmup
Last edited on
And some minor notes:

Line 26 (cout << endl;) is never reached because the if() block is left at break;.

Be careful with things like the lines 42 to 46. It is recommended to use {} even if there's just one line in the if or else block.
endl will perform a buffer flush, simply adding "\n" (newline escape sequence) to the sentence will allow the buffer management functions to handle this aspect of the cout object.
1
2
3
4
5
/* hurts performance (probably un-noticeable with so few screen prints) */
cout << "You guessed the number in " << count << " try." << endl << endl; 

/* better performance */
cout << "You guessed the number in " << count << " try.\n\n";



In general code structure (functions or not) won't affect the performance of a small piece of code like this (and will probably be optimised out), things to look out for and OS/library calls, they can slow things down. Also blocking instructions like a flush instruction or a memory barrier.

Apart form that there's nothing obvious that could negatively affect the performance of this code.
Last edited on
Topic archived. No new replies allowed.