6 Replies - 2635 Views - Last Post: 11 July 2012 - 10:15 AM Rate Topic: -----

#1 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

cin.fail() problem!

Posted 30 June 2012 - 12:28 AM

Hello!!!
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!!!!!

Is This A Good Question/Topic? 0
  • +

Replies To: cin.fail() problem!

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: cin.fail() problem!

Posted 30 June 2012 - 12:57 AM

This link might prove useful... After reading that, you might understand that you check if cin has failed. If it has, you prompt user to enter number again, on line 15. Then, while checks whether cin has failed. It has, so it doesn't continue looping. So, remove the ! from your while. So, all you have to do is remove the ! from while, and add cin.clear() and cin.ignore() to your code. But be careful, because if you place these two in the if condition(line 14-16), then it will clear the cin, and cin will no longer be in failed state. So it will stop looping.
Oh, and remove the conio.h header from your code. You are not using it, and it is non-standard. So try to avoid it.

EDIT : Found a very similar question... Here it is :)

This post has been edited by aresh: 30 June 2012 - 01:01 AM

Was This Post Helpful? 1
  • +
  • -

#3 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: cin.fail() problem!

Posted 11 July 2012 - 09:01 AM

Ok Mr. Aresh. I use cin.fail() and cin.ingnore() functions but when I enter a single character it works fine but when I enter two or more characters the do-while loop runs for character times means if I enter qw the do-while executes for two times and prints Enter a number for two times..!! here is my code:
#include <iostream>

using namespace std;

int main()
{
    int no;
    
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore();
        }
        cout << "Enter a Number: ";
        cin >> no;
    }
    while(cin.fail());
    
    cout << no;
    
    return 0;
}



But when I use cin.ignore(numeric_limits<streamsize>::max(), '\n') I got the following errors. How to get rid from this problem! Errors are:
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp||In function 'int main()':|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|8|error: 'cin' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|11|error: 'numeric_limits' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|11|error: 'streamsize' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|11|error: '::max' has not been declared|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|13|error: 'cout' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|14|error: 'cin' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|16|error: 'cin' was not declared in this scope|
H:\Books\Programming Language\C - C++\My Programms\Code Blocks\cin.fail\main.cpp|17|error: 'cout' was not declared in this scope|
||=== Build finished: 8 errors, 0 warnings ===|

Was This Post Helpful? 0
  • +
  • -

#4 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,176
  • Joined: 08-January 12

Re: cin.fail() problem!

Posted 11 July 2012 - 09:06 AM

That is because it first accepts "q", prints the error message, instantly reads the "w" from stream and prints the second error.
And in your code, I changed the code to
if(cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

And it gave no errors. Strange. Which compiler do you use ??
Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4159
  • View blog
  • Posts: 12,953
  • Joined: 25-December 09

Re: cin.fail() problem!

Posted 11 July 2012 - 09:06 AM

Did you include the proper include file for using numeric_limits?

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: cin.fail() problem!

Posted 11 July 2012 - 10:07 AM

@aresh
you are right. this gives no error if we include limits header file that I forgot to include. when I include this header file, compiler successfully compile my code.
thanks for guiding me.
here is my complete code that's has no error:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
    int no;
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        cout << "Enter a Number: ";
        cin >> no;
    }
    while(cin.fail());
    cout << no;
    return 0;
}



@jim
you are right. I am forgot to include header files for using numeric_limits
thanks for guiding me.

This post has been edited by jimblumberg: 11 July 2012 - 10:08 AM
Reason for edit:: Fixed Code tags.

Was This Post Helpful? 0
  • +
  • -

#7 Red Prince  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 03-June 12

Re: cin.fail() problem!

Posted 11 July 2012 - 10:15 AM

@Aresh
I used Code::Block compiler but problem is occur because we are forgot to include limits header files in order to use numeric limits. here is my complete program that generates no error. Thanks for guiding me.
#include <iostream>
#include <limits>
using namespace std;
int main()
{
    int no;
    do
    {
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }
        cout << "Enter a Number: ";
        cin >> no;
    }
    while(cin.fail());
    cout << no;
    return 0;
}



@Jim
you are right. I am forgot to include header file for numeric limits. Thanks for guiding me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1