Thread: Integer input

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Integer input

    Hey all.

    I just have a small question which has been bugging me for the last day or so. Consider the following program:

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int test;
    
    	cout << "Enter a number: ";
    	cin >> test;
    
    	cout << test << endl;
    
    	/* I'm aware you can use isdigit() here */
    	if ((test < 1) || (test > 9))
    	{
    		cerr << "Invalid input.\n";
    		exit(EXIT_FAILURE);
    	}
    
    	/* Do some other neat program type stuff here. */
    
    	return 0;
    }
    Now you run the program, but don't enter a number. Instead, you enter the character 'd'. Is there a reason that test now contains the value 0? I thought you could use int/char fairly interchangeably like within C. What I mean is, in C, if you tried to store 'd' in an integer, it would store the value 100, in the integer identifier (assuming the charset to be ASCII).

    It might sound pointless to ask as to why it does this, but let's say the program was altered slightly, and the if statement was changed to:

    Code:
    if ((test < 0) || (test > 5))
    If someone entered 'd' into this program, the if condition would be false, and so it wouldn't exit with the error handling code. Again, I know you could use isdigit() here, but this just seems very strange to me.

    Haven't slept in 2 days now though, so apologies if I'm not making sense.

    Thanks.

    John.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    There is no standard way (that I can think of) to do this.

    Here a small example:
    Code:
    int test;
    string dummy;
    
    cout << "Enter a number: ";
    
    while(!(cin >> test))
    {
       cin.clear(0);
       cin >> dummy;
       cout << dummy << " is not an integer\n";
    }
    This example however does not work correct on input like:
    123abc
    or 12 34 abc

    The best way is like you already mentioned: read the complete line and check all characters with the isdigit function.

  3. #3
    What about using taking the input as a string and use sprintf to convert it to what you want?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Thanks for the replies guys.

    As I say, it was more out of curiosity as to why it does it, but yeah, sprintf() would do the trick if at any time I couldn't use isdigit() (can't think of a particular instance off the top of my head). Nice to know there's options though.

    Thanks again.

    John.

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I'm not sure how you can do this with the sprintf function, but you can also use the scanf function:
    Code:
    int main(void)
    {
       int test = 0;
       cout << "Enter a number\n";
    
       while(scanf("%d", &test) != 1)
          while(getchar() != '\n'); /* flush input */
    
       cout << test;
    }
    Still this code doesn't work correct on all input (123ABC).

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >> Is there a reason that test now contains the value 0?
    Your compiler probably initializes new local variables to 0 automatically. If you give cin an int and then it reads a char it fails and doesn't put anything in the variable you give it, that's why it has 0 when you get to the test :-)
    *Cela*

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Ahhh, that would explain things. Still, I thought it was quite nice the way chars/ints could interact. Was quite handy at some points too. Oh well, such is life. ;]

    Thanks all.

    John.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    They still interact, and do so in the same way, it's just you have to lie differently. cin >> (int)j is basically the same as scanf("%d",&j); scanf also hangs on the user entering a 'd', it's just scanf returned a nice friendly count of how many things it actually read, and with cin you have to test things like cin.bad() or cin.fail(). cin also automagically picks the correct % for your type. If you want a single digit you could do something hackish like
    int test;
    cin >> (char)test;
    if(test<'0' || test > '9') fail();
    else test -= '0';

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Ah, nice one grib. I must confess, I don't know the methods of cin very intimately (I'm still new to the C/C++ languages). Thanks for pointing those out. I'll have a play around with them in a bit.

    John.

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. Validate a user input integer?
    By criticalerror in forum C++ Programming
    Replies: 20
    Last Post: 12-07-2003, 08:30 PM
  4. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM