Determining whether input is an integer.

This is a discussion on Determining whether input is an integer. within the C++ Programming forums, part of the General Programming Boards category; if i ask a user to input an integer, how would i go about stopping somone from entering a character ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    Determining whether input is an integer.

    if i ask a user to input an integer, how would i go about stopping somone from entering a character because my program would crash?

    Thanks
    C++ can hurt.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    Check each character to see if its a digit. Use the isdigit() function included with ctype.h to do that. Allow a negative sign at the beginning if you want negative, too.

  3. #3
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    Look into isdigit(). Or you could look into isalpha().

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i know about the isdigit() and isalpha() but they take a character. I want to know if i can find out using the integer only.
    C++ can hurt.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    any other suggestions?
    C++ can hurt.

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    I don't understand... can you show us some code?

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    posting code is bad i have over 1500 lines... i think ill just let somone dl it instead.

    the problem is when a user inputs a length or a width or any numerical value. I want this to be error proof.
    Attached Files Attached Files
    C++ can hurt.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i want to know how i can let the program know when the user inputs a character instead of the integer because it crashes if i don't avoid this.
    C++ can hurt.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    well if you run the program you will see that when you are supposed to enter an double, and you accidentally enter a character, the program will crash because i don't have a way of catching that. I just want to knwo how to stop it from happening.
    An example :

    Code:
                                         while(x)
    				{
    					x = true;
    					z = true;
    					cout<<"Enter the Length of the beam (mm): ";
    					cin>>l;
    					if (l <= 0)
    					{
    						cout<<"The Beam must have a Length!  Enter Again."<<endl;
    						x = true;
    					}
    					else 
    					{
    						while (z)
    						{
    							string q;
    							cout<<"Is this Length correct: "<<l<<" (y/n)?"<<endl;
    							cin>>q;
    							if (q == "y" || q == "Y")
    							{
    								x = false;
    								z = false;
    							}
    							else if (q == "n" || q == "N")
    							{
    								x = true;
    								z = false;
    							}
    							else
    							{
    								cout<<"You have not entered a Proper Response!"<<endl;
    								z = true;
    							}
    						}
    					}
    				}
    as you can see the problem lies here in these lines:

    Code:
                                                 cout<<"Enter the Length of the beam (mm): ";
    					cin>>l;
    					if (l <= 0)
    					{
    						cout<<"The Beam must have a Length!  Enter Again."<<endl;
    						x = true;
    					}
    Now how do i stop people from entering a character because as you can see it'll ruin the program. If it was a character i could do it with isdigit() but its not. Hope this is more clear.
    Oh yeah there is no way that functions will shorten the code up, only the main. also all the funtions i need are in 2 classes. The rest of the stuff in the main is for input
    C++ can hurt.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    oh almost forgot i have another small problem. Whenever i run through the program using a certain "path", when i chose the same path again (to enter different values) , the program just skips the input and outputs exactly what i did the last time. (There is a while loop to allow the user to start over from teh top with new input). Does anyone have any ideas as to what is causing the problem. My latest version is here. Just take a run through it and chose the same options after the run and see for yourself.
    Thanks again
    Attached Files Attached Files
    C++ can hurt.

  11. #11
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    This ought to fix your problem:

    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib> // for atof()
    
    using namespace std;
    
    int main()
    {	string temp;
    	double value;
    	do
    	{
    		cout << "Enter a double: ";
    		cin >> temp;
    		value = atof(temp.c_str());
    	}
    	while (!value);
    	cout << value << " accepted as a valid double" << endl;
    	return 0;
    }
    Last edited by slaveofthenet; 04-18-2003 at 12:42 AM.

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    AFAIK there are two main data validation techiques.

    The first goes something like this. Only accept user input as a string. Then parse the string and remove anything that invalidates input as the desired type or reject entire input if any part of it invalidates input for use as the desired type. Then convert input to the desired type if it is valid.

    The alternative I'm aware of is to use the fail(), clear() and ignore() methods for istreams. That approach goes something like this:
    Code:
    int limit = size limit of ints availabe in climit or limit.h
    double temp;
    //attempt to read input into desired variable
    cin >> temp;
    //if unable to read input into desired variable
    if(cin.fail())
    {
       //clear the fail bit for the stream
       cin.clear();
       //then ignore up to limit (whatever it is) number of char
       //this clears the input buffer about as well as it can be cleared
       cin.ignore(limit);
    }
    This might have a problem if single char input accepted by compiler as int/double/float (assigning the ASCII value of a char to a numerical type) or if you try to read an int into a char (again based on ASCII values). Another problem here is if somebody stands on a key and ends up trying to enter more char than limit (say they hold their finger on the 'k' key for three minutes), but that would also probably be a potential problem for the string method as well).

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    elad, for that to work i need to include the header file climit?

    Correct?

    If so how do i determine the limit size?
    C++ can hurt.

  14. #14
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i decided it would be easier to use strings with stringstreams.

    Although now i have one problem. The isdigit call is not taking characters its taking integers! This is the first time i see this. Not only that but just to see if there actually was something wrong the isalpha call is also asking for integers only.

    Does anyone kow why it doesn't take the usual character and is asking only for integers?
    C++ can hurt.

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    here's an example:

    Code:
     				while(x)
    				{
    					x = true;
    					z = true;
    					// Problem starts here
    					string test;
    					cout<<"Enter the Length of the beam (mm): ";
    					cin>>test;
    					if(isdigit(test.c_str()))	
    					{
    						l = string_to_double(test);
    					}
    					// Problem ends here
    					else
    					{
    						cout<<"This program will only accept digits."<<endl;
    						x = true;
    					}
    
    					if (l <= 0)
    					{
    						cout<<"The Beam must have a Length!  Enter Again."<<endl;
    						x = true;
    					}
    					else 
    					{
    						while (z)
    						{
    							string q;
    							cout<<"Is this Length correct: "<<l<<" (y/n)?"<<endl;
    							cin>>q;
    							if (q == "y" || q == "Y")
    							{
    								x = false;
    								z = false;
    							}
    							else if (q == "n" || q == "N")
    							{
    								x = true;
    								z = false;
    							}
    							else
    							{
    								cout<<"You have not entered a Proper Response!"<<endl;
    								z = true;
    							}
    						}
    					}
    				}
    Is this even correct? I mean should this work?
    Thanks Again.
    C++ can hurt.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  3. Reading Input from file (Integer array)
    By Govalant in forum C Programming
    Replies: 9
    Last Post: 07-23-2007, 06:13 PM
  4. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 06:04 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM

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