《Mathematics 101 using C》代码求助:退出选项逻辑异常问题
Hey there! Let's break down this quirky issue in your Mathematics 101 using C program. The fact that exiting from the test function loops you back to the initial menu (instead of closing the program) while exiting from the multiplication table works perfectly points to a mismatch in how you're handling the "quit" signal across your subfunctions.
What's Likely Going Wrong
Chances are, your program uses a main loop to keep the initial menu running, but the test function's post-action menu isn't properly telling that main loop to stop when you select option 3. Here's a common scenario:
- When you exit the multiplication table's post-menu, you set a flag (like
keep_running = 0) that terminates the main loop. - But in the test function's post-menu, selecting 3 only exits the test's local loop, leaving the main loop still active—so you bounce back to the initial menu.
Fixing the Logic
Let's fix this by standardizing how "exit" is handled across both functions. We'll use a shared flag to control the main loop, and update it whenever the user chooses to quit from any post-action menu.
Step 1: Define a Main Loop Control Flag
In your main() function, declare a variable to track if the program should keep running:
int main() { int keep_running = 1; // Controls the main menu loop int initial_choice; while (keep_running) { // Show initial menu printf("\n=== Mathematics 101 using C ===\n"); printf("1-乘法表\n"); printf("2-测试功能\n"); printf("请输入选择: "); scanf("%d", &initial_choice); switch (initial_choice) { case 1: multiplication_table(&keep_running); // Pass flag pointer break; case 2: test_function(&keep_running); // Pass flag pointer break; default: printf("无效选择,请重新输入!\n"); } } printf("程序已退出,再见!\n"); return 0; }
Step 2: Update the Test Function to Trigger Exit
Modify your test function to accept the keep_running pointer, and set it to 0 when the user selects option 3:
void test_function(int *keep_running) { // ... Your existing test logic here ... int post_choice; do { printf("\n--- 操作选择 ---\n"); printf("1-重新测试\n"); printf("2-重新生成乘法表\n"); printf("3-退出\n"); printf("请输入选择: "); scanf("%d", &post_choice); switch (post_choice) { case 1: // Re-run test logic (call your test execution function here) run_test_logic(); break; case 2: // Return to main loop, which will handle multiplication table selection return; case 3: // Tell main loop to terminate *keep_running = 0; break; default: printf("无效选择,请重新输入!\n"); } } while (post_choice != 3); }
Step 3: Align the Multiplication Table Function
Make sure your multiplication table function uses the same pattern to keep behavior consistent:
void multiplication_table(int *keep_running) { // ... Your existing multiplication table logic here ... int post_choice; do { printf("\n--- 操作选择 ---\n"); printf("1-重新生成乘法表\n"); printf("2-重新测试\n"); printf("3-退出\n"); printf("请输入选择: "); scanf("%d", &post_choice); switch (post_choice) { case 1: // Re-run multiplication table logic generate_multiplication_table(); break; case 2: // Return to main loop for test function selection return; case 3: *keep_running = 0; break; default: printf("无效选择,请重新输入!\n"); } } while (post_choice != 3); }
Why This Works
By passing the keep_running pointer to both subfunctions, we ensure that selecting "exit" (option 3) from any post-action menu will directly tell the main loop to stop running. This eliminates the inconsistency between how your two subfunctions handle termination.
内容的提问来源于stack exchange,提问作者user9419337

