我正在尝试将整数列表转换为字符(整数由空格、换行符和制表符分隔)。输入以 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;
}
}