-4

I dont know what is wrong I have tried changing and the error keeps on piling up.

I have a main.cpp which is this one.

#include <iostream.h>
#include "FloatList.h"

void main(void)
{
    FloatList List;
    list.appendNode(2.5);
    list.appendNode(7.9);
    list.appendNode(12.6);
}

and my header file is this one.

class FloatList
{
private:

    struct ListNode
    {
        float value;
        struct ListNode *next;
    }; 

    ListNode *head; 

public:

    FloatList()     
    { head = NULL; }
    ~FloatList(); 

    void appendNode(float num);
    void insertNode(float num);
    void deleteNode(float num);
    void displayList();

};

and my implementation file was this one

void FloatList::appendNode(float num)
{
    ListNode *newNode, *nodePtr;

    newNode = new ListNode;
    newNode->value = num;
    newNode->next = NULL;

    if (!head)
        head = newNode;
    else 
    {
        nodePtr = head;

        while (nodePtr->next)
            nodePtr = nodePtr->next;

        nodePtr->next = newNode;
    }
    cout << "Input has been APPENDED!" << endl;

}
JaMiT
  • 12,175
  • 4
  • 14
  • 27
  • Is this C or C++? Pick one. What is supposed to be? – Goswin von Brederlow May 29 '22 at 01:47
  • 2
    What and where is your actual error? Nothing springs out apart from the obvious that `head` isn't a `ListNode **`, you insert at the back of the list but don't track the tail and `ListNode` lacks a constructor. Probaly more inefficiencies in the code you didn't show. – Goswin von Brederlow May 29 '22 at 01:49
  • 1
    iostream.h is a sign of Turbo C++, a compiler dating back to the 1990s that is rarely used today. If you don't have to use it, I recommend [using something newer](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2). If you have to use it, do what you have to do to pass the class, but I recommend also practicing with something newer so that you don't have so bumpy a transition into the workforce. – user4581301 May 29 '22 at 01:53
  • 1
    Welcome to Stack Overflow. Please read [ask]. "I dont know what is wrong I have tried changing and the error keeps on piling up." What does this mean? We can only possibly explain errors that we can actually see; and we can only answer **one** question at a time. – Karl Knechtel May 29 '22 at 02:18

1 Answers1

0

Your question is not clear at all. Please read How to Ask.

You should have stated the problem of your code (the expected output and the output you get) to help us understand your problem.

Actually, your code is working but I think I know the problem. You are trying to view your Linked List items in this way

std::cout << List.head[0].value
<< List.head[1].value <<
List.head[2].value << std::endl;

And you are struggling with this output 2.5 0 7.9 while you are expecting 2.5 7.9 12.6.

This is not how you should index your linked list because the size of the class FloatList is not the same as the size of the float, its the double in your case (because the class includes a float and a pointer to a float), and by adding more member variables to the base class, its size gets bigger.

Increasing the index by 2 will give you the output you have expected or simply create a function that loops over the list.

Moe
  • 375
  • 1
  • 5
  • 18
  • *"Your question is not clear at all."* -- This is a sign that the question should not be answered until it is clarified. Please read [Answer]: "Not all questions can or should be answered here. Save yourself some frustration and avoid trying to answer questions which....are unclear or lacking specific details that can uniquely identify the problem." – JaMiT May 29 '22 at 04:23