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

C语言菜单驱动程序二次运行输出为0的问题排查

问题描述

我是C语言编程初学者,编写了一个包含数字反转、数字求和、首尾数字求和功能的菜单驱动程序。程序无语法错误,但首次运行功能正常,后续运行输出均为0。以下是我的代码、运行输出及预期结果,请问这是什么原因?

我的代码

#include<stdio.h>
void main()
{
  int num, temp, rev, rem, sum, i, choice, last, first, yes_or_no;
  printf("Enter the number: ");
  scanf("%d", &num);
  do
  {
      printf("\nMenu");
      printf("\n1.Reverse a number.\n2.Sum of digits of a number.\n3.Sum of first and last numbers.");
      printf("\nEnter your choice: ");
      scanf("%d", &choice);
      switch (choice)
      {
      case 1:
         //Reverse the given number
         temp=num;
         rev=0;
         while (num>0)
         {
           rem=num%10;
           rev=rev*10+rem;
           num=num/10;
         }
         printf("\nThe reverse of %d is %d", temp, rev);
         break;
      case 2:
         //Sum of digits of a number
         temp=num;
         sum=0;
         while (num>0)
         {
           rem=num%10;
           sum=sum+rem;
           num=num/10;
         }
         printf("\nThe sum of digits of %d is %d", temp, sum);
         break;
      case 3:
         //Sum of first and last number
         temp=num;
         last=num%10;
         while(num>=10)
         {
           num=num/10;
         }
         first=num;
         sum=first+last;
         printf("\nThe sum of first and last digit of %d is %d", temp, sum);
         break;
      default:
         printf("\nOption does not exist");
         break;
      }
   } 
   while (choice<=3);  
}

运行输出

Enter the number: 67

Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 2

The sum of digits of 67 is 13
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 3

The sum of first and last digit of 0 is 0
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 2

The sum of digits of 0 is 0
Menu
1.Reverse a number.
2.Sum of digits of a number.
3.Sum of first and last numbers.
Enter your choice: 1

The reverse of 0 is 0

预期结果

输入num=67时,数字反转返回76,数字求和返回13,首尾数字求和返回13。


问题原因与修复方案

问题根源

你在每个功能分支里都直接修改了num变量的值:

  • 反转数字时,通过num=num/10循环直到num变为0
  • 数字求和时,同样通过num=num/10把num变成0
  • 首尾求和时,循环num=num/10直到num变成个位数,之后num不再是原始输入的67

第一次操作后,num已经被修改为0(或首尾求和后的6),后续所有操作都是基于这个被修改后的num执行,自然输出全是0。

修复方法

保存原始输入的数字到一个独立变量,比如original_num,每次操作时用临时变量复制这个原始值,不修改原始值。修改后的代码如下:

#include<stdio.h>
int main() // 符合C标准的主函数写法
{
  int original_num, num, temp, rev, rem, sum, choice, last, first;
  printf("Enter the number: ");
  scanf("%d", &original_num);
  do
  {
      printf("\nMenu");
      printf("\n1.Reverse a number.\n2.Sum of digits of a number.\n3.Sum of first and last numbers.");
      printf("\nEnter your choice: ");
      scanf("%d", &choice);
      switch (choice)
      {
      case 1:
         temp = original_num;
         rev = 0;
         num = original_num; // 用临时变量操作,不修改原始值
         while (num > 0)
         {
           rem = num % 10;
           rev = rev * 10 + rem;
           num = num / 10;
         }
         printf("\nThe reverse of %d is %d", temp, rev);
         break;
      case 2:
         temp = original_num;
         sum = 0;
         num = original_num;
         while (num > 0)
         {
           rem = num % 10;
           sum = sum + rem;
           num = num / 10;
         }
         printf("\nThe sum of digits of %d is %d", temp, sum);
         break;
      case 3:
         temp = original_num;
         num = original_num;
         last = num % 10;
         while(num >= 10)
         {
           num = num / 10;
         }
         first = num;
         sum = first + last;
         printf("\nThe sum of first and last digit of %d is %d", temp, sum);
         break;
      default:
         printf("\nOption does not exist");
         break;
      }
   } 
   while (choice <= 3);
   return 0; // 对应int main()的返回值
}

额外优化建议

  • 使用int main()而非void main(),符合C语言标准规范
  • 移除未使用的变量i和yes_or_no,减少代码冗余

内容的提问来源于stack exchange,提问作者Sneha Mariam Mani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:42:42