Stack not displaying first letter of elements

Hey ya'll I'm trying to print out the elements in my stack and for some reaon the first letter of each stack element won't print.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void StackStory::displayStacks()
{
    StackNode *temp;
    temp = top;
    
    if(isEmpty())
    {
        cout<<"Stack is empty.\n";
    }
    else
    {
        while(temp != nullptr)
        {
            cout<< temp->value << endl;
            temp = temp->next;
        }
    }
}


Should be:

George
Bill
Joe


Prints:

eorge
oll
oe
Last edited on
Are you sure the data elements being stored actually contains the full string for each element? I'd investigate how you store the data in your stack as the possible culprit.
Had a cin.ignore() in my pushItem function, which I guess ignored the first character in the element of the stack. I t worked fine afrter removing the ignore.
By the way, you don't need to separate variable initialization from their declarations.
 
StackNode *temp = top;
1
2
3
4
5
6
7
8
void StackStory::displayStacks()
{
    if (isEmpty())
        cout << "Stack is empty.\n";
    else
        for (auto temp = top; temp != nullptr; temp = temp->next)
            cout << temp->value << endl;
}

Topic archived. No new replies allowed.