메뉴만들기 이어서…(230916~)
어떻게든 만들었네요… 예제코드를 봅시다.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
char get_choice(void);
char get_frist_char(void);
int get_integer(void);
void count();
int main()
{
int user_choice;
while ((user_choice = get_choice()) != 'q')
{
switch (user_choice)
{
case 'a':
printf("Avengers assemble!\\n");
break;
case 'b':
printf("\\a");
break;
case 'c':
count();
break;
default:
printf("Error with %d. \\n", user_choice);
exit(1);
break;
}
}
return 0;
}
char get_choice(void)
{
printf("\\nA. Assemble\\t B. Beep\\nC. Count\\t Q. Quit\\n");
printf("Please input the alphabet : \\n");
char c;
c = get_frist_char();
while (c != 'a' && c != 'b' && c != 'c' && c != 'q')
{
printf("Plese input 'a' or 'b or 'c' or 'q'");
c = get_frist_char();
}
return c;
}
char get_frist_char(void)
{
char ch;
ch = getchar();
while (getchar() != '\\n')
continue;
return ch;
}
void count(void)
{
int a;
char c;
printf("Enter an integer :\\n");
while((scanf("%ld", &a)) != 1)
{
printf("your input - ");
while ((c = getchar()) != '\\n')
{
putchar(c);
}
printf(" - is not an integer. Plese try again \\n");
}
for(int i = 0; i <= a; i++)
{
printf("%d\\n", i);
}
while (getchar() != '\\n')
continue;
}
이번 과제에서 구현해야 하는 기능들은 다음과 같다.
프로세스를 실행하면 4가지의 선택을 안내하는 문장이 뜬다.
문자 하나를 입력받고, 해당 문자의 선택지를 실행한다.
2.1. 입력받은 문자를 검사하고 4가지 외의 문자가 입력되면 다시 입력받는다.
a를 실행하면 avengers assemble이라는 문장이 뜨고 1로 돌아간다.
b를 실행하면 beep음이 들리고 1로 돌아간다.
c를 실행하면 정수하나를 입력받는다.
3.1. 입력받은 수가 정수인지 **판별(검사)**한다. 아니라면 다시 입력받는다.
3.2. 입력받은 정수를 n이라고 할때, 1,2…n을 출력한다.
q를 실행하면 프로세스를 종료한다.
구현한 함수를 먼저 보면,
char get_choice(void);
char get_frist_char(void);
int get_integer(void);