I am facing problem in using cin.fail() function!! The problem is that when I want to check that is user enter correct input or not I am using a do-while loop that force the user to enter a valid number but it didn't works!!! the code is here:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int no;
do
{
cout << "Enter a number: ";
cin >> no;
if (cin.fail())
{
cout << "Please Enter a valid Integer!!";
}
}
while(!cin.fail());
cin >> no;
return 0;
}
But when I enter an integer it again shows enter a number[b/] but when I enter a character it works fine and terminate showing the message Please Enter a valid Integer but I want that if user enter a character then it force the user to enter a no otherwise not.
Also when I remove [b]! (negation) from while then this code doesn't work properly i.e., when I am enter a no it do nothing but when I will enter a character an infinite loop is execute.
how to get rid from this problem!!!!!

New Topic/Question
Reply



MultiQuote




|