작성
·
172
1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void count();
int main()
{
char c = 0;
int number = 0;
while (1)
{
printf("Enter the letter of your choice: \n");
printf("a. avengers b. beep\n");
printf("c. count q. quit\n");
if (getchar(c) == 'a')
{
printf("Avengers assemble!\n");
}
else if (getchar(c) == 'b')
{
printf("\a");
}
else if (getchar(c) == 'c')
{
count();
}
else if (getchar(c) == 'q')
{
exit(1);
}
else
{
printf("wrong choice, choose try again!\n");
}
}
return 0;
}
void count()
{
int number;
printf("Enter an integer :");
scanf("%d", &number);
for (int i = 1; i <=number; i++)
{
printf("%d\n", i);
}
return 0;
}
답변 1
0
안녕하세요, 아래와 같이 개행문자를 처리하는 코드가 없어서 그렇습니다.
질문자님이 문자를 입력할 때 개행문자까지 입력되어서 발생한 문제이지요.
char get_first_char(void) {
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
질문자님 코드에 위 로직까지 추가한 코드는 다음과 같습니다.
이를 돌려보세요..!
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void count();
char get_first_char(void) {
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
int main()
{
char c = 0;
int number = 0;
while (1)
{
printf("Enter the letter of your choice: \n");
printf("a. avengers b. beep\n");
printf("c. count q. quit\n");
char input = get_first_char();
if (input == 'a')
{
printf("Avengers assemble!\n");
}
else if (input == 'b')
{
printf("\a");
}
else if (input == 'c')
{
count();
}
else if (input == 'q')
{
exit(1);
}
else
{
printf("wrong choice, choose try again!\n");
}
}
return 0;
}
void count()
{
int number;
printf("Enter an integer :");
scanf("%d", &number);
for (int i = 1; i <= number; i++)
{
printf("%d\n", i);
}
return 0;
}
감사합니다.
헐랭,,, 개행문자가 문제였네여 ㅠㅠ 계속 고민하고 있었습니다 ㅠ 덕분에 해결됐어요 !! 감사합니다 ㅎㅎ