The constructor for a homemade string class

No values will be assigned to my object even though a string is being set to it. Also no values will be read in or out (I'm overloading the << and >> operators). For example, I have in main: obstring first_name("James") and the only output I get is "J". And if I say (in main) obstring first_name; cin >> first_name; nothing is inputted. I'm at a loss. This is c++.

#include <iostream>
#include <cstring>
#include <cctype>
#include "obstring.h"
using namespace std;

namespace main_savitch_4
{
obstring::obstring(const char str[])
{
current_length = strlen(str);
allocated = current_length + 1;
sequence = new char[allocated];
strncpy(sequence, str);
}

obstring::obstring(const obstring& source)
{
current_length = strlen(source.sequence);
allocated = (source.current_length)+1;
sequence = new char[source.allocated];
strcpy(sequence, source.sequence);
}

obstring::~obstring()
{
delete [] sequence;
}

void obstring::operator +=(const obstring& addend)
{
if((allocated+addend.allocated) > current_length)
{
reserve(allocated + addend.allocated);
}
strcat(sequence,addend.sequence);
}

void obstring::operator +=(const char addend[])
{
int addend_length;
addend_length = strlen(addend)+1;
if((allocated + addend_length) > current_length)
{
reserve(allocated + addend_length);
}
strcat(sequence,addend);
}

void obstring::operator +=(char addend)
{
if((allocated + 2) > current_length)
{
reserve(allocated + 2);
}
strcat(sequence,addend);
}

void obstring::operator +=(char addend)
{
if((allocated + 2) > current_length)
{
reserve(allocated + 2);
}
sequence[allocated] = addend;
}

void obstring::reserve(size_t n)
{
char *larger;
if (n == allocated)
{
return;
}
if (n < allocated)
{
n = allocated;
}
larger = new char[n];
strcpy(larger, sequence);
delete [] sequence;
sequence = larger;
}

void obstring::operator =(const obstring& source)
{
char *copy_sequence;
copy_sequence = new char[source.allocated];
strcpy(sequence, source.sequence);
delete [] sequence;
sequence = copy_sequence;
cout << "were is broke" << endl;
}

char obstring::operator [](size_t position) const
{
char target_char;
target_char = sequence[position];
cout << target_char << endl;
return target_char;
}

std::ostream& operator <<(std::ostream& outs, const obstring& source)
{
for(int i=0; i < source.allocated; i++)
{
outs << source.sequence[i];
if (i == source.current_length)
outs << source.sequence[i];
return outs;
}
}

obstring operator +(const obstring& s1, const obstring& s2)
{
obstring final_string(s1);
final_string += s2;
return final_string;
}

std::istream& operator >>(std::istream& ins, obstring& target)
{
int counter = 0;
char c = target[counter];
while(ins && isspace(ins.peek()))
{
ins.ignore();
}
while(ins && isspace(c))
{
//target[counter]=c;
ins >> c;
counter++;
}
}

void getline(std::istream& ins, obstring& target, char delimiter)
{
delimiter = '\n';
char c;
int counter = 0;
while(ins && isspace(ins.peek()))
{
ins.ignore();
}
while(ins && target[counter] != delimiter)
{
// target[counter]=c;
ins >> c;
counter++;
}
}
}

For a start, please put your code in a code.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <cstring>
#include <cctype>
#include "obstring.h" // ?????????????????????????????????
using namespace std;

namespace main_savitch_4
{
	obstring::obstring(const char str[])
	{
		current_length = strlen(str);
		allocated = current_length + 1;
		sequence = new char[allocated];
		strncpy(sequence, str);
	}

	obstring::obstring(const obstring& source)
	{
		current_length = strlen(source.sequence);
		allocated = (source.current_length) + 1;
		sequence = new char[source.allocated];
		strcpy(sequence, source.sequence);
	}

	obstring::~obstring()
	{
		delete[] sequence;
	}

	void obstring::operator +=(const obstring& addend)
	{
		if ((allocated + addend.allocated) > current_length)
		{
			reserve(allocated + addend.allocated);
		}
		strcat(sequence, addend.sequence);
	}

	void obstring::operator +=(const char addend[])
	{
		int addend_length;
		addend_length = strlen(addend) + 1;
		if ((allocated + addend_length) > current_length)
		{
			reserve(allocated + addend_length);
		}
		strcat(sequence, addend);
	}

	void obstring::operator +=(char addend)
	{
		if ((allocated + 2) > current_length)
		{
			reserve(allocated + 2);
		}
		strcat(sequence, addend);
	}

	void obstring::operator +=(char addend)
	{
		if ((allocated + 2) > current_length)
		{
			reserve(allocated + 2);
		}
		sequence[allocated] = addend;
	}

	void obstring::reserve(size_t n)
	{
		char *larger;
		if (n == allocated)
		{
			return;
		}
		if (n < allocated)
		{
			n = allocated;
		}
		larger = new char[n];
		strcpy(larger, sequence);
		delete[] sequence;
		sequence = larger;
	}

	void obstring::operator =(const obstring& source)
	{
		char *copy_sequence;
		copy_sequence = new char[source.allocated];
		strcpy(sequence, source.sequence);
		delete[] sequence;
		sequence = copy_sequence;
		cout << "were is broke" << endl;
	}

	char obstring::operator [](size_t position) const
	{
		char target_char;
		target_char = sequence[position];
		cout << target_char << endl;
		return target_char;
	}

	std::ostream& operator <<(std::ostream& outs, const obstring& source)
	{
		for (int i = 0; i < source.allocated; i++)
		{
			outs << source.sequence[i];
			if (i == source.current_length)
				outs << source.sequence[i];
			return outs;
		}
	}

	obstring operator +(const obstring& s1, const obstring& s2)
	{
		obstring final_string(s1);
		final_string += s2;
		return final_string;
	}

	std::istream& operator >>(std::istream& ins, obstring& target)
	{
		int counter = 0;
		char c = target[counter];
		while (ins && isspace(ins.peek()))
		{
			ins.ignore();
		}
		while (ins && isspace(c))
		{
			//target[counter]=c;
			ins >> c;
			counter++;
		}
	}

	void getline(std::istream& ins, obstring& target, char delimiter)
	{
		delimiter = '\n';
		char c;
		int counter = 0;
		while (ins && isspace(ins.peek()))
		{
			ins.ignore();
		}
		while (ins && target[counter] != delimiter)
		{
			// target[counter]=c;
			ins >> c;
			counter++;
		}
	}
}
Your code will not compile, so your comments about what happens when it runs aren't terribly helpful. If you could compile your code, this is not the code you compiled.

There are several logic problems with your code, but you should probably start with the fact that reserve is not implemented correctly.

I've complied it, so it does compile. That's how I got those results that I mentioned. Also the reserve function I got from my textbook so I'm not sure what you mean by that.
I've complied it, so it does compile.
strcpyn requires 3 arguments. You only supply 2.
You have two definitions of obstring::operator+=(char)
One cannot use strcat with single character as an argument.

Your code, as supplied, will not compile, even if we were to fake the class definition you didn't supply. This is not the code you are running.



Also the reserve function I got from my textbook so I'm not sure what you mean by that.

On closer inspection, it's not actually incorrect it just does some needless things.



closed account (48T7M4Gy)
FWIW OP the problem is nobody has your obstring.h file hence my ???????'s aside from the fact that I put the code blocks in for you.

As supplied, your code doesn't compile - that's a fact.

Up to you. Beyond that, not much to do at this end. reserve I'll leave to others.
This is the test file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// This is a small demonstration program showing how the obstring class is used.
#include <iostream>     // Provides cout and cin
#include <cstdlib>      // Provides EXIT_SUCCESS
#include "obstring.h"
using namespace std;
using namespace main_savitch_4;

// PROTOTYPES for functions used by this demonstration program:
void match(const obstring& variety, const obstring& mine, const obstring& yours);
// The two strings, mine and yours, are compared. If they are the same, then a 
// message is printed saying they are the same; otherwise mine is printed
// in a message. In either case, the string variety is part of the message.

int main( )
{
    const obstring BLANK(" ");
    obstring me_first("Demo"), me_last("Program");
    obstring you_first, you_last, you;
    
    cout << "What is your first name? ";
    cin >> you_first;
    match("first name", me_first, you_first);
    cout << "What is your last name? ";
    cin >> you_last;
    match("last name", me_last, you_last);

    you = you_first + BLANK + you_last;
    cout << "I am happy to meet you, " << you << "." << endl;
    return EXIT_SUCCESS; 
}

void match(const obstring& variety, const obstring& mine, const obstring& yours)
{
    if (mine == yours)
        cout << "That is the same as my " << variety << "!" << endl;
    else
        cout << "My " << variety << " is " << mine << "." << endl;
}
This is the .h file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Alexandrea Washington, CS 202, Assignment 8
// File: obstring.h 
// CLASS PROVIDED: obstring
//   This is a simple version of the Standard Library obstring.
//   It is part of the namespace main_savitch_4, from the textbook
//   "Data Structures and Other Objects Using C++"
//   by Michal Main and Walter Savitch
//
// CONSTRUCTOR for the obstring class:
//   obstring(const char str[ ] = "") -- default argument is the empty string.
//     Precondition: str is an ordinary null-terminated string.
//     Postcondition: The string contains the sequence of chars from str.
//
// CONSTANT MEMBER FUNCTIONS for the obstring class:
//   size_t length( ) const
//     Postcondition: The return value is the number of characters in the
//     string.
//
//   char operator [ ](size_t position) const
//     Precondition: position < length( ).
//     Postcondition: The value returned is the character at the specified
//     position of the string. A string's positions start from 0 at the start
//     of the sequence and go up to length( )-1 at the right end.
//
// MODIFICATION MEMBER FUNCTIONS for the obstring class:
//   void operator +=(const obstring& addend)
//     Postcondition: addend has been catenated to the end of the string.
//
//   void operator +=(const char addend[ ])
//     Precondition: addend is an ordinary null-terminated string.
//     Postcondition: addend has been catenated to the end of the string.
//
//   void operator +=(char addend)
//     Postcondition: The single character addend has been catenated to the
//     end of the string.
//
//   void reserve(size_t n)
//     Postcondition: All functions will now work efficiently (without
//     allocating new memory) until n characters are in the string.
//
// NON-MEMBER FUNCTIONS for the obstring class:
//   obstring operator +(const obstring& s1, const obstring& s2)
//     Postcondition: The string returned is the catenation of s1 and s2.
//
//   istream& operator >>(istream& ins, obstring& target)
//     Postcondition: A string has been read from the istream ins, and the
//     istream ins is then returned by the function. The reading operation
//     skips white space (i.e., blanks, newlines, tabs) at the start of ins.
//     Then the string is read up to the next white space or the end of the
//     file. The white space character that terminates the string has not
//     been read.
//
//   ostream& operator <<(ostream& outs, const obstring& source)
//     Postcondition: The sequence of characters in source has been written
//     to outs. The return value is the ostream outs.
//
//   void getline(istream& ins, obstring& target, char delimiter)
//     Postcondition: A string has been read from the istream ins. The reading
//     operation starts by skipping any white space, then reading all characters
//     (including white space) until the delimiter is read and discarded (but
//     not added to the end of the string). The return value is ins.
//
//  VALUE SEMANTICS for the obstring class:
//    Assignments and the copy constructor may be used with obstring objects.
//
//  TOTAL ORDER SEMANTICS for the obstring class:
//    The six comparison operators (==, !=, >=, <=, >, and <) are implemented
//    for the obstring class, forming a total order semantics, using the usual
//    lexicographic order on strings.
// 
// DYNAMIC MEMORY usage by the obstring class: 
//   If there is insufficient dynamic memory then the following functions call
//   new_handler: The constructors, resize, operator +=, operator +, and the
//   assignment operator.

#ifndef MAIN_SAVITCH_CHAPTER4_MYSTRING_H
#define MAIN_SAVITCH_CHAPTER4_MYSTRING_H
#include <cstdlib>  // Provides size_t

namespace main_savitch_4
{
    class obstring
    {
    public:
        // CONSTRUCTORS and DESTRUCTOR
        obstring(const char str[ ] = "");
        obstring(const obstring& source);
        ~obstring( );
        // MODIFICATION MEMBER FUNCTIONS
        void operator +=(const obstring& addend);
        void operator +=(const char addend[ ]);
        void operator +=(char addend);
        void reserve(size_t n);
        void operator =(const obstring& source);
        // CONSTANT MEMBER FUNCTIONS
        size_t length( ) const { return current_length; }
        char operator [ ](size_t position) const;
        // FRIEND FUNCTIONS
        friend std::ostream& operator <<(std::ostream& outs, const obstring& source);
        friend bool operator ==(const obstring& s1, const obstring& s2);
        friend bool operator !=(const obstring& s1, const obstring& s2);
        friend bool operator >=(const obstring& s1, const obstring& s2);
        friend bool operator <=(const obstring& s1, const obstring& s2);
        friend bool operator > (const obstring& s1, const obstring& s2);
        friend bool operator < (const obstring& s1, const obstring& s2);
    private:
        char *sequence;
        size_t allocated;
        size_t current_length;
    };

    // NON-MEMBER FUNCTIONS for the obstring class
    obstring operator +(const obstring& s1, const obstring& s2);
    std::istream& operator >>(std::istream& ins, obstring& target);
    void getline(std::istream& ins, obstring& target, char delimiter);
}
The thing is I'm getting all of this from my textbook. The constructor that is written above I copied from the book. The rest of it is based on what my textbook says to do. The test file and the .h file weren't written by me. I have to write the implementation file based on the other two files.
This is the .h file.

That was easy enough to fake. The real issue is that your code still cannot be compiled, so it is not the code that you are running.

Please supply the actual code that you are using to implement the class (and, if you're using an IDE, ensure that it is actually compiling the code and not running a previous version of the executable because it can't compile the current code.)
Topic archived. No new replies allowed.