trouble printing

This is a discussion on trouble printing within the C Programming forums, part of the General Programming Boards category; i want to print all the rows of my array and for whatever reason im having some difficulty, probably a ...

  1. #1
    dPmunky
    Guest

    trouble printing

    i want to print all the rows of my array and for whatever reason im having some difficulty, probably a quick fix but i cant find it

    Code:
    #include <stdio.h>
    
    #define length 1024
    
    
    int Capitalize(int ch);
    void UserInput(char string[]);
    char NEW_2D_array(char* str);
    
    /* the main function prints a  message to the user, the user inputs a string
    ** other functions are called to make sure that the desired information is found
    ** and ultimately printed neatly in a table like this...
    **
    
    Total Words: 6
    
    Word Count	Word			Character Count
    -------------------------------------------------------
    1			SEA					3
    2			TO					2
    3			SHINING				7
    4			C					1
    5			HELLO				5
    6			WORLD				5
    --------------------------------------------------*/
    char TWO_D_array[1024][100];
    int wordcount, charcount;
    int r, c, i;
    
    
    int main()
    {
    	char str[length];
    	int i, ch;
    	ch = 0;
    
    	
    	printf("Enter a string of characters\n");        /* this is where the user */
    	printf("End input by pressing <CTRL-Z>\n\n");	 /* will input their string */
    	printf("Enter string: ");
    	UserInput(str);
    
    	/* this part goes through every letter of the */
    	/* array and checks to see if the letter is upper case */
    	for (i = 0; str[i] != '\0'; i++)	
    	{									
    		str[i] = (char) Capitalize(str[i]);
    	}
    
    	NEW_2D_array(str);
    
    	printf("Word Count           Word          Character Count\n");
    	printf("--------------------------------------------------\n");
    
    	while (TWO_D_array[r][c] != '\0')
    	{
    		printf("%-21d%s%10d\n", wordcount, TWO_D_array, charcount );
    	}
    
    	return 0;
    }
    
    char NEW_2D_array(char* str)
    {
    	r = c = i = 0;
    	charcount = 0;
    	wordcount = 0;
    
    	for (i = 0; str[i] != '\0'; i++)
    	{
    		TWO_D_array[r][c] = str[i];
    
    		if ((str[i] == ' ') || (str[i] == '\n'))
    		{
    			r++;
    			c = 0;
    		}
    
    		else
    		{
    			c++;
    		}
    	}
    	return (TWO_D_array[r][c]);
    }
    
    
    /* this function capitalizes letters that are lower case by using math */
    int Capitalize(int ch)
    {
    	if (( 'a' <= ch) && (ch <= 'z' ))
    	{
    		return (ch + 'A' - 'a');
    	}
    
    	else
    	{
    		return ch;
    	}
    }
    
    
    /* takes input from user by cycling through every letter in the string up until
    ** the EOF marker, when the marker is hit, the last element in the array is set 
    ** as the NULL character */
    void UserInput(char string[])
    {
    	int ch;
    	int i = 0;
    	
    	while ( (ch = getchar()) != EOF )
    	{		
    		if (ch != '\n')
    		{
    			string[i++] = (char) ch;
    		}
    	}
    
    	string[i] = '\0';
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /* takes input from user by cycling through every letter in the string up until
    ** the EOF marker, when the marker is hit, the last element in the array is set 
    ** as the NULL character */
    void UserInput(char string[])
    {
    	int ch;
    	int i = 0;
    	
    	while ( (ch = getchar()) != EOF )
    	{		
    		if (ch != '\n')
    		{
    			string[i++] = (char) ch;
    		}
    	}
    
    	string[i] = '\0';
    
    }
    You really should just use fgets. It'll save you loads of work. The above code can be summarized in the following:

    fgets( str, length, stdin );

    Vola! See how easy that was?

    If you do want to use your own code, here's a few thoughts:

    1) There is no need to typecast 'ch'.
    2) You shouldn't use any variables or functions that start with 'str', they're reserved words.
    3) Your function has a potential buffer overflow, since it merrily keeps adding to the string until you hit CTRL+Z. An example of such would be me redirecting stdin to read from a file, and having that file contain no EOF or EOL markers until it was well past the size of your buffer. (IE: My file is more than 1K long with no carrage returns.)
    4) There's no need to use your own 'Capitalize' function, when the ANSI standard provides one for you to use:

    #include <ctype.h>
    toupper( x )
    tolower( x )

    5) What's the point in having a return value on 'NEW_2D_Array'? You don't use it, so just make it void.

    6) What exactly are you stuck on printing?
    Code:
    for( x = 0; x < 100; x++ )
    {
        ...do some test to stop early...
    
        ...print stuff...
        printf("%03d\t%30s\t%03\n",
            x+1, /* the line number */
            myArray[x], /* array index */
            strlen( myArray[x] ), /*letter count of said index */
    }
    That should get you through. Read up on printf if you have any questions as to what I've done.

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

  3. #3
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    i want to place the single dimensional array into the 2dimensional array so that i can print it out like this. the reason im doing my own code is because thats part of my assignment i had to write my own functions for pretty much everything....i hope that was useful
    Code:
    Total Words: 6
    
    Word Count	Word		Character Count
    -------------------------------------------------------
    1		SEA		3		
    2		TO		2		
    3		SHINING		7		
    4		C		1		
    5		HELLO		5		
    6               WORLD		5		
    --------------------------------------------------*/
    Last edited by dP munky; 11-15-2002 at 10:50 PM.
    guns dont kill people, abortion clinics kill people.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i want to place the single dimensional array into the 2dimensional array so that i can print it out like this.
    But you don't need a 2-d array, as shown by Quzah. Don't over engineer something just because you feel you *need* to.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    dont worry about it i figured it out

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by dP munky
    dont worry about it i figured it out
    What's to figure out? I gave you the single line of code you need to solve your problem.

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

  7. #7
    cereal killer dP munky's Avatar
    Join Date
    Nov 2002
    Posts
    655
    well then i guess i did it the hard way
    guns dont kill people, abortion clinics kill people.

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, 02:13 AM
  2. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 06:29 PM
  3. trouble printing linked list
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 12-02-2008, 05:33 PM
  4. Having trouble printing from an array
    By Sir Andus in forum C Programming
    Replies: 2
    Last Post: 10-30-2006, 01:48 PM
  5. Trouble Printing ASCII Character
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2002, 09:04 PM

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