data types

This is a discussion on data types within the C Programming forums, part of the General Programming Boards category; I'd like to have the user get a number character then turn it into an integer value. I tried things ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    data types

    I'd like to have the user get a number character then turn it into an integer value. I tried things like atoi(getch()), but it hasn't worked for me. I've only been able to use the ascii keypress values for the number keys. Could someone tell me how else I can do it?

  2. #2
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    Munich, Germany
    Posts
    224
    char no=getch();
    int a = no - 49; or int a = no - '0';

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    212
    Code:
    int num;
    printf("Enter Number: ");
    scanf(" %d", &num);
    printf(" %d is character: %c\n",num);
    Warning add a test for non-printable characters. Check an ASCII table for this.

  4. #4
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Code:
    char str[4];
    int num;
    
    sprintf(str, "%d", getch());
    num = atoi(str);
    #include <stdlib.h>

    int atoi(
    const char *nptr);

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    shaik786, your example won't work too well, I'm afraid. The contents of the str array is unknown, and you only place 1 character in it with getch(). This means the rest of the array could be anything, including other valid numbers.

    Then, when you call atoi(), it will convert the whole array, not just the first byte.

    Use something like this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i;
    
    	if (scanf("%d", &i) != 1)
    	{
    		printf ("Bad number!\n");
    		return 1;
    	}
    	
    	printf ("i is %d\n", i);
    	
    	return 0;
    }
    OR

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	int i;
    	char buffer[1024];
    
    	if (fgets(buffer, 1024, stdin) != NULL)
    	{
    		printf ("Number is %d\n", atoi(buffer));
    	}
    	
    	return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Oh yes, that's right Hammer, I missed on that!
    You can use scanf() instead.

    Code:
    char str[255];
    int num;
    
    scanf("%s", str);
    num = atoi(str);
    Last edited by shaik786; 06-05-2002 at 03:11 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by shaik786
    Oh yes, that's right Hammer, I missed on that!
    You can use scanf() instead.

    Code:
    char str[255];
    int num;
    
    scanf("%s", str);
    num = atoi(str);
    But if you're going to do that, then you might as well read straight into the int variable, like in my first example.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Yes, it could be achieved with your first example Hammer, but my only idea was to show the use of atoi(), where it all began

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 04:28 PM
  2. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 07:51 PM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 07:26 PM

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