Thread: get a character in Set

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    get a character in Set

    HI! I was wondering if anyone knew how to code a function with getchar in set . where someone enters a character and it reads a single character and then uppercases it and then prints the integer value of the character

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    Yes, easy, give it a go.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    What? I dont even know where to start

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    where do i start
    Quote Originally Posted by Salem View Post
    Yes, easy, give it a go.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    One step at a time.

    int ch;
    ch = getchar();
    printf("%d\n",ch);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    im confused

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    Ok, full program then.
    Code:
    #include <stdio.h>
    int main ( ) {
      int ch;
      ch = getchar();
      printf("%d\n",ch); 
      return 0;
    }
    Press something like 'a' and then 'enter'.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    I was given
    Char *prompt
    Int N
    Char *L

    Thats where Im confused

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    I suppose you could have
    char *prompt = "Type something > ";
    printf("%s",prompt);


    But as for meaningless single letter identifiers like N and L, you need to ask whoever set the assignment.

    None of it seems relevant to the question you asked.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    550
    Quote Originally Posted by bre0006 View Post
    HI! I was wondering if anyone knew how to code a function with getchar in set . where someone enters a character and it reads a single character and then uppercases it and then prints the integer value of the character
    Consider this code. It is a little bit more elaborated than Salem's (but it does the exact same thing!).

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void )
    {
      int c;
    
      c = getchar();
    
      // Consider the possibility of calling this program as:
      //
      // $ ./test << EOF
      //   EOF
      //
      if ( c == EOF )
      {
        fputs( "End of stream.\n", stderr );
        return EXIT_FAILURE;
      }
    
      printf( "%1$c -> %1$d\n", toupper( c ) );
    
      return EXIT_SUCCESS;
    }
    getchar() returns an int and, sometimes, if the input stream (stdin) has no chars, it will return -1 (or EOF). Otherwise, you use toupper() macro to change character sensitiveness before printing... %1$c and %1$d are extensions of glibc printf to use only one extra argument on printf()...

    But, this code fails if your charset isn't ISO-8859-1 or WINDOWS-1252, if you aren't using an ASCII char or if your system allows for UNICODE and you enter a special char (like an emoticon, for instance)... Using "wide chars" won't help if your charset is UTF-8 (for example). In that case you can try to use glibc iconv() or the external library libiconv to convert from UTF-8 to wchar_t (Transliteration not always works!)...

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    Need help getting started on this function

    So I have an assignment where I need to create a function for getCharInSet.
    I was given this explanation.
    /*
    * This function prompts the user with the string pointed to by prompt
    * and then reads a single character from the keyboard, and throws the
    * rest of the input line away. It then checks if the input character is a
    * lower case letter, in which case it upcases it. It then checks if
    * the resulting character is one of the N approved characters given
    * in the N-length character array pointed to by L. If the character
    * is not allowed, the routine prints a list of allowed characters, and
    * then repeats the whole process until an allowed character is entered
    */


    I need help with how to get started. I have been stuck and dont even know where to begin.

    Thanks!

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,534
    Perhaps if you'd posted your actual requirements at the start, we could have saved a day of dribbling information.

    > I have been stuck and dont even know where to begin.
    I gave you a start, but you're just a rabbit stuck in the headlights of the whole problem.

    Stop staring at the whole thing and panicking, and focus on doing small things one at a time, and build up step by step.

    * This function prompts the user with the string pointed to by prompt
    * and then reads a single character from the keyboard, and throws the
    * rest of the input line away.
    It then checks if the input character is a
    * lower case letter, in which case it upcases it
    . It then checks if
    * the resulting character is one of the N approved characters given
    * in the N-length character array pointed to by L
    . If the character
    * is not allowed, the routine prints a list of allowed characters, and
    * then repeats the whole process until an allowed character is entered

    Given your other posts, it looks like you need
    Code:
    int getCharInSet ( char *prompt, int N, char *L ) {
        return 'a';
    }
    
    int main ( ) {
        char allowed[] = "abc";
        int ch = getCharInSet("Type a char",3,allowed);
        printf("You entered %c\n", ch);
    }
    The object is to take each coloured requirement IN TURN and add it to the program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from an opened file character by character
    By SneakySnake in forum C Programming
    Replies: 1
    Last Post: 12-07-2012, 01:05 PM
  2. Replies: 10
    Last Post: 07-05-2011, 08:21 PM
  3. multi-character character constant error
    By robwhit in forum C Programming
    Replies: 3
    Last Post: 07-15-2007, 12:12 AM
  4. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  5. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM

Tags for this Thread