Help with isprint function

This is a discussion on Help with isprint function within the C Programming forums, part of the General Programming Boards category; Hello- I haven't been able to find a good example to help with this: How would I apply the isprint ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    Question Help with isprint function

    Hello-
    I haven't been able to find a good example to help with this:

    How would I apply the isprint function to this so that it prints a space character for every non-printable ASCII character? Thank you so much in advance!!!

    #include <ctype.h>
    #include <stdio.h>

    Code:
    void ascii (void)
    
    {
    	
    	int counter;
    	char binarystring[9];
    
    
    
    	for (counter = 0; counter< 128; counter++)
    		
    	{
    	printf(  " %4c   %12i    %10X    %8o    %s\n",  counter, counter, counter, counter, charAsBinary(counter, binarystring));
    	
    		
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( !isprint( c ) )
        ...output a space...
    What exactly are you stuck on?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    Hello-
    Well, what I'm trying to do is print out a formatted ASCII character table. I need it to print a space when a nonprintable character comes up. From the examples I've seen, the isprint would be within the printf function. I was thinking something like....

    Code:
    printf(  (isprint ( '%4c') ? " %4c: " ")  %12i    %10X    %8o    %s\n",  counter, counter, counter
    Thanks for the response.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You pass one character to the function, not a whole bunch of crap.
    Code:
    printf( " %4c %12i %10X %8o %s\n",
        isprint( c ) ? whateveryouaretryingtopring : ' ',
        ...the rest of your arguments...
    );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    What am I not understanding here?
    I'm assuming that c is a varible -in my case 'counter'

    Code:
    void ascii (void)
    
    {
    	
    	int counter;
    	char binarystring[9];
    
    
    
    	for (counter = 0; counter< 128; counter++)
    		
    	{
    		
    		printf(" %4c   %12i    %10X    %8o    %s\n", isprint( counter ) ? counter : ' ', counter, counter, counter, counter, charAsBinary(counter, binarystring)); 
    }
    }
    I get nada save for 3 zeros, (null) and *crash*

    Thanks again

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's your function crashing then. Remove the portion that calls your 'charAsBinary', see if it runs. If it does, go fix your function. You need to learn how to form a bit of logic and troubleshooting, or programming isn't going to work out too well for you.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    I haven't been at this too long - especially not long enough to extract any meaning from the errors I'm getting.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    The function was working fine before I inserted the isprint in there...

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not saying you should automatically know everything. I'm saying you need to look at your output and be able to try and find out what's wrong. Look at this line:
    Code:
    printf("a b c d e", a b c d e )
    Now, you said you got:
    Code:
    a b c d (crash)
    Therefore, it's not hard to figure out that e is what's crashing your program, since the rest are turning out fine, and since e is a function call, where the rest are simple values.

    Furthermore, all you're giving us to work with is a single line of code. That's not a whole lot to go on. Either learn to How to ask questions the smart way, or you'll need to be able to troubleshoot your own code. Truth be told, you really need to learn both.

    It's not a slight on you, it's just the truth. You'll need to learn to troubleshoot your own code. You have figure out what your code is doing. There's no way around it. Also, if you're asking for help, you need to learn to ask in a way that people both want to help you, but have enough information so they can help you if they so choose.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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