do ... while循环问题与双向链表[重复]

时间:2022-01-20 07:17:51

This question already has an answer here:

这个问题在这里已有答案:

So I've implemented this simple and pretty useless doubly linked list just for the sake of practice. It is a list of athletes and the sport they play. Each node is defined as follows:

所以我为了练习而实现了这个简单而无用的双链表。这是运动员和他们所运动的运动的清单。每个节点定义如下:

typedef struct node {
char* name;
char* sport;
struct node* prev;
struct node* next;
}node;

I've created the first node of the list in main (node* head is globally defined):

我在main中创建了列表的第一个节点(node * head是全局定义的):

head = malloc(sizeof(node));
if (head == NULL) {
    printf("malloc failed");
    return 1;
}
head->name = "Stephen Curry";
head->sport = "Basketball";
head->next = NULL;
head->prev = NULL;

This do while loop is intended to allow the user to add as many nodes as he/she wants to the list at the terminal:

这个while循环旨在允许用户在终端上添加他/她想要的节点:

char names[50]; // declaring the arrays wherein the names and sports will be stored temporarily
char sports[30];
char YorN; // this will store the result from the prompt
do {
    printf("name: ");
    fgets(names, 50, stdin);
    strtok(names, "\n"); // removing the "\n" that fgets adds so that name and     sport will be printed on the same line

    printf("sport: ");
    fgets(sports, 30, stdin);

    addNode(names, sports); // adds node to the head of the list
    printReverse(); // prints the list in reverse, giving the illusion that the user is adding to the tail

    printf("add a new name to the list? (Y or N): ");
    YorN = fgetc(stdin);
    YorN = toupper(YorN);
} while (YorN == 'Y');

It works fine for the first entry. output:

它适用于第一次进入。输出:

name: Reggie Miller
sport: Basketball
Stephen Curry,Basketball
Reggie Miller,Basketball
add a new name to the list? (Y or N):

After that if the user selects "Y" to add a new node, the terminal prints this:

之后,如果用户选择“Y”添加新节点,终端打印出:

name: sport:

which only allows the user to enter the sport. Then outputs this:

只允许用户进入运动。然后输出:

name: sport: k
Stephen Curry,Basketball

,k


,k

add a new name to the list? (Y or N):

where "k" is the sport entered. I don't think it's an issue with my addNode() or printReverse() functions so I've omitted posting those for the sake of brevity. However if someone thinks that it may be an issue with those functions or just would like to see them I'd be happy to post them. It just seems to me it is an issue with some aspect of the loop, maybe my implementation of fgets? When I tried scanf it failed on even the first attempt. Any help is much appreciated, thanks!

其中“k”是进入的运动。我不认为这是我的addNode()或printReverse()函数的问题所以我为了简洁起见省略了这些问题。但是,如果有人认为这可能是这些功能的问题,或者只是想看到它们,我会很乐意发布它们。在我看来这是循环的某些方面的问题,也许我的fgets的实现?当我尝试scanf时,即使是第一次尝试也失败了。非常感谢任何帮助,谢谢!

1 个解决方案

#1


3  

getc(stdin) leaves '\n' into stdin. So the second loop fgets exits immediately.

getc(stdin)将'\ n'留给stdin。所以第二个循环fgets立即退出。

You can perform a dummy call to fgetc(stdin); at end of loop.

您可以对fgetc(stdin)执行虚拟调用;在循环结束时。

Or you fgets to read out the "Y\n" input string.

或者你想读出“Y \ n”输入字符串。

char answer[3];
fgets(answer, sizeof(answer), stdin);
YorN = toupper(answer[0]);

#1


3  

getc(stdin) leaves '\n' into stdin. So the second loop fgets exits immediately.

getc(stdin)将'\ n'留给stdin。所以第二个循环fgets立即退出。

You can perform a dummy call to fgetc(stdin); at end of loop.

您可以对fgetc(stdin)执行虚拟调用;在循环结束时。

Or you fgets to read out the "Y\n" input string.

或者你想读出“Y \ n”输入字符串。

char answer[3];
fgets(answer, sizeof(answer), stdin);
YorN = toupper(answer[0]);