Search code examples
c++linked-liststackdoubly-linked-list

The "linked-list" console application froze when something went wrong, but still wouldn't show errors, so I can't pinpoint the problem


For context, I've created a basic "Email Simulator" that you can use to send emails to yourself, read the emails, and delete the emails. I have to use a stack linked list-style approach in this method, that's the criteria.

So far I've found out that the issue is with the displayInbox function, but what exactly went wrong I don't know for sure, because from the knowledge I have it seems logical that it would work. However I am unable to be sure as it doesn't provide an error code or trigger the Local Windows Debugger, it just freezes.

To trigger the same error I had, just compose 2 emails and then check the inbox. It doesn't matter if you check inbox in between. I used to be able to do it but now I couldn't. I'm using VS2019 for reference.

I'm not very experienced so it might be very bloated, so its quite large. But I did my best to make a shortened one from more than 600 lines to around 280 lines but you can still make the error occur. And this is a pastebin link to the full program in case https://pastebin.com/Hp3uP0B9

#include <iostream>
#include <string>
#include <stdlib.h> 
#include <windows.h> 
using namespace std;

struct Email //just a basic struct
{   
    string data;
    
    int index{}; //every new email will have a different index, used to traverse and pinpoint the correct email.

    Email* prev{}, * next{}; //just previous and next nodes
};
Email* myTemplate() //used to initialise a struct (alternative of constructor)
{
    Email* temp = new Email;

    temp->data = "\0";

    temp->index = 0;
    temp->next = NULL;
    temp->prev = NULL;

    return temp;
};

struct UI //another basic struct, but using constructors
{
    UI* prev, * next; //previous and next nodes
    string text[2]; //stores the selected text and unselected text

    bool selected; //bool to know when to trigger selected text
    int menuFunc; //used to determine which function to call, can also be used to represent Email's index when passing in values

    UI()
    {
        prev = NULL;
        next = NULL;
        text[0] = "\0";
        text[1] = "\0";
        selected = false;
        menuFunc = -1;
    }
    UI(UI* myPrev, UI* myNext, int myMFunc, string myText)
    {
        prev = myPrev;
        next = myNext;
        menuFunc = myMFunc;
        text[0] = myText;
        text[1] = "<<" + text[0] + ">>";
        selected = false;
    }
};

int mainMenu()
{
    int temp;
    cout << "type 1 to compose email, type 2 to display inbox, type 99 to quit\n: ";
    cin >> temp;
    if (temp == 1||temp == 2||temp == 99)
    {
        return temp;
    }
    else
    {
        return mainMenu();
    }   
}

int getKeyInt() //just a function to get relevant key codes
{
    if (GetAsyncKeyState(VK_UP))
    {
        return VK_UP;
    }
    else if (GetAsyncKeyState(VK_DOWN))
    {
        return VK_DOWN;
    }
    else if (GetAsyncKeyState(VK_RETURN))
    {
        return VK_RETURN;
    }
    else if (GetAsyncKeyState(VK_ESCAPE))
    {
        return VK_ESCAPE;
    }
    else
    {
        return -1;
    }
}

void displayInbox(UI* myNavi, UI* myHead, UI* myTail, Email*& eNavi, Email*& eHead, Email*& eTail) //displays the inbox of the mail
{
    bool modified = false;
    bool tempRunFlag = true;
    int tempKeyPress;

    UI* refreshCache = myHead; // to log the navigator's location upon reprinting the screen, points to head before the loop begins

    myNavi = myHead; //points the navigator to the newest node in the list

    eNavi = eTail; //email navigator switches address to the newest node in the email linked list

    if (eNavi->data != "\0") //to check if newest mail has values, just in case
    {
        myNavi->menuFunc = eNavi->index; //assigns the email index to the "menuFunc" to be passed when calling deleteMail() and displayMail()
        myNavi->text[0] = eNavi->data;  //sets the UI text of navigator
        myNavi->text[1] = "<<" + myNavi->text[0] + ">>";
        myHead = myNavi;//setting myHead to point to navigator's current address
        myHead->selected = true; //this will trigger the selected text's output later
        refreshCache = myHead;// assigns refreshCache to myHead
        myTail = myHead; //if it's only 1 mail in the list, then this will prevent UI from traversing into NULL, if not, below will change the values

        if (eNavi->prev != NULL)//if email navigator has a earlier mail to point to, false if there's only 1 element in the list, which is already done above
        {
            while (eNavi != NULL)//if email navigator is not pointing to the earliest mail, or if theres only 1 element
            {
                if (eNavi != eHead) //double checking if email navigator hasn't reached the earliest mail yet, if it is, then no need to go even earlier
                {
                    eNavi = eNavi->prev; //email navigator approaches eHead by 1 node
                }
                myNavi->next = new UI(myNavi, NULL, eNavi->index, eNavi->data); //creates a new node to point to 
                myNavi = myNavi->next; // navigator switch to the next address that it created
                myTail = myNavi; //assigns tail to newly created UI navigator node
            }
        }
    }
    else //if newest mail has no values, or no mail
    {
        myHead = new UI(NULL, NULL, 0, "nothing to see here");
        myHead->selected = true;
        refreshCache = myHead;
        myTail = myHead;
    }

    while (tempRunFlag)
    {
        system("CLS");
        cout <<
            "\nNow browsing inbox..." << endl <<
            "____________________________________________________________________________________________________" << endl <<
            "MAIL DATA"<< endl;
        myNavi = myHead; //UI navigator will be reassigned to point to first UI
        while (myNavi != NULL)
        {
            cout << myNavi->text[(int)myNavi->selected] << endl;
            myNavi = myNavi->next;
        }
        myNavi = refreshCache;//similar implementation in the prompt() function;
        if (myNavi != NULL)
        {
            cout << "\nMenu Function code: " << myNavi->menuFunc << endl;//used for debugging, displays the menuFunc
        }
        cout <<
            "\n\nReached end of inbox" << endl <<
            "____________________________________________________________________________________________________" << endl <<
            "[Use ARROW KEYS UP & DOWN to navigate, ESC to return to Main Menu]" << endl;
        system("pause");

        tempKeyPress = getKeyInt();

        switch (tempKeyPress)
        {
        case VK_UP:
            if (myNavi != myHead)
            {
                myNavi->selected = false;
                myNavi = myNavi->prev;
                myNavi->selected = true;
                refreshCache = myNavi;
            }
            break;
        case VK_DOWN:
            if (myNavi != myTail)
            {
                myNavi->selected = false;
                myNavi = myNavi->next;
                myNavi->selected = true;
                refreshCache = myNavi;
            }
            break;
        case VK_ESCAPE:
            tempRunFlag = false;
            break;
        default:
            break;
        }
    }
    return;
}

void composeEmail(Email*& eHead, Email*& eTail) //prompting the user to get data
{
    Email* draft = new Email;
    system("CLS");
    if (eHead->data != "\0") ///used to get rid of a \n character in the buffer for no reason, I don't know why it's there, but whats weird is that it only happens once the email linked list already has 1 element inside of it
    {
        cin.ignore();
    }
    do
    {
        cout << "\nPlease type in data (16 character limit)" << endl; //just for no reason
        getline(cin, draft->data);
    } while (draft->data.length() > 16 || draft->data.length() <= 0);
    while (draft->data.length() < 16)
    {
        draft->data += " ";
    }
    cout << "\n\nEmail is sent! Please check inbox." << endl;

    draft->next = NULL;
    if (eHead->data == "\0") //if current head node is empty
    {
        draft->prev = NULL;
        draft->index = eTail->index + 1;
        eHead = draft;
        eTail = eHead;
    }
    else //if head node already has a value
    {
        draft->prev = eTail;
        draft->index = eTail->index + 1;
        eTail->next = draft;
        eTail = draft;
    }

    system("pause");
}

bool mainMenuExecute(int executionCode, UI* uiNaviIB, UI* uiHeadIB, UI* uiTailIB, Email*& eNavi, Email*& eHead, Email*& eTail)
{
    if (executionCode == 1)
    {
        composeEmail(eHead, eTail);
        return true;
    }
    else if (executionCode == 2)
    {
        displayInbox(uiNaviIB, uiHeadIB, uiTailIB, eNavi, eHead, eTail);
        return true;
    }
    else if (executionCode == 99)
    {
        return false;
    }
}

int main()
{
    //Inbox UI
    UI* uiHeadIB = new UI(NULL, NULL, 0, "nothing to see here"),
        * uiTailIB = uiHeadIB,
        * uiNaviIB = uiHeadIB;
    uiNaviIB->selected = true;

    //Actual mail stuff
    Email* mHead = myTemplate(),
        * mTail = mHead;
    Email* mNavi = mHead;

    bool simulate = true;

    while (simulate)
    {
        system("CLS");
        simulate = mainMenuExecute(mainMenu(), uiNaviIB, uiHeadIB, uiTailIB, mNavi, mHead, mTail);
    }
}


Hope someone can provide constructive criticism as I get to learn more, cheers.


Solution

  • I solved the issue, but there's another problem, however thats for another case, as to how I solved it, I put a else{break;} after if(eNavi ! = eHead), because it was causing the while loop to loop infinitely if it didn't break.