hey (prelude, most likely ;) ) all, if you have a sec can you check the attached. i pulled them nearly verbatium from a book. i don't know if i'm missing something or the book messed up. thanks in advance.
Printable View
hey (prelude, most likely ;) ) all, if you have a sec can you check the attached. i pulled them nearly verbatium from a book. i don't know if i'm missing something or the book messed up. thanks in advance.
here's the header
i think it is, if anyone cares. i made this modifaction:
what's another way to solve it?Code:void c_stack::mf_peek(void)
{
if (back!=0)
{
cout<<endl<<"this is your character: "<<data[back-1]<<endl; //[back-1] is the change
}
else
{
cout<<endl<<"stack is empty"<<endl;
}
}
I just looked over it really quick (I hit the hay soon). I noticed you make a call to exit(). You have to include stdlib.h or cstdlib to be able to use that.
it exits fine without, i'm using bcc55. maybe they have a different library
Yep, that sure is an error. Most likely caused by a lack of error handling so as to keep the code small for the book. I personally would have added more checking, that out of bounds value is ugly ;)
You can fix the problem with a simple test before printing.
toCode:void c_stack::mf_peek(void)
{
if (back!=0)
{
cout<<endl<<data[back]<<endl;
}
else
{
cout<<endl<<"stack is empty"<<endl;
}
}
Though technically, you could keep the size the same by sacrificing readability in the test using the ternary operator in cout, which I don't recommend unless you document it very well.Code:void c_stack::mf_peek(void)
{
if (back!=0)
{
if ( back >= 9 )
cout<<endl<<data[back-1]<<endl;
else
cout<<endl<<data[back]<<endl;
}
else
{
cout<<endl<<"stack is empty"<<endl;
}
}
Also, to be able to use exit() safely you must include stdlib.h or cstdlib, your author seems to have forgotten that.Code:cout<<endl<<data[( back >= 9 )?back-1:back]<<endl;
-Prelude