for loop仅打印最后一项(C)

时间:2016-02-22 00:18:32

标签: c arrays structure

您好我正在尝试编写一个基本上采用结构的C程序,将其存储在数组中,然后以这种格式打印该结构:

姓氏,名字\ n 等级:B

对于添加到数组中的每个项目,现在我遇到的问题只是正在打印的最后一个项目。我知道这是在for循环中发生的,因为代码正在执行和打印。我不会发布整个程序,因为它是很多代码,所以我只是把重要的东西。

void add(char* student_firstname, char* student_lastname, char* student_grade, char* student_level, struct student* list)
{

    int i;
    for (i = count-1; i < count; i++){
        if ((strcmp(list[i].lastName, student_lastname) != 0) && (strcmp(list[i].firstName, student_firstname) != 0)){
            strcpy(list[i].firstName, student_firstname);
            strcpy(list[i].lastName, student_lastname);
            strcpy(list[i].grade, student_grade);
        }
        else{
            printf("This student is already on the list!");
        }
        //count++;
    }
    printf("Student added!");
}

之前将count变量设置为0。 *不要担心student_grade

display()函数供参考:

void display()
{
    int i;
    for (i = count-1; i < count; i++){
        printf("%s, %s \n", list[i].lastName, list[i].firstName);
        printf("Grade: %s \n", list[i].grade);
    }
}

这里也是我的read()函数:

void read()
{
    char student_firstName[100];
    char student_lastName[100];
    char student_grade[30];
    char student_level[100];

    printf("\nEnter the student's first name:\n");
    fgets(student_firstName, sizeof(student_firstName), stdin);

    printf("\nEnter the student's last name:\n");
    fgets(student_lastName, sizeof(student_lastName), stdin);

    printf("\nEnter the student's grade (A+,A,A-,...):\n");
    fgets(student_grade, sizeof(student_grade), stdin);

    printf("\nEnter the student's education level (f/so/j/s):\n");
    fgets(student_level, sizeof(student_level), stdin);

    // discard '\n' chars attached to input; NOTE: If you are using GCC, you may need to comment out these 4 lines
    student_firstName[strlen(student_firstName) - 1] = '\0';
    student_lastName[strlen(student_lastName) - 1] = '\0';
    student_grade[strlen(student_grade) - 1] = '\0';
    student_level[strlen(student_level) - 1] = '\0';

    add(student_firstName, student_lastName, student_grade, student_level, list);
    printf("\n"); // newline for formatting
}

2 个答案:

答案 0 :(得分:2)

看看这一行:

for (i = count-1; i < count; i++)

在你的添加和显示功能中。这从count-1到count,这意味着它只在位置count-1处打印/添加一个项目。你应该摆脱add函数中的for循环,递增计数,并重写print函数从0到count。这应该可以解决您遇到的所有问题。

答案 1 :(得分:1)

您检查add中重复项的算法已被破坏。我需要看起来更像这样,但你也应该为缓冲区溢出添加错误检查和保护。

void add(char* student_firstname, char* student_lastname, char* student_grade, char* student_level, struct student* list)
{
    for (int i = 0; i < count; i++){
        if ((strcmp(list[i].lastName, student_lastname) == 0) &&      
            (strcmp(list[i].firstName, student_firstname) == 0)) {
            printf("This student is already on the list!");
            return;
        }
    }
    strcpy(list[count].firstName, student_firstname);
    strcpy(list[count].lastName, student_lastname);
    strcpy(list[count].grade, student_grade);
    count++;
    printf("Student added!");
}

首先,您需要添加保护count不会太大。

其次,您需要停止使用strcpy并使用strncpy,以避免缓冲区溢出错误。

第三,您可能希望避免使用线性扫描,并弄清楚如何使用某种索引,但对于可能不重要的学校项目。

类似于你的display函数,你应该调整for循环,如果你希望它打印出所有记录,或者如果它只打算打印最后一条记录就消除循环。