473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String ADT HELP! Pointer problems

I am done and this compiles and everything (except for one of the provided files, which seems to have errors itself...I exclude that from the build until I need it, but will post it too). The only code I made is the implementation. Other than that, the others are provided to me. I need this before tomorrow, if you can help me. This is probably a stupid typo or something but I am at my wit's end.

I am having problems with a pointer somewhere. I am using VC++, and I get problems like
Expand|Select|Wrap|Line Numbers
  1.  "Unhandled exception at 0x1022fb10 in lab3.exe: 0xC0000005: Access violation reading location 0x00000000." 
  2.  
  3. +        this    0x0012fef0 {bufferSize=1 buffer=0x00000000 <Bad Ptr> }    const String * const
  4.         bufferSize    1    int
  5. +        buffer    0x00000000 <Bad Ptr>    char *
  6.  
  7.  
  8. +        buffer    0x00000000 <Bad Ptr>    char *
  9.  
whenever I input anything at run time, yet the output is all correct. It says the problem is something to do with ostream...
Feb 14 '07 #1
3 3278
implementation:
Expand|Select|Wrap|Line Numbers
  1. //--------------------------------------------------------------------
  2. //
  3. //                                              STRADT.CPP
  4. //
  5. //  Implementation of the String ADT
  6. //
  7. //--------------------------------------------------------------------
  8.  
  9.  
  10. #include <iostream>
  11. #include "stradt.h"
  12. #include <cstring>
  13.  
  14. //constructor used if a numeral amount of characters is passed in
  15. //inputs: numChars, the number of characters for the string
  16. //outputs: none
  17. String::String ( int numChars )            // Create an empty string
  18. {    
  19.  
  20.     bufferSize = numChars + 1; //declare a variable for the size, add 1 for delimiter
  21.     if ( numChars <= 1 ) //if numChars is 1 or smaller it won't work, due to delimiter 
  22.         {
  23.          buffer = 0; //set as zero so that an obvious error is apparent
  24.         }
  25.     else //otherwise, create the string with the specified number of characters, initialized to 0
  26.         {
  27.          buffer = new char[numChars]; 
  28.          buffer[0] = 0;
  29.         }
  30. }
  31.  
  32. //constructor used if a sequence of characters is entered
  33. //inputs: charSeq points to the character sequence entered
  34. //outputs: none
  35. String::String ( const char *charSeq )      // Initialize using char*
  36. {
  37.  
  38.    bufferSize = strlen(charSeq) +1;          // find length of charSeq
  39.  
  40.    buffer = new char[bufferSize + 1]; // allocate memory for charSeq + the delimiter
  41.  
  42.    strcpy(buffer, charSeq);             // copy the charSeq into the buffer
  43.  
  44. }
  45.  
  46. //destructor, to clear the buffer data
  47. //inputs:none
  48. //outputs:none
  49. String::~String ()
  50. {
  51.     delete [] buffer; //clear the buffer 
  52. }
  53.  
  54. //length - finds the length of the buffer
  55. //inputs: none
  56. //outputs: length of the buffer
  57. int String::length () const
  58. {
  59.     return strlen(buffer); //return the buffer's length
  60.  
  61. // [] operator overload - subscript, returns contents of buffer slot n
  62. //inputs:n, an int representing the desired slot
  63. //outputs:contents of a slot
  64. char String::operator [] ( int n ) const
  65. {
  66.     if (n <= strlen(buffer)) //if the number is less than or equal to the length of the buffer
  67.     { 
  68.         return  buffer[n] ; //go ahead and return what is in the slot
  69.     }
  70.     else 
  71.         return 0; //otherwise, return 0 so that user will see the error
  72. }
  73. //assignment operator overload - copies the contents of rightString into the buffer
  74. //inputs: &rightString, whatever its contents may be
  75. //outputs:none
  76. void String::operator = ( const String &rightString )   // Assignment
  77. {
  78.             if (buffer == rightString.buffer) //if they are the same
  79.             {
  80.                                 NULL; //don't do anything
  81.             }
  82.  
  83.             else //otherwise...
  84.             {
  85.                 bufferSize = rightString.bufferSize; //make sure that the size of buffer is the same as rightString
  86.  
  87.                 delete [] buffer; //delete the contents of buffer
  88.                 buffer  = new char [bufferSize]; //reassign buffer with a string of bufferSize
  89.  
  90.                 strcpy(buffer, rightString.buffer); //finally, copy rightString's buffer into buffer
  91.             }
  92.  
  93.  
  94. }
  95.  
  96. //clear function - clears the buffer, and sets its size to zero
  97. //inputs: none
  98. //outputs: none
  99. void String::clear()
  100. {
  101.     buffer[0] = 0; //set buffer entries to zero
  102.     bufferSize = 0; //reuce buffer slots to zero
  103. }
  104.  
  105. //showstructure- used to display contents of the buffer
  106. //inputs:none
  107. //outputs: none
  108. void String::showStructure () const
  109. {
  110.     std::cout << buffer << std::endl; //output the buffers, using the standard namespace
  111. }
  112.  
  113. //copy constructor - copies the contents of valueString into buffer
  114. //inputs:valueString
  115. //outputs:none
  116. String::String ( const String &valueString )
  117. {
  118.     bufferSize = valueString.bufferSize; //make sure the bufferSize is the same as valueString's
  119.  
  120.     buffer = new char[bufferSize]; //reassign buffer with a string of bufferSize slots
  121.  
  122.     strcpy(buffer, valueString.buffer); //copy contents of valueString's buffer into buffer
  123.  
  124. }    
  125.  
  126. // == operator overload - (friend function), compares leftString and rightString buffers to see if they are equal
  127. //inputs: leftString and rightString
  128. //outputs 1 or 0, depending on true or false
  129. int operator == ( const String &leftString, const String &rightString )
  130. {
  131.     if ((strcmp(leftString.buffer, rightString.buffer)) == 0) //if they are the same
  132.         return 1; //return true
  133.     else
  134.         return 0;//otherwise return false
  135. }
  136.  
  137. // < operator overload - (friend function), sees if leftString's buffer is greater than rightString's
  138. //inputs: leftString and rightString
  139. //outputs 1 or 0, depending on true or false
  140. int operator <  ( const String &leftString, const String &rightString )
  141.     {
  142.         if ((strcmp(leftString.buffer, rightString.buffer)) < 0) //if leftString is larger
  143.             return 1;//return true
  144.         else
  145.         return 0;//otherwise return false
  146.     }
  147.  
  148. // > operator overload - (friend function), sees if leftString's buffer is less than rightString's
  149. //inputs: leftString and rightString
  150. //outputs 1 or 0, depending on true or false
  151. int operator >  ( const String &leftString, const String &rightString )
  152.     {
  153.         if ((strcmp(leftString.buffer, rightString.buffer)) > 0)//if leftstring is less than rightString
  154.             return 1;//return true
  155.         else
  156.         return 0;//otherwise return false
  157.     }
Feb 14 '07 #2
header:
Expand|Select|Wrap|Line Numbers
  1. //--------------------------------------------------------------------
  2. //
  3. //  Laboratory 3                                            stradt.h
  4. //
  5. //  Class declaration for the array implementation of the String ADT
  6. //
  7. //--------------------------------------------------------------------
  8.  
  9. class String
  10. {
  11.   public:
  12.  
  13.     // Constructors
  14.     String ( int numChars = 0 );            // Create an empty string
  15.     String ( const char *charSeq );         // Initialize using char*
  16.  
  17.     // Destructor
  18.     ~String ();
  19.  
  20.     // String operations
  21.     int length () const;                             // # characters
  22.     char operator [] ( int n ) const;                // Subscript
  23.     void operator = ( const String &rightString );   // Assignment
  24.     void clear ();                                   // Clear string
  25.  
  26.     // Output the string structure -- used in testing/debugging
  27.     void showStructure () const;
  28.  
  29.     // In-lab operation
  30.     String ( const String &valueString );          // Copy constructor
  31.  
  32.   private:
  33.  
  34.     // Data members
  35.     int bufferSize;   // Size of the string buffer
  36.     char *buffer;     // String buffer containing a null-terminated
  37.                       // sequence of characters
  38.  
  39.   // Friends
  40. /*
  41.     // String input/output operations (In-lab Exercise 1)
  42.     friend istream & operator >> ( istream &input,
  43.                                    String &inputString );
  44.     friend ostream & operator << ( ostream &output,
  45.                                    const String &outputString );
  46. */
  47.     // Relational operations (In-lab Exercise 3)
  48.     friend int operator == ( const String &leftString,
  49.                              const String &rightString );
  50.     friend int operator <  ( const String &leftString,
  51.                              const String &rightString );
  52.     friend int operator >  ( const String &leftString,
  53.                              const String &rightString );
  54.  
  55. };
test program:
Expand|Select|Wrap|Line Numbers
  1. //--------------------------------------------------------------------
  2. //
  3. //  Laboratory 3                                           test3.cpp
  4. //
  5. //  Test program for the operations in the String ADT
  6. //
  7. //--------------------------------------------------------------------
  8.  
  9. #include <iostream>
  10. #include "stradt.h"
  11.  
  12. using namespace std;
  13.  
  14. //--------------------------------------------------------------------
  15. //
  16. //  Function prototype
  17.  
  18. void dummy ( String copyString );   // copyString is passed by value
  19.  
  20. //--------------------------------------------------------------------
  21.  
  22.  
  23. void main()
  24. {
  25.     String a("a"),                // Predefined test strings
  26.            alp("alp"),
  27.            alpha("alpha"),
  28.            epsilon("epsilon"),
  29.            empty,
  30.            assignStr(5),          // Destination for assignment
  31.            inputStr(5);           // Input string
  32.     int n;                        // Input subscript
  33.     char ch,                      // Character specified by subscript
  34.          selection;               // Input test selection
  35.  
  36.     // Get user test selection.
  37.  
  38.     cout << endl << "Tests:" << endl;
  39.     cout << "  1  Tests the constructors" << endl;
  40.     cout << "  2  Tests the length operation" << endl;
  41.     cout << "  3  Tests the subscript operation" << endl;
  42.     cout << "  4  Tests the assignment and clear operations" << endl;
  43.     cout << "  5  Tests the copy constructor         (Inactive : "
  44.          << "In-lab Exercise 2)" << endl;
  45.     cout << "  6  Tests the relational operations    (Inactive : "
  46.          << "In-lab Exercise 3)" << endl;
  47.     cout << "Select the test to run : ";
  48.     cin >> selection;
  49.  
  50.     // Execute the selected test.
  51.  
  52.     cout << endl;
  53.     switch ( selection )
  54.     {
  55.       case '1' :
  56.            // Test 1 : Tests the constructors.
  57.            cout << "Structure of various strings: " << endl;
  58.            cout << "string: alpha" << endl;
  59.            alpha.showStructure();
  60.            cout << "string: epsilon" << endl;
  61.            epsilon.showStructure();
  62.            cout << "string: a" << endl;
  63.            a.showStructure();
  64.            cout << "empty string" << endl;
  65.            empty.showStructure();
  66.            break;
  67.  
  68.       case '2' :
  69.            // Test 2 : Tests the length operation.
  70.            cout << "Lengths of various strings:"  << endl;
  71.            cout << " alpha   : " << alpha.length() << endl;
  72.            cout << " epsilon : " << epsilon.length() << endl;
  73.            cout << " a       : " << a.length() << endl;
  74.            cout << " empty   : " << empty.length() << endl;
  75.            break;
  76.  
  77.       case '3' :
  78.            // Test 3 : Tests the subscript operation.
  79.            cout << "Enter a subscript : ";
  80.            cin >> n;
  81.            ch = alpha[n];
  82.            cout << "  alpha[" << n << "] : ";
  83.            if ( ch == '\0' )
  84.               cout << "\\0" << endl;
  85.            else
  86.               cout << ch << endl;
  87.            break;
  88.  
  89.       case '4' :
  90.            // Test 4 : Tests the assignment and clear operations.
  91.            cout << "Assignments:" << endl;
  92.            cout << "assignStr = alpha" << endl;
  93.            assignStr = alpha;
  94.            assignStr.showStructure();
  95.            cout << "assignStr = a" << endl;
  96.            assignStr = a;
  97.            assignStr.showStructure();
  98.            cout << "assignStr = empty" << endl;
  99.            assignStr = empty;
  100.            assignStr.showStructure();
  101.            cout << "assignStr = epsilon" << endl;
  102.            assignStr = epsilon;
  103.            assignStr.showStructure();
  104.            cout << "assignStr = assignStr" << endl;
  105.            assignStr = assignStr;
  106.            assignStr.showStructure();
  107.            cout << "assignStr = alpha" << endl;
  108.            assignStr = alpha;
  109.            assignStr.showStructure();
  110.            cout << "Clear assignStr" << endl;
  111.            assignStr.clear();
  112.            assignStr.showStructure();
  113.            cout << "Confirm that alpha has not been cleared" << endl;
  114.            alpha.showStructure();
  115.            break;
  116.  
  117.   case '5' :                                  // In-lab Exercise 2
  118.         // Test 5 : Tests the copy constructor.
  119.         cout << "Calls by value:" << endl;
  120.         cout << "alpha before call" << endl;
  121.         alpha.showStructure();
  122.         dummy(alpha);
  123.         cout << "alpha after call" << endl;
  124.         alpha.showStructure();
  125.         cout << "a before call" << endl;
  126.         a.showStructure();
  127.         dummy(a);
  128.         cout << "a after call" << endl;
  129.         a.showStructure();
  130.         break;
  131.  
  132.    case '6' :                                  // In-lab Exercise 3
  133.         // Test 6 : Tests the relational operations.
  134.         cout << "  left     right     <   ==   > " << endl;
  135.         cout << "--------------------------------" << endl;
  136.         cout << " alpha    epsilon    " << (alpha<epsilon)
  137.              << "    " << (alpha==epsilon) << "   "
  138.              << (alpha>epsilon) << endl;
  139.         cout << " epsilon   alpha     " << (epsilon<alpha)
  140.              << "    " << (epsilon==alpha) << "   "
  141.              << (epsilon>alpha) << endl;
  142.         cout << " alpha     alpha     " << (alpha<alpha) << "    "
  143.              << (alpha==alpha) << "   " << (alpha>alpha) << endl;
  144.         cout << "  alp      alpha     " << (alp<alpha) << "    "
  145.              << (alp==alpha) << "   " << (alp>alpha) << endl;
  146.         cout << " alpha      alp      " << (alpha<alp) << "    "
  147.              << (alpha==alp) << "   " << (alpha>alp) << endl;
  148.         cout << "   a       alpha     " << (a<alpha) << "    "
  149.              << (a==alpha) << "   " << (a>alpha) << endl;
  150.         cout << " alpha       a       " << (alpha<a) << "    "
  151.              << (alpha==a) << "   " << (alpha>a) << endl;
  152.         cout << " empty     alpha     " << (empty<alpha) << "    "
  153.              << (empty==alpha) << "   " << (empty>alpha) << endl;
  154.         cout << " alpha     empty     " << (alpha<empty) << "    "
  155.              << (alpha==empty) << "   " << (alpha>empty) << endl;
  156.         cout << " empty     empty     " << (empty<empty) << "    "
  157.              << (empty==empty) << "   " << (empty>empty) << endl;
  158.         break;
  159.  
  160.       default :
  161.            cout << "Inactive or invalid test" << endl;
  162.     }
  163.  
  164.     cin.ignore();
  165.     cin.ignore();
  166. }
  167.  
  168. //--------------------------------------------------------------------
  169.  
  170. void dummy ( String copyString )
  171.  
  172. // Dummy routine that is passed a string using call by value. Outputs
  173. // copyString and clears it.
  174.  
  175. {
  176.     cout << "Copy of string" << endl;
  177.     copyString.showStructure();
  178.     cout << "Clear copy" << endl;
  179.     copyString.clear();
  180.     copyString.showStructure();
  181. }
Feb 14 '07 #3
the buggy Strio.cpp, which thows the following errors for no apparent reason (I didn't create this, it was part of the project files for testing...

first, the errors:
Expand|Select|Wrap|Line Numbers
  1. 1>------ Build started: Project: lab3, Configuration: Debug Win32 ------
  2. 1>Compiling...
  3. 1>STRIO.CPP
  4. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2143: syntax error : missing ';' before '&'
  5. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  6. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'input' : undeclared identifier
  7. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'String' : undeclared identifier
  8. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'inputString' : undeclared identifier
  9. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  10. 1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : fatal error C1903: unable to recover from previous error(s); stopping compilation
  11. 1>Build log was saved at "file://c:\Users\projects\112\LAB3\lab32\lab3\Debug\BuildLog.htm"
  12. 1>lab3 - 7 error(s), 0 warning(s)
  13. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  14.  
the cpp file:
Expand|Select|Wrap|Line Numbers
  1. //--------------------------------------------------------------------
  2. //
  3. //  Laboratory 3, In-lab Exercise 1                        strio.cpp
  4. //
  5. //  String input/output operations
  6. //
  7. //--------------------------------------------------------------------
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11.  
  12. //--------------------------------------------------------------------
  13.  
  14. istream & operator >> ( istream &input, String &inputString )
  15.  
  16. // String input function. Extracts a string from istream input and
  17. // returns it in inputString. Returns the state of the input stream.
  18.  
  19. {
  20.     const int textBufferSize = 256;     // Large (but finite)
  21.     char textBuffer [textBufferSize];   // text buffer
  22.  
  23.     // Read a string into textBuffer, setw is used to prevent buffer
  24.     // overflow.
  25.  
  26.     input >> setw(textBufferSize) >> textBuffer;
  27.  
  28.     // Apply the String(char*) constructor to convert textBuffer to
  29.     // a string. Assign the resulting string to inputString using the
  30.     // assignment operator.
  31.  
  32.     inputString = textBuffer;
  33.  
  34.     // Return the state of the input stream.
  35.  
  36.     return input;
  37. }
  38.  
  39. //--------------------------------------------------------------------
  40.  
  41. ostream & operator << ( ostream &output, const String &outputString )
  42.  
  43. // String output function. Inserts outputString in ostream output.
  44. // Returns the state of the output stream.
  45.  
  46. {
  47.    output << outputString.buffer;
  48.    return output;
  49. }
  50.  
  51.  
Feb 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Amy | last post by:
I have a string say a = "Hello how are YOU" I want to end up with a = "hello how are you' Isn't there a built in method for changing a string to lowercase?
11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
5
by: Sia Jai Sung | last post by:
Hi, I have a class that I modify from a sample program, like below ========================================== Imports System Imports System.Web.UI Imports System.Security.Cryptography ...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
5
by: Joe Nova | last post by:
I'm a C++ noob and I need a little help manipulating strings. I've got a program that takes an expression in the form: "operand1 operator operand2" I'd like to: 1. Find the total length...
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.