편집 : getchar();을 추가해야했습니다. scanf 이후 ("% i", & 선택); 지금은 한 번만 묻습니다!입력을 받아 while 루프에서 비교하면 fgets입니까?
외관상으로는 케이스 스위치가 출력을 두 번 출력합니다. 케이스 밖의 함수를 호출하면 1을 출력하지만, 스위치 내부에서 호출하면 스위치가 두 번 출력됩니다.
무엇이 원인입니까? scanf 선택을 의심합니까?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test();
void choose();
int main(void)
{
//if i call here its fine
test();
choose();
return 0;
}
void choose()
{
int choice;
do
{
printf("1 - Testing if double ask\n");
printf("2 - Exit\n");
printf("Please enter your choice: ");
scanf("%i", &choice);
switch(choice)
{//but pressing 1 here asks twice?
case 1:
test();
break;
default:
if(choice !=2)
printf("Input Not Recognized!\n");
break;
}
}
while(choice !=2);
if(choice == 2)
printf("Ciao!");
}
void test()
{
printf("HELLO");
char *name = malloc (256);
do
{
printf("Would you like to continue (y/n)\n");
fgets(name, 256, stdin);
}
while(strncmp(name, "n", 1) != 0);
free (name);
}
한 문자 또는 'strcmp()'에 작은 따옴표를 사용하십시오 (두 문자가 같으면 '0'이됩니다). – alex
fgets 다음에 이름을 출력하고이를 게시물에 추가하십시오. – MWB
@MWB ok, 그 게시 – Dog