C++为什么尽管我调用了void函数,它却不起作用

C++ Why the void function is not working eventhough I called it?

本文关键字:函数 不起作用 void 为什么 管我 调用 C++      更新时间:2023-10-16

我不知道如何使用调试器,我想我没有调试器所以我尝试在c++中创建一个简单的链表Called Product此列表将用于存储产品。。。我已经做了一个函数,在列表的开头添加一个新节点还制作了一个名为"Afficher1"的无效函数,该函数应该向我显示列表中产品的总数及其总价(不包括增值税(以及增值税总额和最终总额,包括增值税但当我在主函数中调用void时,它并没有运行,它只是用一个返回值来完成主执行=0

当我删除功能内部的一些操作时,例如:

双total_TVA=((总计(*(温度->TVA((/(100.0(;双TTC=总计+总计_TVA;

#include<iostream>
#include<string>
using namespace std;
struct Product{
string code_prod;
string designation;
string UM;
double PUA_HT;
double QTE;
double TVA;
Product *next;
};
Product *head=NULL;

Product *Add_Product(Product* &head, string code, string des, string um, double pua, double qte, double tva){
Product *prod=new Product;
prod->code_prod=code;
prod->designation=des;
prod->UM=um;
prod->PUA_HT=pua;
prod->QTE=qte;
prod->TVA=tva;
prod->next=head;
head=prod;
return head;
}
void Afficher1(){
if(head != NULL){
Product *temp=head;
double total=0;
int i=0;
while(temp != NULL){
total=total + ((temp->PUA_HT)*(temp->QTE));
i++;
temp=temp->next;
}
double total_TVA=((total)*(temp->TVA))/(100.0);
double TTC=total+total_TVA;
cout<<"Nombre total des produits Achetes: "<<i<<endl;
cout<<"Le Montant Total HT: "<<total<<endl;
cout<<"Total TVA de "<<temp->TVA<<" : "<<total_TVA<<endl;
cout<<"Total TTC: "<<TTC<<endl;
}   

}
int main(){
Product *head=NULL;
string codes; string dess; string ums; double puas; double qtes; double tvas;
for(int i=0;i<1;i++){
cout<<"Donner les infos pour le proudit "<<i+1<<endl;
cin>>codes;
cin>>dess;
cin>>ums;
cin>>puas;
cin>>qtes;
cin>>tvas;
head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);
}
Afficher1();
return 0;
}

main()函数中,您声明了一个名为head:的局部变量

int main(){
Product *head=NULL;

稍后您将其设置为非NULL:

head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);

但是,您的Afficher1()函数不知道该局部变量,而是查看您在程序顶部声明的全局变量head

Product *head=NULL;

并且该全局变量仍然为NULL,因此Afficher1()顶部的if (head != NULL)测试失败,并且该函数什么也不做。

您的程序在全局声明变量和本地声明的同名变量的作用域相互覆盖方面存在基本问题。

此外,您在分配"head->next=head"时出现了逻辑错误,这将创建循环链表。

head变量在全局范围中为NULL时,if条件(head!=NULL(下的函数体未执行。

我在上面的代码中添加了相同的注释,现在它工作得很好。见下文:-

#include <iostream>
#include<string>
using namespace std;
struct Product{
string code_prod;
string designation;
string UM;
double PUA_HT;
double QTE;
double TVA;
Product *next;
};
/* This globally visible head pointer */
Product *head=NULL;

Product *Add_Product(Product* &head, string code, string des, string um, double pua, double qte, double tva){
Product *prod=new Product;
prod->code_prod=code;
prod->designation=des;
prod->UM=um;
prod->PUA_HT=pua;
prod->QTE=qte;
prod->TVA=tva;
prod->next=NULL; // You set it to head, will create circular linked list and your Afficher1 loop will run infinitely.
/* This is head pointer is pointing to GLOBAL head which is NULL */
head=prod;
return head;
}
void Afficher1(){
if(head != NULL){
Product *temp=head;
double total=0;
int i=0;
while(temp != NULL){
total=total + ((temp->PUA_HT)*(temp->QTE));
i++;
temp=temp->next;
}
double total_TVA=((total)*(temp->TVA))/(100.0);
double TTC=total+total_TVA;
cout<<"Nombre total des produits Achetes: "<<i<<endl;
cout<<"Le Montant Total HT: "<<total<<endl;
cout<<"Total TVA de "<<temp->TVA<<" : "<<total_TVA<<endl;
cout<<"Total TTC: "<<TTC<<endl;
}   

}
int main(){
/* This is locally declared and initialized head pointer which overrides global scope */
// Product *head=NULL;
string codes; string dess; string ums; double puas; double qtes; double tvas;
for(int i=0;i<1;i++){
cout<<"Donner les infos pour le proudit "<<i+1<<endl;
cin>>codes;
cin>>dess;
cin>>ums;
cin>>puas;
cin>>qtes;
cin>>tvas;
head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);
}
Afficher1();
return 0;
}