首页 > 解决方案 > 有人可以告诉我为什么 void 功能不起作用吗?为什么它停在主要功能而不完成其余部分?

问题描述

代码有什么问题?当代码运行时,它只执行代码的第一部分,忽略 void 函数。我做错了什么,缺少什么?

#include <stdio.h>
void Fun2_xxx_F3();
void F3();
int main()
{
int Function1_60897_F3,M,A=6,B=8,items=1,counter=0,d; //decleration
float price,discount,discounta,total_discount;
  
   
printf("Function1_xxx_F3 is created by student ID = xxx- section F3"); //print student name and id

  if (A>B) //decide the value of M
  {
   M=A;
  }
  else if(A<B)
  {
   M=B;
  }

  while(items<=M)//Calculate the sum of 10% discount items
  {
   printf("\n\nEnter the price of item %d: ",items);
   scanf("%f",&price);
   
  if(price<1100)
   {
   discounta=price-price*10/100;
   printf("\nThe price of the item after discount = %.3f KD",discounta);
   total_discount+=discounta;
   counter++;
   }
      
  if(price>=1100 && price<=2200)
  {
   discount=price-price*15/100;
   printf("\nThe price of the item after discount = %.3f KD",discount);
  }
      
  if(price>2200)
  {
   discount=price-price*20/100;
   printf("\nThe price of the item after discount = %.3f KD",discount);
  }
   
items++;
}
printf("\n\nThe total price after discount of all items that got a discount of 10 percent is: %.3f KD",total_discount);
  

void Fun2_xxx_F3() //define void function
{
int M;
unsigned int computer=1,counter=0,counter2=0;                              //decleration 
float computer_price,computer_tax,avg1,avg2,computer_tax3=0,computer_tax5=0;
M=30;
char S1[60897]= "XXX YYY section:F3 ID xxx";// define a local string variable named S1 with size equal to 60897

printf("M is equal to the sum of my ID-number:xxx which is: %d",M);
printf("\n\nFunction (Fun2_xxx_F3) is created by student: %s\n\n\n",S1);

  while(computer<=30) //calculate the average price of all computers with and without tax.
  {
   printf("\nEnter the price of 30 computers: ");
   scanf("%f",&computer_price);


  if (computer_price> 1100)
  {
   computer_tax= computer_price+computer_price*3/100;
   computer_tax3+=computer_tax;
   counter++;
  }
  else if(computer_price<= 1100)
  {
   computer_tax= computer_price+computer_price*5/100;
   computer_tax5+=computer_tax;
   counter2++;
  }
   computer++;
  }
  
avg1=computer_tax3/counter; //calculate the average price after tax of all computers which get a tax of 3% 
avg2=(computer_tax3+computer_tax5)/30;
printf("\n\n%.3f KD ... %.3f KD",avg1, avg2);
}

void F3() //void function 2
{
int i=6;
unsigned int N;
  for(N=1 ; N<=i ; N+=1)//print on the screen your ID, your name and your section i
times
  {
   printf("ID:xxx   Name:XXX YYY   SECTION:F3");
  }

}
}

还有什么意思:
根据以下内容编写一个主函数:
• 主函数的主体中没有局部变量。
• 使用您的ID 值调用第一个函数Function1_42357_F2 作为实际参数。
• 调用函数 Fun2 和 F3

标签: c

解决方案


尝试了解函数在 C 编程语言中是如何工作的。在这种情况下,主函数可以写成如下:

#include <stdio.h>

int    main(void)
{
    Function1_42357_F2(your_id_here);    
    Fun2_xxx_F3();
    F3();
    return (0);
}

您需要相应地调整代码以使其正常工作。


推荐阅读