以下是我用来从用户输入创建结构的一些代码:
// make a new branch from user input
branch usr_input_branch() {
char main_text[INPUT_MAX_SIZE];
char b1[INPUT_MAX_SIZE];
char b2[INPUT_MAX_SIZE];
char response[4];
do {
printf("Enter main text: ");
fgets(main_text,INPUT_MAX_SIZE,stdin);
if ((strlen(main_text)>0) && (main_text[strlen (main_text) - 1] == '\n'))
main_text[strlen (main_text) - 1] = '\0';
printf("Enter Option 1: ");
fgets(b1,INPUT_MAX_SIZE,stdin);
if ((strlen(b1)>0) && (b1[strlen (name) - 1] == '\n'))
name[strlen (name) - 1] = '\0';
printf("Enter Option 2: ");
fgets(b2,INPUT_MAX_SIZE,stdin);
if ((strlen(name)>0) && (name[strlen (name) - 1] == '\n'))
name[strlen (name) - 1] = '\0';
puts("Here is your branch:");
branch br = brCreate(b1,b2,main_text);
brPrint(br);
printf("Is this OK? [Y/n] ");
scanf("%s",response);
int doLoop = 1;
do {
if (tolower(response[0]) == 'y')
{
return br;
}
else if (tolower(response[0]) == 'n') {
doLoop = 0;
}
else {
puts("Invalid response.");
printf("Is this OK? [Y/n] ");
scanf("%s",response);
}
} while (doLoop);
} while (1);
// inaccessible code
error("Inaccessible code",0);
return brCreate("","","");
}
这是我想要的输出:
Enter main text: stuff1
Enter Option 1: stuff2
Enter Option 2: stuff3
Here is your branch:
stuff1
Option 1: stuff2
Option 2: stuff3
然而:这是发生的事情:
Enter main text: Enter Option 1: stuff
Enter Option 2: stuff1
Here is your branch:
Option 1: stuff
Option 2: stuff1
它似乎完全忽略了第一件事,并直接转向第二件事。我应该怎么做才能解决这个问题?
我正在使用gcc和c99以防万一。