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
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
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.
What? I dont even know where to start
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.
im confused
Ok, full program then.
Press something like 'a' and then 'enter'.Code:#include <stdio.h> int main ( ) { int ch; ch = getchar(); printf("%d\n",ch); return 0; }
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.
I was given
Char *prompt
Int N
Char *L
Thats where I’m confused
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.
Consider this code. It is a little bit more elaborated than Salem's (but it does the exact same thing!).
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()...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; }
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!)...
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!
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
The object is to take each coloured requirement IN TURN and add it to the program.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); }
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.