Thread: is this a design flaw

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    is this a design flaw

    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.

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    here's the header

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    i think it is, if anyone cares. i made this modifaction:

    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;
    	}
    }
    what's another way to solve it?

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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.

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    it exits fine without, i'm using bcc55. maybe they have a different library

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    Code:
    void c_stack::mf_peek(void)
    {
      if (back!=0)
      {
        cout<<endl<<data[back]<<endl;
      }
      else
      {
        cout<<endl<<"stack is empty"<<endl;
      }
    }
    to
    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;
      }
    }
    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:
    cout<<endl<<data[( back >= 9 )?back-1:back]<<endl;
    Also, to be able to use exit() safely you must include stdlib.h or cstdlib, your author seems to have forgotten that.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design flaw in C++?
    By @nthony in forum C++ Programming
    Replies: 24
    Last Post: 10-20-2008, 07:47 PM
  2. Implementing Inheritence into your design
    By bobthebullet990 in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2006, 04:40 PM
  3. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  4. linked list of templates (or a design flaw?)
    By Ess in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2002, 08:15 PM