C语言停车场程序栈损坏报错及循环逻辑异常问题咨询
Perdana停车管理系统代码问题修复
已知问题
- 录入第1条车辆信息后无法重新输入车辆类型,对应输入逻辑未按预期循环执行
- 代码修改后触发运行时错误:
Run-Time Check Failure #2 - Stack around the variable 'type' was corrupted - 目标功能为实现支持15个停车位的停车计费全流程管理
原始问题代码
float rate(char, int, int); int main() { int a, b, v, x; float r; char type; printf("\t**\n"); printf("\t! Welcome to Perdana Parking Management System !\n"); printf("\t__\n"); printf("\n\tEnter the type of your vehicle (C for car, V for Van) : "); scanf("%c", &type); for (x = 0; x < 15; x++) { if (type == 'c' || type == 'C' || type == 'v' || type == 'v') { printf("\n\tEnter vehicle number : "); scanf("%d", &v); printf("\n"); printf("\tEnter the Hours Vehicle entered the lot ( from 0 to 24): "); scanf("%d", &a); printf("\t\n"); printf("\tEnter the Hours vehicle left the parking lot ( from 0 to 24): "); scanf("%d", &b); printf("\t\n"); r = rate(type, a, b); printf("\tYour Parking Fee is RM %.2f \n", r); } else { printf("\tYou have to Enter the type of your vehicle correctly\n"); return; } } return 0; } float rate(char t, int x, int y) { float rate = 0; switch (t) { case 'C': case 'c':if (y - x <= 1) { rate = 4; break; } else if (y - x > 1 && y - x <= 3) { rate = ((((y - x) - 1) * 2) + 4); break; } else if (y - x > 3 && y - x <= 24) { rate = (2 * 2) + (4 * 1) + (((y - x) - 3) * 1); break; } case 'V': case'v':if (y - x <= 1) { rate = 6; break; } else if (y - x > 1 && y - x <= 3) { rate = ((((y - x) - 1) * 3) + 6); break; } else if (y - x > 3 && y - x <= 24) { rate = (3 * 2) + (6 * 1) + (((y - x) - 3) * 2); break; } default: break; } return rate; }
问题根因
- 输入位置错误:车辆类型输入语句写在
for循环外部,程序启动后仅执行1次,15次循环过程中不会重新触发车辆类型输入,直接导致多车录入流程失效 - 栈损坏诱因:
else分支中直接写return;,但main函数定义返回值为int类型,无返回值直接退出会破坏栈帧结构;同时scanf读取字符时未清空输入缓冲区残留的换行符,会出现非预期内存读写,触发栈损坏报错 - 逻辑笔误:车辆类型判断条件重复写了两次
type == 'v',漏掉了大写'V'的判断;rate函数中C类车24小时内计费分支未写break,会穿透执行V类车的计费逻辑,导致费用计算错误
修复后完整代码
#include <stdio.h> // 清空输入缓冲区辅助逻辑 #define CLEAR_BUFFER() while(getchar() != '\n') float rate(char, int, int); int main() { int a, b, v, x; float r; char type; printf("\t**\n"); printf("\t! Welcome to Perdana Parking Management System !\n"); printf("\t__\n"); // 15个停车位对应循环15次,车辆类型输入移入循环内部 for (x = 0; x < 15; x++) { printf("\n\tEnter the type of your vehicle (C for car, V for Van) : "); scanf("%c", &type); CLEAR_BUFFER(); // 清空缓冲区残留换行,避免影响后续输入 // 修正类型判断笔误,覆盖大小写C/V if (type == 'c' || type == 'C' || type == 'v' || type == 'V') { printf("\n\tEnter vehicle number : "); scanf("%d", &v); CLEAR_BUFFER(); printf("\n"); printf("\tEnter the Hours Vehicle entered the lot ( from 0 to 24): "); scanf("%d", &a); CLEAR_BUFFER(); printf("\t\n"); printf("\tEnter the Hours vehicle left the parking lot ( from 0 to 24): "); scanf("%d", &b); CLEAR_BUFFER(); printf("\t\n"); r = rate(type, a, b); printf("\tYour Parking Fee is RM %.2f \n", r); } else { printf("\tYou have to Enter the type of your vehicle correctly\n"); // 错误输入不直接退出,跳过当前车位继续下一轮输入 continue; } } return 0; } float rate(char t, int x, int y) { float rate = 0; int park_duration = y - x; switch (t) { case 'C': case 'c': if (park_duration <= 1) { rate = 4; } else if (park_duration > 1 && park_duration <= 3) { rate = (((park_duration - 1) * 2) + 4); } else if (park_duration > 3 && park_duration <= 24) { rate = (2 * 2) + (4 * 1) + ((park_duration - 3) * 1); } break; // 补全break,防止分支穿透 case 'V': case 'v': if (park_duration <= 1) { rate = 6; } else if (park_duration > 1 && park_duration <= 3) { rate = (((park_duration - 1) * 3) + 6); } else if (park_duration > 3 && park_duration <= 24) { rate = (3 * 2) + (6 * 1) + ((park_duration - 3) * 2); } break; default: break; } return rate; }
修复说明
- 车辆类型输入移入for循环内部,每处理一个车位都会触发一次类型输入,满足15个车位逐车录入的需求
- 增加输入缓冲区清空逻辑,避免残留换行符导致的字符读取异常
- 修正无返回值return的错误写法,输入错误时跳过当前轮次继续循环,不会异常退出也不会破坏栈结构
- 修正类型判断、switch分支穿透的笔误,保证计费逻辑正确
内容的提问来源于stack exchange,提问作者Nurin
相关产品推荐
相关产品推荐

