data types

This is a discussion on data types within the C Programming forums, part of the General Programming Boards category; Hello, thanks for looking at my thread. Is there a data type which can be either a number or letter. ...

  1. #1
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67

    data types

    Hello, thanks for looking at my thread. Is there a data type which can be either a number or letter. So you can input a character or number and it will treat then as the same and still carry out the function only with a number or letter?

    Code:
    #include<stdio.h> 
    int main(void)
    {
     
     
       int array[100], search, c, n, count = 0;
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%d",&array[c]);
     
       printf("Enter the number to search\n");
       scanf("%d",&search);
     
       for ( c = 0 ; c < n ; c++ )
       {
          if ( array[c] == search )    
          {
             printf("%d is present at location %d.\n", search, c+1);
        count++;
          }
       }
       if ( count == 0 )
          printf("%d is not present in array.\n", search);
       else
          printf("%d is present %d times in array.\n", search, count);
       if ( count == 2)
          printf("One Pair"); 
       if ( count == 3)
          printf("Three of a kind");
       if ( count == 4)
          printf( "Four of a kind");    
          
             
          
          
       
          
       return 0;
       
       
       
       
       
       
       
    }
    Here is the code just you get the general idea. You input how many number you want and which number you want to search for. It then tells you how many times that number appears. But as said earlier I was wondering if it was possible to do this with either numbers or letters.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    What you might want is a pointer to void.
    Code:
    void* aSequenceOfBytes
    This is a pointer to just some sequence of bytes. The computer has no idea what this bytes represent. It's your responsibility.
    In order to copy raw data you use memcpy.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thanks for the reply, I have not yet used or come across the function memcpy. Is there another way in which it would be possible, perhaps one that I may have used before or easy to learn.

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    There is not a recommended way in my mind.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  5. #5
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Would this work, make a statement which says that if count = 0 for all five, so count == count2 == count3 == count4 == count5, then from there adapt the linear search to search for up to five characters? so it will always search for numbers first then character?

    Also would there be a way to loop is using count++ etc.
    Thanks

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    983
    Quote Originally Posted by SDH View Post
    Would this work, make a statement which says that if count = 0 for all five, so count == count2 == count3 == count4 == count5, then from there adapt the linear search to search for up to five characters? so it will always search for numbers first then character?

    Also would there be a way to loop is using count++ etc.
    Thanks
    It sounds like you want `count' to be an array. For example, if the user enters 10 items, then you want an array int count[10]

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Quote Originally Posted by SDH View Post
    Would this work, make a statement which says that if count = 0 for all five, so count == count2 == count3 == count4 == count5, then from there adapt the linear search to search for up to five characters? so it will always search for numbers first then character?

    Also would there be a way to loop is using count++ etc.
    Thanks
    Doesn't sound like a good idea to me.
    If you think that is really a good idea (thus I am wrong, which is of course a possibility), transform your idea into code and post it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Quote Originally Posted by c99tutorial View Post
    It sounds like you want `count' to be an array. For example, if the user enters 10 items, then you want an array int count[10]
    Yes it should be an array, but I am looking for it be an array of either a letter or number.

    std10093 - I will have a look at doing the code and post it on here if it succesfull.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    983
    Quote Originally Posted by SDH View Post
    Yes it should be an array, but I am looking for it be an array of either a letter or number.
    Probably the easiest way is to make an array of strings. If the string contains "123" then it is a number. If the string contains "a" for example, then it is a letter. This is sometimes called duck-typing.

    Code:
    int main(void)
    {
        char buf[5][BUFSIZ];
        strcpy(buf[0], "123");
        strcpy(buf[1], "a");
        strcpy(buf[2], "-12738");
        strcpy(buf[3], "G");
        strcpy(buf[4], "");
        
        for (int i=0; i < 5; i++) {
            int num;
            char c;
            if (sscanf(buf[i], " %d ", &num) == 1)
                printf("buf[%d] contains an integer: %d\n", i, num);
            else if (sscanf(buf[i], " %1c ", &c) == 1)
                printf("buf[%d] contains a letter: '%c'\n", i, c);
            else
                printf("buf[%d] contains a value of unknown type\n", i);
        }
    }

  10. #10
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    I think I am getting somewhere with my initial code and why it is not working.

    I believe it has something to do with, for ( c = 0 ; c < n ; c++ ), because this requires c to be number right? but then it also needs c to be an letter so it put the letters into the array correct?

    Code:
    #include<stdio.h> void main(void)
    {
     
     
       int n, count = 0, array[100], c;
       char search;
     
       printf("Enter the number of elements in array\n");
       scanf("%d",&n);
     
       printf("Enter %d numbers\n", n);
     
       for ( c = 0 ; c < n ; c++ )
          scanf("%c ",&array[c]);
     
       printf("Enter the number to search\n");
       scanf("%c",&search);
     
       for ( c = 0 ; c < n ; c++ )
       {
          if ( array[c] == search )    
          {
             printf("%c is present at location %d.\n", search, c+1);
        count++;
          }
       }
       if ( count == 0 )
          printf("%c is not present in array.\n", search);
       else
          printf("%c is present %d times in array.\n", search, count);
    
    
       
          
       return 0;
       
       
     }

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    What you need is to have the array as char type and not int

    Also mind that because you are reading characters, you need to leave a space in scanf, so that newline characters is bypasses.
    You have
    Code:
    scanf("%c",...);
    I would suggest you to have it like this
    Code:
    scanf(" %c",...);
    Mind that this is not happening with %d, because it automatically eats such characters.

    PS - Obviously, I hadn't understood your idea, sorry
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  12. #12
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Second thought.

    You need strcmp to compare strings.

    But now I see what you want to do.

    You can't do that. The easiest way that comes to my mind is to have them as strings and compare characters (ascii values)
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  13. #13
    SDH
    SDH is offline
    Registered User
    Join Date
    Dec 2012
    Posts
    67
    Thanks for your help std10093, I have managed to get my program to do what I want it to do. Thanks all.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    983
    Quote Originally Posted by SDH View Post
    I believe it has something to do with, for ( c = 0 ; c < n ; c++ ), because this requires c to be number right? but then it also needs c to be an letter so it put the letters into the array correct?
    In C there is technically no difference between a letter and an integer. Consider this example:

    Code:
    int x = 97;
    int y = 'a';
    x contains the integer 97 and y contains the 'a'. But in fact the letter a is just another integer. The exact integer it is depends on your character set, so if you're very unlucky 'a' will have exactly the value 97 and you will not be able to distinguish whether 97 is the integer 97 or whether it is the letter 'a'.

    Here's another solution: if you store an integer, add the value CHAR_MAX to it to distinguish it from valid character values.

    Code:
    if (sscanf(buf, "%d", &number) == 1)
        x = number + CHAR_MAX;
    else if (sscanf(buf, "%c", &character) == 1)
        x = character;
    ... Now x is unambiguous. Remember to subtract CHAR_MAX from it before you use it in calculations.

    Code:
    if (x >= CHAR_MAX)
        printf("x is an integer: %d\n", x - CHAR_MAX);
    else
        printf("x is a character '%c'\n", x);
    Notice this is only valid for non-negative integers.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Quote Originally Posted by c99tutorial View Post
    Notice this is only valid for non-negative integers.
    Notice also that this makes such a non-clean code (at least in my eyes)

    Glad that I helped
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

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

Similar Threads

  1. data types
    By britto116 in forum C Programming
    Replies: 12
    Last Post: 11-13-2009, 06:32 AM
  2. Data Types
    By kas2002 in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 07:30 AM
  3. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  4. data types
    By §elfi~ in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2003, 10:23 PM
  5. data types
    By Draco in forum C Programming
    Replies: 7
    Last Post: 06-05-2002, 03:26 AM

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