Thread: ascii value?

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

    ascii value?

    How do you put in a printf: the asci values of what i typed in...?
    if i type "aaaaa" it comes out: 65 65 65 65.


    Code:
    void main( void )
    {
    	char Letter, sentence[50] ;
    	int counter1, counter2;
    	
    	printf("Type a sentence and press enter to stop:\n");
    	for ( counter1 = 0; counter1 < 50 && ( Letter = getche() ) != '\r'; counter1++ )
    	{
    		sentence[ Teller1 ] = Letter;
    	}
                     sentence[ counter1 ] ='\0'
    
    	printf("\nYou typed in the following sentence: \n");
    	for ( counter2 = 0; counter2 < counter1; counter2++ )
    	{
    		printf("%s", sentence);
                                    printf????????? ASCIIII??????
    	}
    
    	printf("\npress enter to quit...");
    	while ( getch() != '\n' );
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       static const char text[] = "ABC123";
       int i;
       puts(text);
       for ( i = 0; text[i] != '\0'; ++i )
       {
          printf("%d ", text[i]);
       }
       putchar('\n');
       return 0;
    }
    
    /* my output
    ABC123
    65 66 67 49 50 51
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    void main( void )
    {
        ...
    }
    Main should always return an int.

    Code:
    char Letter, sentence[50];
    int counter1, counter2;
    	
    printf("Type a sentence and press enter to stop:\n");
    for ( counter1 = 0; counter1 < 50 && ( Letter = getche() ) != '\r'; counter1++ )
    {
        sentence[ Teller1 ] = Letter;
    }
    sentence[ counter1 ] ='\0'
    What is Teller1? The getche() function is not standard. That whole for loop is the reason a function like fgets exists.

    Code:
    printf("\nYou typed in the following sentence: \n");
    for ( counter2 = 0; counter2 < counter1; counter2++ )
    {
        printf("%s", sentence);
        printf????????? ASCIIII??????
    }
    Using the %s format specifier would print the entire sentence beginning to end, you would not need a loop unless it was your goal to print the same thing over and over again. Printing the ASCII value would however require a loop which you've already been shown how to do.

    Code:
    printf("\npress enter to quit...");
    while ( getch() != '\n' );
    Like getche(), the getch() function is also not standard.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASCII character with ASCII value 0 and 32
    By hitesh_best in forum C Programming
    Replies: 4
    Last Post: 07-24-2007, 09:45 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Office access in C/C++ NOT VC++!! :)
    By skawky in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2005, 01:43 PM
  4. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM
  5. Checking ascii values of char input
    By yank in forum C Programming
    Replies: 2
    Last Post: 04-29-2003, 07:49 AM