It's generally a bad idea to use std::cin all alone. One of the most used ways to get user input, no matter type is to use strings, stringstreams and the resulting type. We can make a template function, to do this for us.
Code:
template<typename Type>
bool GetInput(Type &destination, std::istream &stream = std::cin)
{
std::string temporaryHolder;
std::stringstream stringStream;
std::getline(stream, temporaryHolder);
stringStream << temporaryHolder;
return (stringStream >> destination);
}
This function accepts user input of
any kind. You can use it with integers, floating numbers, strings, and so on. That's because that the stringstream can "convert" with these types. First we're getting the user input, exactly as it was typed, into a string. Secondly, we're giving the string to the stringstream. Thirdly, we're sending it from the stringstream to the variable the user specified. The second parameter in the function, is simply the stream to get input from - in our example std::cin, because we want user input. It could had been a file stream too.
If you then want to use the function, it's a lot easier, and you don't need to do much. F.ex. if we want to get an integer, but will prevent something like "10.001", "123hello123", and so on.
Code:
int iMyInteger;
GetInput(iMyInteger);
// iMyInteger now holds a user value
To be even more sure, that the input actually worked, we can check it with the boolean value the function returns.
Code:
if(GetInput(iMyInteger))
// Everything went fine
else
// Everything went... wrong
As you can see you've a lot control, and it's easy to use.
I've other optimization tips for you as well.
You need to look at your headers, but also the headers and what the program actually uses of functions, objects, etc. First take a look at second line, where you're including the time header. In the way you're doing it, it's deprecated. In C++, all the old standardized libraries, have gotten "new names" if we can say that. So instead of the ".h" in the end, you put a 'c' in the start. It's not only for the time library, but also for stdlib, stdio, and so on.
Code:
#include <ctime>
#include <cstdlib>
#include <cstdio>
// ...
Now you've to look at the rest of the program too. You're using a lot of functions and objects, without including the headers for 'em. The first that felt into my eyes were std::string. You don't even includes the string library. You should. The second is exit(), which is btw not used anymore in C++. Alternatives would be std::exit(), or the good old return-statement, to do the same.
My last tip, is not something you've to change, but it is much easier to use, to you might want to think about it. It's how you're handling the different numbers, when printing the message about how many guesses you used for finding the right number. You're using if-statements... that's just fine, but with a single switch-statement, the code will be much nicer and cleaner. To "support" multiple cases, you can do as following.
Code:
case 1:
case 2:
case 3:
// Do something, if the number was 1, 2 or 3.
break;
When you're using exit() - or if it was me, a return-statement, you don't even need the break neither. Because when using those functions, the break-statement will never be executed anyway.
I've decided not to post the corrected code, so you've to do something by yourself. If you want me to post it, I'll post it in here, so you can take a look at it.