943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1154
  • C++ RSS
Jan 16th, 2007
0

MyString Woes

Expand Post »
I was absent for the discussion and lab due to illness and I am at a total loss as to how to do this assignment.

Implement the my_string class in header file my_string.h, so that the test code given below reports SUCCESS.

C++ Syntax (Toggle Plain Text)
  1. // test_my_string.cpp
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cassert>
  6. #include "my_string.h"
  7.  
  8. using namespace std;
  9.  
  10. //#define STRING string
  11. #define STRING my_string
  12.  
  13. int main()
  14. {
  15. STRING s1; // s1 == ""
  16. assert(s1.length() == 0);
  17.  
  18. STRING s2("hi"); // s2 == "hi"
  19. assert(s2.length() == 2);
  20.  
  21. STRING s3(s2); // s3 == "hi"
  22. assert(s3.length() == 2);
  23. assert(s3[0] == 'h');
  24. assert(s3[1] == 'i');
  25.  
  26. s1 = s2; // s1 == "hi"
  27.  
  28. s3 = "bye"; // s3 == "bye"
  29. assert(s3.length() == 3);
  30. assert(s3[0] == 'b');
  31. assert(s3[1] == 'y');
  32. assert(s3[2] == 'e');
  33.  
  34. s1 += "re"; // s1 == "hire"
  35. assert(s1.length() == 4);
  36. assert(s1[0] == 'h');
  37. assert(s1[1] == 'i');
  38. assert(s1[2] == 'r');
  39. assert(s1[3] == 'e');
  40.  
  41. cout << "SUCCESS" << endl;

I cannot get the program to get past

assert(s3[0] == 'h');

nor can I get cout << s3[0]; to work.

What am I missing here?
Last edited by FC Jamison; Jan 16th, 2007 at 1:54 am.
Similar Threads
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Jan 16th, 2007
0

Re: MyString Woes

Nevermind...

I got it to work using

C++ Syntax (Toggle Plain Text)
  1. char &operator[](unsigned int index) {
  2. return str[index];
  3. }
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Setprecision help please
Next Thread in C++ Forum Timeline: Adding Random Numbers help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC