You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行showStudents()函数时C语言程序崩溃问题求助

Hey there! Let's break down why your showStudents() function is crashing and fix those issues step by step:

Key Issues Causing the Crash & Incorrect Output

  1. Off-by-one Error in Loop: Your loop runs i <= count, but count is the number of students added. Since array indices start at 0, valid indices are 0 to count-1. Accessing v[count] (when i == count) reads uninitialized memory, leading to crashes or garbage data.
  2. Wrong Format Specifier for Age: You're using %s (for strings) to print v[i].age, which is an integer. This mismatch causes undefined behavior—definitely a crash trigger!
  3. Missing Header for bool: The code uses bool but doesn't include <stdbool.h>, which will cause compilation errors in standard C.
  4. Non-Standard main() Declaration: main() should return an int per C standards.

Fixed Code

#include <stdio.h>
#include <stdbool.h>  // Added for bool type

int count = 0; // Global count of students

struct student{
    char name[20];
    char surname[20];
    int age;
    char registrationDate[10];
} v[200];

void addStudents(){
    printf("\nJu po shtoni nje student te ri ne liste!\n");
    printf("\nShkruani emrin: ");
    scanf("%s", v[count].name); // Removed unnecessary & (array decays to pointer)
    printf("\nShkruani mbiemrin: ");
    scanf("%s", v[count].surname);
    printf("\nShkruani daten e regjistrimit: ");
    scanf("%s", v[count].registrationDate);
    printf("\nShkruani moshen: ");
    scanf("%d", &v[count].age);
    printf("\nRegjistrimi u ruajt me sukses.\n");
    count++;
}

void menu (){
    printf("\nShtyp 1 per te shtuar student.");
    printf("\nShtyp 2 per te shfaqur studentet.");
    printf("\nShtyp 3 per menune.");
    printf("\nShtyp 0 per te mbyllur programin.");
}

void showStudents(){
    if(count == 0){
        printf("\nNuk ka student te regjistruar!\n");
        return;
    }
    for(int i = 0; i < count; i++){ // Fixed loop condition to i < count
        printf("\nStudenti te %d:\n", i+1);
        printf("Emri: %s\n", v[i].name);
        printf("Mbiemri: %s\n", v[i].surname);
        printf("Data e regjistrimit: %s\n", v[i].registrationDate);
        printf("Mosha: %d\n", v[i].age); // Fixed format specifier to %d for int
    }
}

int main(){ // Standard int main()
    printf("*******************PROGRAM PER LIBRARI NE GJUHEN C***********************\n\n");
    menu();
    bool cond = true;
    while(cond){
        int choice;
        printf("\n\nShkruani zgjedhjen tuaj: ");
        scanf("%d", &choice);
        switch(choice){
            case 1:
                addStudents();
                break;
            case 2:
                showStudents();
                break;
            case 3:
                menu();
                break;
            case 0:
                cond = false;
                printf("\nProgrami u mbyll.\n");
                break;
            default:
                printf("\nZgjedhje e pavlefshme! Provoni perseri.\n");
        }
    }
    return 0; // Standard return statement
}

Breakdown of Fixes

  • Loop Condition: Changed i <= count to i < count to only iterate over valid, initialized student entries.
  • Age Format Specifier: Swapped %s to %d in the age print line to match the integer type of age.
  • Added <stdbool.h>: Required to use the bool type in standard C.
  • Standardized main(): Changed to int main() and added return 0; to comply with C standards.
  • Removed Unnecessary &: For char arrays like name, scanf("%s", v[count].name) works because arrays decay to pointers—& was redundant here.
  • Added Edge Case Handling: If no students are added, showStudents() now prints a friendly message instead of trying to loop over nothing.
  • Default Case in Switch: Added a default case to handle invalid input gracefully.

These changes should fix the crash and ensure all students are displayed correctly when you select option 2.

内容的提问来源于stack exchange,提问作者Shpend Palushi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:13:29