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?
#include <iostream>
#include <ctime>
#include <cstdlib>
usingnamespace std;
// Main function starts program
int main()
{
srand(static_cast<unsignedint>(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;
}
elseif (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;
}
elseif (guess > num)
{
count++;
tries--;
cout << "The number you have entered is too high." << endl;
cout << "You have " << tries << " tries left." << endl;
}
elseif (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;
}
-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.
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.