Reputation: 362
I make a game that the user need to guess the number,
the number is generated with rand
function.
If the user wrote an invalid number or a character, print error message.
My problem is that cin.fail()
does not working well for me, for example, when I enter a character as input, my program is always printing "Too Low!", maybe because it calculates the value of the character ( ASCII TALBE ).
Any suggestions ?
My Code:
void Game()
{
srand(time(0));
int iGuess;
const unsigned int iNum = (rand() % 1000 + 1);
Start:
system("cls");
cout << "\n Guess the Num: "; cin >> iGuess;
if (iGuess == iNum) {
system("color A");
cout << "\n\n Good Job! You Won!";
exit(0);
}
if (iGuess > iNum) {
cout << "\n\n Too High!";
Sleep(3000);
goto Start;
}
if (iGuess < iNum) {
cout << "\n\n Too Low!";
Sleep(3000);
goto Start;
}
if (cin.fail()) {
cout << "Input has failed! & Error Code: " << GetLastError();
Sleep(3000);
goto Start;
}
}
Upvotes: 2
Views: 832
Reputation: 10982
First you can check that for streams, boolean conversion (explicit operator bool() const
) is equivalent to !failed()
(see https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool).
This allows you to write:
void Game()
{
srand(time(0));
int iGuess;
const unsigned int iNum = (rand() % 1000 + 1);
Start:
system("cls");
cout << "\n Guess the Num: ";
if (cin >> iGuess)
{
if (iGuess == iNum)
{
system("color A");
cout << "\n\n Good Job! You Won!";
exit(0);
}
if (iGuess > iNum)
{
cout << "\n\n Too High!";
Sleep(3000);
goto Start;
}
if (iGuess < iNum)
{
cout << "\n\n Too Low!";
Sleep(3000);
goto Start;
}
}
else
{
cin.clear(); // clear error flags
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // empty buffer
assert(cin); // check that we are still in a good state
cout << "Input has failed! & Error Code: " << GetLastError();
Sleep(3000);
goto Start;
}
}
If an error happened it is important not to forget these steps:
Clear error flags:
cin.clear();
Remove all the data previously stored in the buffer (and which could not be interpreted as an integer)
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Check that we are still in a good state (defensive programming, another error could have happened)
assert(cin);
Update:
it is certainly better to use ignore
, my first version was
while (cin.get() != '\n') continue;
my update is:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Upvotes: 1
Reputation: 573
I recommend utilizing the do while format to get your input.
#include <stdlib.h>
#include <time.h>
#include <iostream>
#ifdef _WIN32
#include <windows.h>
void sleep(unsigned milliseconds)
{
Sleep(milliseconds);
}
#else
#include <unistd.h>
void sleep(unsigned milliseconds)
{
usleep(milliseconds * 1000); // takes microseconds
}
#endif
using namespace std;
int main()
{
srand(time(0));
int iGuess;
const unsigned int iNum = (rand() % 1000 + 1);
Start:
system("cls");
do
{
if (!std::cin)
{
std::cin.clear();
std::cin.ignore(10000, '\n');
std::cout << "\nFailed input";
}
std::cout << "\n Guess the Num: ";
} while (!(std::cin >> iGuess));
if (iGuess == iNum) {
system("color A");
cout << "\n\n Good Job! You Won!";
exit(0);
}
if (iGuess > iNum) {
cout << "\n\n Too High!";
Sleep(3000);
goto Start;
}
if (iGuess < iNum) {
cout << "\n\n Too Low!";
Sleep(3000);
goto Start;
}
}
Upvotes: 0
Reputation: 1150
The simplest way to parse the input string using std::string_view
and std::isdigit()
.
char str[] = "12abc12";
int alphabet = 0, number = 0, i;
for (i=0; str[i]!= '\0'; i++)
{
// check for alphabets
if (isalpha(str[i]) != 0)
alphabet++;
// check for decimal digits
else if (isdigit(str[i]) != 0)
number++;
}
https://www.geeksforgeeks.org/isalpha-isdigit-functions-c-example/
Upvotes: 0