how do i ensure a correct integer input

This is a discussion on how do i ensure a correct integer input within the C++ Programming forums, part of the General Programming Boards category; this may seem a lower grade question, but there maybe a trick Code: int grpNo; do { cout << "\n\nEnter ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    how do i ensure a correct integer input

    this may seem a lower grade question, but there maybe a trick

    Code:
            int grpNo;
    
    	do
    	{
    		cout << "\n\nEnter the " << statement << " number of existing groups : ";
    		cin >> grpNo;
    		statement="valid!...";
    		cin.ignore();
    	}
    	while( !isdigit(grpNo) );
    If i enter non integer values, the program throws a DEBUG error because is expecting an integer, so this happens even before i validate .. I changed int grpNo to char grpNo .. but the problem is i'm gonna use grpNo in a for loop as a limiter i.e. for(i=0; i<grpNo;i++)

    kinda stuck ?
    Last edited by csonx_p; 09-15-2008 at 01:40 PM.

  2. #2

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    I hope i wont have to install extra stuff like i did with boost to use <limits> ... Was a bit of pain i wouldn't like to go through to get it to work ...

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Quote Originally Posted by csonx_p View Post
    I hope i wont have to install extra stuff like i did with boost to use <limits> ... Was a bit of pain i wouldn't like to go through to get it to work ...
    You don't need boost for <limits>, and you certainly don't for input validation either.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by iMalc View Post
    You don't need boost for <limits>, and you certainly don't for input validation either.
    I thought you could also validate inputs using boost library ... integer_traits stuff and max const

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    tried using your function below..

    Code:
    	template<typename charT, typename traits, typename T>
    	inline bool basic_input(std::basic_istream<charT,traits>& is, T& x, charT delim)
    	{
    		std::ios::fmtflags old_flags = is.flags();
    		if ((is >> std::noskipws >> x) && (is.eof() || is.get() == delim))
    		{
    			is.flags(old_flags);
    			return true;
    		}
    		is.flags(old_flags);
    
    		x = T();
    		is.clear();
    		ignore_line(is, delim);
    		return false;
    	}
    
    [EDIT] ..... SORRY provided a wrong one, here's a correct one
    
        template<typename charT, typename traits, typename T>
        inline bool basic_input(std::basic_istream<charT,traits>& is, T& x)
        {
            return basic_input(is, x, is.widen('\n'));
        }
    [/EDIT]
    If i enter a character value, it returns true shouldn't it be accepting for integers only?
    Last edited by csonx_p; 09-16-2008 at 11:38 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It depends on how you use it. Can you post the code that calls the function?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Daved View Post
    It depends on how you use it. Can you post the code that calls the function?
    I forgot to change back char grpNo; to int grpNo; now solved !

    May i ask, i have a problem of reading unwanted input .. But if i call cin.ignore() then the program expects me to enter something at least once.. I find this irritating. Is there a way i can ignore stdin without pausing the screen?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Again, an example would really help.

    Calling cin.ignore() when there's nothing in the input stream will pause the program until enter is pushed. You have to identify when it needs to be called and only call it at that time. There are ways to make sure it doesn't pause the program when you don't want it to, but it really depends on the situation.

    Also, if you're using basic_input.h from the post I linked to then you shouldn't need to call cin.ignore() anywhere.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Daved View Post
    Again, an example would really help.

    Calling cin.ignore() when there's nothing in the input stream will pause the program until enter is pushed. You have to identify when it needs to be called and only call it at that time. There are ways to make sure it doesn't pause the program when you don't want it to, but it really depends on the situation.

    Also, if you're using basic_input.h from the post I linked to then you shouldn't need to call cin.ignore() anywhere.
    can i use basic_input() for enums?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    I typically read a string and use boost::lexical_cast to validate (and convert) the input.
    It's used directly from the headers, so no "installing" is necessary.
    It also avoids having to deal with input buffer problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> can i use basic_input() for enums?

    Good question. If you can use cin >> then you can probably use basic_input. I'm not sure if you can use operator>> with enums, though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  3. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  4. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 07:30 PM
  5. Check if input password is correct
    By kagemand in forum C++ Programming
    Replies: 2
    Last Post: 11-28-2001, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21