編集: getchar(); を追加する必要がありました。scanf("%i", &choice); の後 今では一度だけ尋ねます!
どうやら、2回出力する原因となっているケーススイッチです。ケーススイッチの外で関数を呼び出すと1が出力されますが、スイッチ内で呼び出すと2回出力されます
これは何が原因ですか?私は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);
}