-5

I do not know how to use the debugger and I think thad I do not have one So I am trying to create simply linked list in c++ Called Product this list will be used to store product... and I have already made a function that add a new node to the begining of the list and also made a void function called "Afficher1" that is supposed to show me the total number of products in the list and their total price EXCLUDING VAT and the total amount of VAT and finnaly TOTAL Including VAT but when I call the void in the main function it does not run it just finishesh the main execution with a return value !=0

and when I remove some operations that are inside the function such as:

double total_TVA=((total)*(temp->TVA))/(100.0); double TTC=total+total_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;
}
  • Think about what `temp` is after the `while (temp != NULL)` loop. – molbdnilo Nov 03 '18 at 14:07
  • It seems highly unlikely that `Afficher1()` is not being called or is not running. Have a look at it: can you imagine a way that it might run, yet give you the impression that it hadn't? – John Bollinger Nov 03 '18 at 14:09
  • 3
    Obtaining a debugger and learning to use it will be much to your advantage. You may even have one already, as most IDEs include one or include an interface to an external one, and the developer tools for most operating systems include at least one (*e.g.* `gdb`). – John Bollinger Nov 03 '18 at 14:11
  • 2
    _"I DO NOT KNOW HOW TO USE THE DEBUGGER AND I DO NOT HAVE ONE"_ That's a completely irrelevant statement. Every c++ toolchain comes with a debugger, and you obviously should learn how to use it. That's an essential skill every programmer needs to master. – πάντα ῥεῖ Nov 03 '18 at 15:08
  • 1
    "I DO NOT KNOW HOW TO USE THE DEBUGGER AND I DO NOT HAVE ONE" - Then you should *get* one and *learn how to use it*. And please, STOP SHOUTING. – Jesper Juhl Nov 03 '18 at 15:10
  • Thank you all for helping me, but I really don't know yet how to use a debugger otherwise I won't be posting my issue here. thank you again – Raedin Khaled Nov 03 '18 at 15:24

2 Answers2

2

In your main() function you declare a local variable named head:

int main(){
   Product *head=NULL;

... and later you set it to be non-NULL:

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

... however, your Afficher1() function has no knowledge of that local variable, instead it is looking at the global variable head that you declared at the top of your program:

Product *head=NULL;

... and that global variable is still NULL, hence the if (head != NULL) test at the top of Afficher1() fails and the function does nothing.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
0

You program has basic problems with the scope of the globally declared variable and locally declared variables of the same name overriding each other.

Also, you had logical error where you assigned 'head->next = head' this will create circular linked list.

And you function body enclosed under if condition (head != NULL) was not executed as head variable was NULL in the global scope.

I have added comments to the above code pointing the same and it now it works fine. See below:-

#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;
}
  • Hello, Thank you for your reply I think that the way I am adding the node is right because in your case you are adding the node to the end of the list while in my case I am adding it to begining also I tried your code and still the function is not showing anything when executing :/ – Raedin Khaled Nov 03 '18 at 14:28
  • I have edited to code. now it will print as expected. I had highlighted the problems in your code and forget to comment out locally declared `head` variable which overrides your globally declared `head` variable. – jatin shah Nov 04 '18 at 15:10