Thread: Need Help On an Assignment

  1. #181
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Yeah, I'm saying that there's something else. When I took out At, it still dumped after "Testing Overloaded <<:", but I'm not sure what the issue is.

  2. #182
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Erik Ingvoldsen View Post
    Yeah, I'm saying that there's something else.
    I'm saying that there isn't.

    When I took out At, it still dumped after "Testing Overloaded <<:", but I'm not sure what the issue is.
    I'm not impressed. Taking code out does not magically fix anything.

    If you would kindly read my earlier post, I have conclusively narrowed down the issue to line 99 which only doesn't work because you haven't implemented AddFront() and friends. I'm not saying that you should go and implement that too ... rather the takeaway is that your program works correctly up to line 99.

    YOu have to remember that you are implementing the class slowly, focusing on passing one test at a time. Of course there are going to be functions that you haven't implemented causing core dumps. Stop execution before functions that you haven't implemented are tested.

  3. #183
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    My apologies, I misunderstood you. I thought you were check to see if the problem as At or something else. I wasn't aware that the core could dump unless I did something wrong with one of the existing codes and that including At had, somehow or another, changed one problem into a different problem. With that said, it seems like it's going to be hard to tell if the core dumping is the fault of the code or not. Since At seems to be OK I have moved on to GetFirst:

    Code:
    std::string* DynamicStringArray::GetFirst() {
      if(currentsize == 0) {
        throw "Array Empty";
      }
      return myarray[0];
    }
    The code is dumping earlier than before...but then again it was dumping earlier with At than without, so I can't tell if this is OK or not:

    Code:
    ----------------------------------------
    UNIT TEST:
    ----------------------------------------
    
    *****Constructor*****
    PASSSED TEST 1 GetSize()!
    PASSSED TEST 2 GetCapacity()!
    PASSSED TEST 3 ToString()!
    PASSSED TEST 4 Empty()!
    PASSSED TEST 5 At(0) EXCEPTION HANDLING!
    Segmentation fault (core dumped)
    You mentioned using a debugger. Could you explain more on this, please? I haven't heard of that before.

  4. #184
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Honestly I would just move on to the next test. Your professor wants to test more exception handling. The next test was for GetFront().


    You mentioned using a debugger. Could you explain more on this, please? I haven't heard of that before.
    Your cygwin installation should have come with a gdb installation. At a prompt try
    $ gdb --version

    If you get output, that's your debugger. Try reading RMS's gdb Tutorial.

    Question 1.1 "How do I compile with debugging symbols?" and part 4 "How do I use breakpoints?" are of particular importance.

  5. #185
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I was about to ask why are you not throwing an exception of a class derived from std::exception, but then I saw in your instructor's test code:
    Code:
    try {
      list.At(0);
    } catch (const string &e) {
      Test(e == "Invalid Location", "At(0) EXCEPTION HANDLING");
    }
    I am appalled.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #186
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Yeah, you're right, doing the tests in order was definitely better. After doing AddFront, I saw there were apparently over 40 tests. x.x GetFirst(12) was next, but when I added it, the core dumped again (still at 5). Is this a problem with the code?

    Code:
    #include "dynamic_string_array.h"
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cctype>
    #include <sstream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::stringstream;
    
    DynamicStringArray::DynamicStringArray(int size) : arraysize(size), currentsize(0) {
      myarray = new std::string* [arraysize]();
    }
    
    DynamicStringArray::~DynamicStringArray() {
      delete [] myarray;
      currentsize = 0;
    }
    
    std::string* DynamicStringArray::At(int i) const {
      if ((i < currentsize) && (i >= 0)) {
        return myarray[i];
      }
      else {
        throw std::string("Invalid Location");
      }
    }
      
    std::string* DynamicStringArray::GetFirst() {
      if(currentsize == 0) {
        throw "Array Empty";
      }
      return myarray[0];
    }
    
    std::string* DynamicStringArray::GetLast() {
    
    }
    
    void DynamicStringArray::AddFront(std::string* str) {
      if(currentsize == arraysize) {
        IncreaseCapacity();
      }
      for (int i = currentsize; i > 0; i--) {
        myarray[i] = myarray[i - 1];
      }
      myarray[0] = str;
      currentsize++;
    }
     
    void DynamicStringArray::AddBack(std::string* string) {
    
    }
    
    void DynamicStringArray::DeleteFront() {
    
    }
    
    void DynamicStringArray::DeleteBack() {
    
    }
    
    unsigned int DynamicStringArray::GetSize() const
    {
      return currentsize;
    }
    
    unsigned int DynamicStringArray::GetCapacity() const
    {
      return arraysize;
    }
    
    bool DynamicStringArray::Empty() const
    {
      return currentsize == 0;
    }
    
    void DynamicStringArray::Clear() {
    
    }
     
    void DynamicStringArray::Sort() {
    
    }
    
    std::string DynamicStringArray::ToString() {
      std::ostringstream ss;
      if (this->Empty() == false) {
        ss << *myarray[0];
        for (unsigned int i = 1; i < currentsize; i++) {
          ss << ", " << *myarray[i];
        }
      }
      return ss.str();
    }
    
    std::ostream& operator<<(std::ostream& comma, const DynamicStringArray& DSA) {
    
    }
    
    void DynamicStringArray::IncreaseCapacity() {
    
    }
    
    std::string DynamicStringArray::ToUpper(std::string letter) {
    
    }

  7. #187
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Quote Originally Posted by laserlight View Post
    I was about to ask why are you not throwing an exception of a class derived from std::exception, but then I saw in your instructor's test code:
    Code:
    try {
      list.At(0);
    } catch (const string &e) {
      Test(e == "Invalid Location", "At(0) EXCEPTION HANDLING");
    }
    I am appalled.
    Why's that?

  8. #188
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by laserlight View Post
    I was about to ask why are you not throwing an exception of a class derived from std::exception, but then I saw in your instructor's test code:
    Code:
    try {
      list.At(0);
    } catch (const string &e) {
      Test(e == "Invalid Location", "At(0) EXCEPTION HANDLING");
    }
    I am appalled.
    Yeah the teacher is really good at forcing some strange implementation decisions, isn't he?

    Erik, please use std::exception when you aren't writing code for your teacher anymore.

    Why's that?
    Exceptions are caught by type. It's not specific, and that is why so many exceptions are their own type (std:ut_of_range, std::bad_alloc, etc.) Technically, a string exception would be caught by the first catch (string&) block the code runs into, which is not necessarily how you want to handle a specific error.

    Quote Originally Posted by Erik Ingvoldsen View Post
    Yeah, you're right, doing the tests in order was definitely better. After doing AddFront, I saw there were apparently over 40 tests. x.x GetFirst(12) was next, but when I added it, the core dumped again (still at 5). Is this a problem with the code?
    From what I can tell, the only real problem is you are still throwing "strings like this" - that isn't actually caught by your teacher's unit test. A "string like this" is a char array, not a string. You should prefer to throw string("whatever"); for the purposes of the assignment.
    Last edited by whiteflags; 04-06-2015 at 11:23 PM.

  9. #189
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Ohhh OK, I see the difference now. It worked for GetFront, but not for GetLast--or rather there's something else going on there. It dumps after 14 now.

    Code:
    #include "dynamic_string_array.h"
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cctype>
    #include <sstream>
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::stringstream;
    
    DynamicStringArray::DynamicStringArray(int size) : arraysize(size), currentsize(0) {
      myarray = new std::string* [arraysize]();
    }
    
    DynamicStringArray::~DynamicStringArray() {
      delete [] myarray;
      currentsize = 0;
    }
    
    std::string* DynamicStringArray::At(int i) const {
      if ((i < currentsize) && (i >= 0)) {
        return myarray[i];
      }
      else {
        throw std::string("Invalid Location");
      }
    }
      
    std::string* DynamicStringArray::GetFirst() {
      if(currentsize == 0) {
        throw std::string("Array Empty");
      }
      return myarray[0];
    }
    
    std::string* DynamicStringArray::GetLast() {
      int i = currentsize;
      if(currentsize == 0) {
        throw std::string("Array Empty");
      }
      return myarray[i];
    }
    
    void DynamicStringArray::AddFront(std::string* str) {
      if(currentsize == arraysize) {
        IncreaseCapacity();
      }
      for (int i = currentsize; i > 0; i--) {
        myarray[i] = myarray[i - 1];
      }
      myarray[0] = str;
      currentsize++;
    }
     
    void DynamicStringArray::AddBack(std::string* string) {
    
    }
    
    void DynamicStringArray::DeleteFront() {
    
    }
    
    void DynamicStringArray::DeleteBack() {
    
    }
    
    unsigned int DynamicStringArray::GetSize() const
    {
      return currentsize;
    }
    
    unsigned int DynamicStringArray::GetCapacity() const
    {
      return arraysize;
    }
    
    bool DynamicStringArray::Empty() const
    {
      return currentsize == 0;
    }
    
    void DynamicStringArray::Clear() {
    
    }
     
    void DynamicStringArray::Sort() {
    
    }
    
    std::string DynamicStringArray::ToString() {
      std::ostringstream ss;
      if (this->Empty() == false) {
        ss << *myarray[0];
        for (unsigned int i = 1; i < currentsize; i++) {
          ss << ", " << *myarray[i];
        }
      }
      return ss.str();
    }
    
    std::ostream& operator<<(std::ostream& comma, const DynamicStringArray& DSA) {
    
    }
    
    void DynamicStringArray::IncreaseCapacity() {
    
    }
    
    std::string DynamicStringArray::ToUpper(std::string letter) {
    
    }

  10. #190
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks wrong:
    Code:
    std::string* DynamicStringArray::GetLast() {
      int i = currentsize;
      if(currentsize == 0) {
        throw std::string("Array Empty");
      }
      return myarray[i];
    }
    I would expect:
    Code:
    std::string* DynamicStringArray::GetLast() {
      if(currentsize == 0) {
        throw std::string("Array Empty");
      }
      return myarray[currentsize - 1];
    }
    Also, remember that these sort of member functions that do not change the logical/observable state of the object should be declared const so that they can be called from objects of this class that are const (or const references).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #191
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ohhh OK, I see the difference now. It worked for GetFront, but not for GetLast--or rather there's something else going on there.
    Yeah, I can see that it's implemented wrong. I think you can figure it out though. Hint: when dealing with arrays, don't just automatically type i. The number between the brackets can be any expression - and what you want to return invariably is the last element of the array.

  12. #192
    Registered User
    Join Date
    Feb 2015
    Posts
    225
    Hey guys, I managed to get the code to pass all the tests. Thank you for your help and even more thanks for your patience. I know I'm not easy to work with, but I really do appreciate it. ^^;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need some help with an assignment..please.
    By ronaldh12 in forum C Programming
    Replies: 3
    Last Post: 09-30-2011, 09:33 AM
  2. Help!! Assignment!
    By Joeshua Lee in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2011, 09:30 AM
  3. Help!! Assignment!
    By Joeshua Lee in forum C Programming
    Replies: 3
    Last Post: 08-09-2011, 09:30 AM
  4. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  5. Assignment Help...maybe..
    By TerribleatC in forum C Programming
    Replies: 5
    Last Post: 01-05-2007, 08:05 AM