user1612261
user1612261

Reputation: 57

Convert ascii codes(integers) to characters

I am trying to convert a list of integers to characters (integers are separated by white spaces, newlines and tabs). The input ends with EOF. For example, input; 72 101 108 108 111 44 32 119 111 114 108 100 33 output Hello, world!

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void )
{
    char ch;
        ch != '\n';
    //ch = getchar();
    //while( ch != '\n' )
        //ch = getchar();
}

main()
{
    char    ch;                     /* handles user input */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        //ch = getchar();
                scanf("%d",&ch)
        char_count = 0;
        while( (ch != '\n')  &&  (char_count < MAXBUFFERSIZE)) {
            buffer[char_count++] = ch;
            ch = getchar();
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\007Error: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}

Upvotes: 0

Views: 468

Answers (2)

Norman Ramsey
Norman Ramsey

Reputation: 202735

Learn the difference between scanf and getchar(), especially when they are presented with the same input.

Read the documentation for scanf very, very carefully—there's a lot going on.

You might learn the most by writing your own specialized routine instead of calling scanf(). It's usually poor practice to duplicate functionality that is in the C standard, but it's OK if the goal is to help you learn.

Upvotes: 1

BLUEPIXY
BLUEPIXY

Reputation: 40155

fixed e.g

#include <stdio.h>
#include <ctype.h>
#define MAXBUFFERSIZE   100

void cleartoendofline( void );  /* ANSI function prototype */

void cleartoendofline( void ){
    while('\n'!=getchar());
}

main(){
    char    ch;                     /* handles user input delimiter*/
    int     num;                    /* user input number */
    char    buffer[MAXBUFFERSIZE];  /* sufficient to handle one line */
    int     char_count;             /* number of characters read for this line */
    int     exit_flag = 0;
    int     valid_choice;

    while( exit_flag  == 0 ) {
        printf("Enter integer(s)\n: ");
        scanf(" %d%c", &num, &ch);//read integer and delimiter
        char_count = 0;
        while( char_count < MAXBUFFERSIZE - 1) {//-1 for End of string
            buffer[char_count++] = (char)num;
            if(ch == '\n')break;
            scanf("%d%c", &num, &ch);
        }
        buffer[char_count] = 0x00;      /* null terminate buffer */
        printf("\nIntegers translates to:\n");
        printf("%s\n", buffer);

        valid_choice = 0;
        while( valid_choice == 0 ) {
            printf("Continue (Y/N)?\n");
            scanf(" %c", &ch );
            ch = toupper( ch );
            if((ch == 'Y') || (ch == 'N') )
                valid_choice = 1;
            else
                printf("\aError: Invalid choice\n");
            cleartoendofline();
        }
        if( ch == 'N' ) exit_flag = 1;
    }
}

Upvotes: 0

Related Questions