执行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
- Off-by-one Error in Loop: Your loop runs
i <= count, butcountis the number of students added. Since array indices start at 0, valid indices are0tocount-1. Accessingv[count](wheni == count) reads uninitialized memory, leading to crashes or garbage data. - Wrong Format Specifier for Age: You're using
%s(for strings) to printv[i].age, which is an integer. This mismatch causes undefined behavior—definitely a crash trigger! - Missing Header for
bool: The code usesboolbut doesn't include<stdbool.h>, which will cause compilation errors in standard C. - Non-Standard
main()Declaration:main()should return anintper 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 <= counttoi < countto only iterate over valid, initialized student entries. - Age Format Specifier: Swapped
%sto%din the age print line to match the integer type ofage. - Added
<stdbool.h>: Required to use thebooltype in standard C. - Standardized
main(): Changed toint main()and addedreturn 0;to comply with C standards. - Removed Unnecessary
&: For char arrays likename,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
相关产品推荐
相关产品推荐

