-1

How do I get rid of this 0 in the output? I think the problem is with the function called printReceipt.

#include <iostream>
using namespace std;


struct Node
{
    string name;
    int price;
    Node *link;
};


// Insert Node with value new_name at the end of the linked list
// whose first node is head_ref
void appendName(Node** head_ref, string new_name)
{
    Node* newNode = new Node();
    Node *last = *head_ref;
    newNode->name = new_name;
    newNode->link = NULL;
    if (*head_ref == NULL)
    {
        *head_ref = newNode;
        return;
    }
    while (last->link != NULL)
    {
        last = last->link;
    }
    last->link = newNode;
    return;
}

// Insert Node with price new_price at the end of the linked list
// whose first node is head_ref
void appendPrice(Node** head_ref, int new_price)
{
    Node* newNode = new Node();
    Node *last = *head_ref;
    newNode->price = new_price;
    newNode->link = NULL;
    if (*head_ref == NULL)
    {
        *head_ref = newNode;
        return;
    }
    while (last->link != NULL)
    {
        last = last->link;
    }
    last->link = newNode;
    return;
}

// Print the linked list
void printReceipt(Node *node)
{
    while (node != NULL)
    {
        cout<<" "<<node->name<<" "<<node->price;
        node = node->link;
    }
    
}


int main()
{
    Node* r1 = NULL;

    appendName(&r1, "Item#1");
    appendPrice(&r1, 23);
    

    cout<<"Receipt\n";

    printReceipt(r1);
    return 0;
}

Program output:

Receipt
 Item#1 0 23

https://i.stack.imgur.com/FwGgL.png

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
Adel
  • 61
  • 5
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver May 03 '22 at 15:33
  • 1
    Calling `appendName` followed by `appendPrice` will add _two_ Nodes to your linked list. The first will have the specified name and an uninitialized price, the second will have a name of `""` and the specified price. So `printReceipt` is printing _two_ nodes: One with a name of `"Item#1"` and what seems to be a price of 0, and one with a name of `""` (which isn't showing up in the output since it's just a blank string) and a price of 23. – Nathan Pierson May 03 '22 at 15:42
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/995714) – phuclv May 03 '22 at 15:50

1 Answers1

0

The problem you are having is because you have 2 Nodes. One that has "Item#1" as the name property and 0 as price and another one that has no name set and 23 as the price. I went ahead and changed the output for you so you can see in more detail what's going on:

void printReceipt(Node *node)
{
    while (node != NULL)
    {
        printf("Address: %x\n", node);
        cout << "Name: " << node->name << endl;
        cout << "Price: " << node->price << endl;
        node = node->link;
    }
}

It shows us the Adress of the node and the contents:

Receipt
Address: c9eedeb0
Name: Item#1
Price: 0
Address: c9eedef0
Name:
Price: 23

As you can see the first address ends with b0 and the second one ends with f0.

The error is inside your appendPrice function. In there you first add the price to the new Node but then at the end put the new Node address in the link of the old node.

last->link = newNode;
return;
Sebito
  • 11
  • 2