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 -
"Unhandled exception at 0x1022fb10 in lab3.exe: 0xC0000005: Access violation reading location 0x00000000."
-
-
+ this 0x0012fef0 {bufferSize=1 buffer=0x00000000 <Bad Ptr> } const String * const
-
bufferSize 1 int
-
+ buffer 0x00000000 <Bad Ptr> char *
-
-
-
+ buffer 0x00000000 <Bad Ptr> char *
-
whenever I input anything at run time, yet the output is all correct. It says the problem is something to do with ostream...
3 3278
implementation: -
//--------------------------------------------------------------------
-
//
-
// STRADT.CPP
-
//
-
// Implementation of the String ADT
-
//
-
//--------------------------------------------------------------------
-
-
-
#include <iostream>
-
#include "stradt.h"
-
#include <cstring>
-
-
//constructor used if a numeral amount of characters is passed in
-
//inputs: numChars, the number of characters for the string
-
//outputs: none
-
String::String ( int numChars ) // Create an empty string
-
{
-
-
bufferSize = numChars + 1; //declare a variable for the size, add 1 for delimiter
-
if ( numChars <= 1 ) //if numChars is 1 or smaller it won't work, due to delimiter
-
{
-
buffer = 0; //set as zero so that an obvious error is apparent
-
}
-
else //otherwise, create the string with the specified number of characters, initialized to 0
-
{
-
buffer = new char[numChars];
-
buffer[0] = 0;
-
}
-
}
-
-
//constructor used if a sequence of characters is entered
-
//inputs: charSeq points to the character sequence entered
-
//outputs: none
-
String::String ( const char *charSeq ) // Initialize using char*
-
{
-
-
bufferSize = strlen(charSeq) +1; // find length of charSeq
-
-
buffer = new char[bufferSize + 1]; // allocate memory for charSeq + the delimiter
-
-
strcpy(buffer, charSeq); // copy the charSeq into the buffer
-
-
}
-
-
//destructor, to clear the buffer data
-
//inputs:none
-
//outputs:none
-
String::~String ()
-
{
-
delete [] buffer; //clear the buffer
-
}
-
-
//length - finds the length of the buffer
-
//inputs: none
-
//outputs: length of the buffer
-
int String::length () const
-
{
-
return strlen(buffer); //return the buffer's length
-
}
-
-
// [] operator overload - subscript, returns contents of buffer slot n
-
//inputs:n, an int representing the desired slot
-
//outputs:contents of a slot
-
char String::operator [] ( int n ) const
-
{
-
if (n <= strlen(buffer)) //if the number is less than or equal to the length of the buffer
-
{
-
return buffer[n] ; //go ahead and return what is in the slot
-
}
-
else
-
return 0; //otherwise, return 0 so that user will see the error
-
}
-
//assignment operator overload - copies the contents of rightString into the buffer
-
//inputs: &rightString, whatever its contents may be
-
//outputs:none
-
void String::operator = ( const String &rightString ) // Assignment
-
{
-
if (buffer == rightString.buffer) //if they are the same
-
{
-
NULL; //don't do anything
-
}
-
-
else //otherwise...
-
{
-
bufferSize = rightString.bufferSize; //make sure that the size of buffer is the same as rightString
-
-
delete [] buffer; //delete the contents of buffer
-
buffer = new char [bufferSize]; //reassign buffer with a string of bufferSize
-
-
strcpy(buffer, rightString.buffer); //finally, copy rightString's buffer into buffer
-
}
-
-
-
}
-
-
//clear function - clears the buffer, and sets its size to zero
-
//inputs: none
-
//outputs: none
-
void String::clear()
-
{
-
buffer[0] = 0; //set buffer entries to zero
-
bufferSize = 0; //reuce buffer slots to zero
-
}
-
-
//showstructure- used to display contents of the buffer
-
//inputs:none
-
//outputs: none
-
void String::showStructure () const
-
{
-
std::cout << buffer << std::endl; //output the buffers, using the standard namespace
-
}
-
-
//copy constructor - copies the contents of valueString into buffer
-
//inputs:valueString
-
//outputs:none
-
String::String ( const String &valueString )
-
{
-
bufferSize = valueString.bufferSize; //make sure the bufferSize is the same as valueString's
-
-
buffer = new char[bufferSize]; //reassign buffer with a string of bufferSize slots
-
-
strcpy(buffer, valueString.buffer); //copy contents of valueString's buffer into buffer
-
-
}
-
-
// == operator overload - (friend function), compares leftString and rightString buffers to see if they are equal
-
//inputs: leftString and rightString
-
//outputs 1 or 0, depending on true or false
-
int operator == ( const String &leftString, const String &rightString )
-
{
-
if ((strcmp(leftString.buffer, rightString.buffer)) == 0) //if they are the same
-
return 1; //return true
-
else
-
return 0;//otherwise return false
-
}
-
-
// < operator overload - (friend function), sees if leftString's buffer is greater than rightString's
-
//inputs: leftString and rightString
-
//outputs 1 or 0, depending on true or false
-
int operator < ( const String &leftString, const String &rightString )
-
{
-
if ((strcmp(leftString.buffer, rightString.buffer)) < 0) //if leftString is larger
-
return 1;//return true
-
else
-
return 0;//otherwise return false
-
}
-
-
// > operator overload - (friend function), sees if leftString's buffer is less than rightString's
-
//inputs: leftString and rightString
-
//outputs 1 or 0, depending on true or false
-
int operator > ( const String &leftString, const String &rightString )
-
{
-
if ((strcmp(leftString.buffer, rightString.buffer)) > 0)//if leftstring is less than rightString
-
return 1;//return true
-
else
-
return 0;//otherwise return false
-
}
header: -
//--------------------------------------------------------------------
-
//
-
// Laboratory 3 stradt.h
-
//
-
// Class declaration for the array implementation of the String ADT
-
//
-
//--------------------------------------------------------------------
-
-
class String
-
{
-
public:
-
-
// Constructors
-
String ( int numChars = 0 ); // Create an empty string
-
String ( const char *charSeq ); // Initialize using char*
-
-
// Destructor
-
~String ();
-
-
// String operations
-
int length () const; // # characters
-
char operator [] ( int n ) const; // Subscript
-
void operator = ( const String &rightString ); // Assignment
-
void clear (); // Clear string
-
-
// Output the string structure -- used in testing/debugging
-
void showStructure () const;
-
-
// In-lab operation
-
String ( const String &valueString ); // Copy constructor
-
-
private:
-
-
// Data members
-
int bufferSize; // Size of the string buffer
-
char *buffer; // String buffer containing a null-terminated
-
// sequence of characters
-
-
// Friends
-
/*
-
// String input/output operations (In-lab Exercise 1)
-
friend istream & operator >> ( istream &input,
-
String &inputString );
-
friend ostream & operator << ( ostream &output,
-
const String &outputString );
-
*/
-
// Relational operations (In-lab Exercise 3)
-
friend int operator == ( const String &leftString,
-
const String &rightString );
-
friend int operator < ( const String &leftString,
-
const String &rightString );
-
friend int operator > ( const String &leftString,
-
const String &rightString );
-
-
};
test program:
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: -
1>------ Build started: Project: lab3, Configuration: Debug Win32 ------
-
1>Compiling...
-
1>STRIO.CPP
-
1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2143: syntax error : missing ';' before '&'
-
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
-
1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'input' : undeclared identifier
-
1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'String' : undeclared identifier
-
1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : error C2065: 'inputString' : undeclared identifier
-
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
-
1>c:\users\projects\112\lab3\lab32\lab3\strio.cpp(14) : fatal error C1903: unable to recover from previous error(s); stopping compilation
-
1>Build log was saved at "file://c:\Users\projects\112\LAB3\lab32\lab3\Debug\BuildLog.htm"
-
1>lab3 - 7 error(s), 0 warning(s)
-
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
-
the cpp file: -
//--------------------------------------------------------------------
-
//
-
// Laboratory 3, In-lab Exercise 1 strio.cpp
-
//
-
// String input/output operations
-
//
-
//--------------------------------------------------------------------
-
-
#include <iostream>
-
#include <iomanip>
-
-
//--------------------------------------------------------------------
-
-
istream & operator >> ( istream &input, String &inputString )
-
-
// String input function. Extracts a string from istream input and
-
// returns it in inputString. Returns the state of the input stream.
-
-
{
-
const int textBufferSize = 256; // Large (but finite)
-
char textBuffer [textBufferSize]; // text buffer
-
-
// Read a string into textBuffer, setw is used to prevent buffer
-
// overflow.
-
-
input >> setw(textBufferSize) >> textBuffer;
-
-
// Apply the String(char*) constructor to convert textBuffer to
-
// a string. Assign the resulting string to inputString using the
-
// assignment operator.
-
-
inputString = textBuffer;
-
-
// Return the state of the input stream.
-
-
return input;
-
}
-
-
//--------------------------------------------------------------------
-
-
ostream & operator << ( ostream &output, const String &outputString )
-
-
// String output function. Inserts outputString in ostream output.
-
// Returns the state of the output stream.
-
-
{
-
output << outputString.buffer;
-
return output;
-
}
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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 ...
|
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'.
...
|
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
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 ...
|
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,...
|
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$) {
}
...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |