Thread: Problem printing ASCII control characters

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    42

    Problem printing ASCII control characters

    Hello, I am trying to finish an assignment that reads input as a stream of characters and prints it as a decimal number followed by the ASCII code. If the code comes prior to the "space" it needs to print the control character notation.

    I am having problems printing some ot the codes for characters 8-20. Mainly 8 (BS), and 13 (CR). On 13 it prints 10 (LF).

    Any ideas where I'm going wrong ("pulling my few remaining hairs out!")?

    Here is my code:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
    	int ch;
    	int chcount = 0;  //character count
    
    	printf("Please enter your characters:  \n");
    	
    	while ((ch = getchar()) != EOF)
    	{
    		if (chcount >= 8){
    			printf("\n");
    			chcount = 0;
    		}	
    		
    		if (isprint(ch)){
    			printf("%d = %c, " , ch, ch);
    			chcount++;
    		}					
    		
    		else if(ch == 9){
    			chcount++;
    			printf("%d = Horizontal Tab, ", ch);
    		}
    		
    		else if(ch == 10){
    			chcount++;
    			printf("%d = Newline, ", ch);
    		}
    		
    		else if(ch == 13){
    			chcount++;
    			printf("%d = Carriage Return, ", ch);
    		}
    				
    		else if(iscntrl(ch)){
    			chcount++;
    			printf("%d = ^%c, ", ch, ch+64);
    		}
    
    		else if(isspace(ch)){
    			chcount++;
    			printf("%d = ^%c, ", ch, ch+64);
    		}
    	
    	}
    
    	printf("program has ended\n");
    	getchar();
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    I suspect you're on Windows because you have a getchar() at the end of the program. For some reason, Windows treats text files and binary files differently, which means that it does newline translation for text streams: in C, '\n' is newline, but CRLF is newline on Windows. Thus your getchar() might not actually read what's in a redirected file/typed at the keyboard, because it will have been translated. I suspect that's your ^H problem as well, although I'm not familiar enough with Windows to say for sure.

    C99 allows you to try to reopen standard input/output as binary with freopen(), but it's not guaranteed to work. You could also switch to using fopen() and force the user to give a filename, since you can tell fopen() to operate in binary mode. Or, perhaps, see if there is a non-standard function on your platform to force standard input not to do any sort of translation.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Thank you for the reply, yes I am using windows and microsoft visual studio. I am a newbie at this and just following instructions for the assignment. we have not worked with any C99 options.

    I did get this bit of information but I cannot figure out how to keep this from happening:

    "characters such as backspace which cannot be entered from the keyboard because the terminal program interprets them before handing them to your program as input"

    It seems like there should be a simpler soulution than what I've come up with ( which doesnt completly work anyway).

    TIA

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    So regarding backspace, you mean that you try to hit backspace and nothing shows up? Yes, that is expected and unavoidable, at least with standard C. You're generally going to get your input line-at-a-time when the user is typing, and any backspaces are handled by the terminal, not your program.

    It seems you want to respond immediately when a key is hit. This is not possible in standard C. There are system-specific functions that do this, but you'll have to a) consult your system's documentation (or check the FAQ) and b) find out what your instructor allows.

    Your instructor might expect you to use Windows-only functions, wittingly or not (unfortunately, many instructors don't have a good grasp of the language). You need to figure out what's allowed for your class.

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    I have modified my code using getch() to give me unechoed unbuffered input but I have now lost the ^z EOF function. I only find one paragraph in my book (C Primer Plus) on getch(). Can anyone tell me how to stop the program? Thanks!
    Code:
    printf("Please enter your characters:  \n");
    	
    	while ((ch = getch()) != EOF)
    		
    	{
    		if (chcount >= 8){
    			printf("\n");			
    			chcount = 0;
    		}	
    		
    		if (isprint(ch)){
    			printf("%d = %c, " , ch, ch);
    			chcount++;
    		}					
    		
    		else if(ch == '\t'){
    			printf("%d = \\t, ", ch);
    			chcount++;
    		}
    		
    		else if(ch == '\n'){
    			chcount++;
    			printf("%d = \\n, ", ch);
    		}
    
    		else if(ch == '\r'){
    			chcount++;
    			printf("%d = \\r, ", ch);
    		}
    				
    		else if(iscntrl(ch)){
    			chcount++;
    			printf("%d = ^%c, ", ch, ch+64);
    		}
    
    		else if(isspace(ch)){
    			chcount++;
    			printf("%d = ^%c, ", ch, ch+64);
    		}
    	
    	}
    
    	printf("program has ended\n");
    	getchar();
    
    	return 0;
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You could try....

    Code:
    while ((ch = getch()) != '\z')

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    You could try....

    Code:
    while ((ch = getch()) != '\z')
    Thanks Mr Tater. That will end the program on "z". I actually figured that out but I was trying to get it to work using the standard windows "^Z". When I use ^Z if prints out "26 = ^Z"

    Stumped newbie

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did you try it? '\z' is ^z .... or it should be.

    If that's not working just substitute the explicit number 26.

    You might also want to try using do {... } while() moving the exit condition to the end of the loop.

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    \z is not valid C. \ does not introduce a control character, but instead starts an escape sequence. Only a select number of letters may follow the backslash.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    yes the '\z', '^Z', or '26' do not work. Using 'z' or something like '#' will end the program just fine. Also if i wanted to use a switch statement how would I code that?
    TIA

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by d2rover View Post
    yes the '\z', '^Z', or '26' do not work. Using 'z' or something like '#' will end the program just fine. Also if i wanted to use a switch statement how would I code that?
    TIA
    Look at the DO... WHILE structure.... it would go in your while() statement at the bottom.

    And it's not '26' it's just 26. (i.e. a number not a character)

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cas View Post
    \z is not valid C. \ does not introduce a control character, but instead starts an escape sequence. Only a select number of letters may follow the backslash.
    Thanks... I found that out after posting. It's not standard C.

    Odly I seem to remember using it.... Oh well.

  13. #13
    Registered User
    Join Date
    Sep 2010
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    Look at the DO... WHILE structure.... it would go in your while() statement at the bottom.

    And it's not '26' it's just 26. (i.e. a number not a character)
    Thanks Tater, I thought I had tried every combination 26 worked fine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing from a Rich Edit control
    By JustMax in forum Windows Programming
    Replies: 10
    Last Post: 02-14-2009, 07:12 PM
  3. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  4. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 10:12 AM