Один из циклов избыточен.То есть цикл for
не имеет смысла в контексте цикла while
.
Примите во внимание, что вы просите пользователя ввести 'Y'
или 'N'
в приглашении, но сравнитепеременная answer
с символом 'y'
.
Я мог бы предложить следующее решение.
#include <stdio.h>
#include <ctype.h>
int main( void )
{
enum { N = 99 };
int numbers1[N];
int n = 0;
for ( char answer = 'Y'; n < N && answer == 'Y'; n++ )
{
printf("Insert a number: ");
scanf("%d", &numbers1[n]);
printf("Would you like to add more numbers? [Y/N] ");
if ( scanf( " %c", &answer ) != 1 ) answer = 'N';
answer = toupper( ( unsigned char )answer );
}
printf( "\nNumbers are following: " );
for ( int i = 0; i < n; i++ )
{
if ( i != 0 ) printf( ", " );
printf( "%d", numbers1[i] );
}
putchar( '\n' );
}
Вывод программы может выглядеть как
Insert a number: 0
Would you like to add more numbers? [Y/N] y
Insert a number: 1
Would you like to add more numbers? [Y/N] 1
Insert a number: 2
Would you like to add more numbers? [Y/N] y
Insert a number: 3
Would you like to add more numbers? [Y/N] y
Insert a number: 4
Would you like to add more numbers? [Y/N] y
Insert a number: 5
Would you like to add more numbers? [Y/N] y
Insert a number: 6
Would you like to add more numbers? [Y/N] y
Insert a number: 7
Would you like to add more numbers? [Y/N] y
Insert a number: 8
Would you like to add more numbers? [Y/N] y
Insert a number: 9
Would you like to add more numbers? [Y/N] n
Numbers are following: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9