0

我正在尝试将整数列表转换为字符(整数由空格、换行符和制表符分隔)。输入以 EOF 结束。例如,输入;72 101 108 108 111 44​​ 32 119 111 114 108 100 33 输出 你好,世界!

#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;
    }
}
4

2 回答 2

1

了解 和 之间的区别scanfgetchar()尤其是当它们呈现相同的输入时。

非常非常仔细地阅读文档scanf——其中有很多内容。

通过编写自己的专门例程而不是调用scanf(). 复制 C 标准中的功能通常是不好的做法,但如果目标是帮助您学习,那也没关系。

于 2012-08-22T01:01:56.533 回答
0

固定例如

#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;
    }
}
于 2012-08-21T23:21:36.843 回答