How can I display the space character?

This is a discussion on How can I display the space character? within the C Programming forums, part of the General Programming Boards category; Guys, I am a programming newbie so I need your help I have written a program that generates a grid ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    How can I display the space character?

    Guys, I am a programming newbie so I need your help
    I have written a program that generates a grid of 20 x20 random numbers. I need to insert 20 randomly distributed empty spaces into the grid.
    My problem is, I have used the following array to insert the spaces but when I run the program I dont get the spaces. I get the number 32 in place of the spaces.

    Code:
    n = 20;
    	for (i = 0; i < n; i++)
    	{
    		array1[rand()%ROWS][rand()%COLS] = ' ';
       
    	}
    	
    
    	for(r=0;r<ROWS;r++)
    	{
    		for(c=0;c<COLS;c++)
    		{
    			printf("%d", array1[r][c]);
    						
    		}
    		printf("\n");
    	}
    this is the bit of the code where spaces are meant to be generated

    I know 32 is the ASCII code for the space character, but thats not what I want.How can I display the actual character in the grid rather than the code?

    Thanks in advance guys, your help will be much appreciated.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    printf("%d", array1[r][c]);
    Well due to the way you are outputting each character, you will never get a space, because you are formatting as a decimal.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    I appreciate your quick response. Do you have any suggestions how I can achieve the desired result. I thought about the "%d" as well but due to my limited knowledge in C I got stuck. I even tried the "%c" but then again it turned all the numbers to characters.
    Any ideas to this please!

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    You need to output the data as you see fit. It might be bad practice to store numbers in an array with space characters where the numbers aren't represented by their character values (I doubt you follow this sentence, I barely do).

    You need to know what is in your array and treat it how you see fit. Throw something like:
    Code:
    if (arr[i] == 32) 
        printf("%c", arr[i]);
    else
        printf("%d", arr[i]);
    This is obviously a bad example, but I hope you see the point. You could expand that if statement to check for other things like tabs, letters, punctuation, etc.

    It is not good to mix data types inside of a single array because it creates situations like this. If you have different data types, try using a struct to store the data.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    Quote Originally Posted by carrotcake1029 View Post
    You need to output the data as you see fit. It might be bad practice to store numbers in an array with space characters where the numbers aren't represented by their character values (I doubt you follow this sentence, I barely do).

    You need to know what is in your array and treat it how you see fit. Throw something like:
    Code:
    if (arr[i] == 32) 
        printf("%c", arr[i]);
    else
        printf("%d", arr[i]);
    This is obviously a bad example, but I hope you see the point. You could expand that if statement to check for other things like tabs, letters, punctuation, etc.

    It is not good to mix data types inside of a single array because it creates situations like this. If you have different data types, try using a struct to store the data.
    Thanks very much! I tried what you said and it worked perfectly. I know like you said its not a good programming practice to mix data types like that, but for the mean time it will serve the purpose. I will come back to that later. Once again, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementing space allocated for a character array
    By nicoeschpiko in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2011, 05:36 PM
  2. Is space a character ?
    By freddyvorhees in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2008, 04:35 PM
  3. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 10:42 PM
  4. Converting space character in string to integer
    By boostage in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2006, 09:40 PM
  5. How do I tell what character is in a space?
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-27-2002, 11:11 PM

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