Sign in to follow this  
toogreat4u

Problems with overloaded

Recommended Posts

toogreat4u    127
I am trying to make a StringVar class that only reads in one word of input using a standard iostream.
istream& operator >>(istream& ins, StringVar& the_string)
{
	char input;
	int index = 0;
	bool word = true;

	while(word == true)
	{
		ins >> input;
		
		if(input == ' ' || input == '\n')
		{
		        word = false;
		}
		else
		{
			the_string.value[index] = input;
			index++;
		}
	}
	return ins;
}
The "the_string.value[index]" is char*, whenever I test this problem I don't ever escape the while loop. If someone can get me going in the right direction that would be great.

Share this post


Link to post
Share on other sites
jyk    2094
1. You don't need to write 'while(word == true)'; you can just write 'while (word)'.

2. Is the memory for value allocated dynamically? If so, how do you know how much memory to allocate?

3. Have you stepped through the code using a debugger to see what characters are being read, and why the loop is not terminating?

4. It looks like you're trying to duplicate functionality already offered by std::string and the SC++L 'stream' classes. Is there a particular reason for this? Or are you just not sure how to extract text a word at a time from an input stream?

Share this post


Link to post
Share on other sites
toogreat4u    127
Yes the memory is allocated dynamically:
Here is the class structure for StringVar


/* Header file strvar.h: This is the implementation for hte class StringVar whose values are strings.
* An object is declared as follows.
* Note that you use (max_size), not [max_size]
* StringVar the_object(max_size);
* where max_size is the longest string length allowed.
*/

#ifndef STRVAR_H
#define STRVAR_H
using namespace std;

class StringVar
{
public:
StringVar(int size);
/* Initializes the object so it can accept string values up to size in length.
* Sets the value of the object equal to the empty string.
*/

StringVar();
/* Initializes the object so it can accep tstring vlaues of length 100 or less.
* Setst he value of the ojbect equal to the empty string.
*/

StringVar(const char a[]);
/* Precondition: The array a contains characters terminated with '\0'.
* Initializes the boject so tis value is the string stored in a so that it can later be set to string values up to strlen(a)
* in length
*/

StringVar(const StringVar& string_object);
// Copy constructor

~StringVar();
// Returns all the dynamic memroy used by the object to the freestore.

void StringVar::operator =(const StringVar& right_side);
// overloads the assignment operator

friend bool operator ==(const StringVar& string1, const StringVar& string2);
// only the string values have to be equal, max_length need not be the same

friend StringVar operator +(const StringVar& string1, const StringVar& string2);
// performs the concatenation of strings of type StringVar

friend void copy_piece(StringVar& target, const StringVar& source, int number);
// returns a specified substring

char one_char(int index) const;
// returns a specified single character

void set_char(int index, char replace);
// changes a specified character

int length() const;
// Returns the lenght of the current string value.

void input_line(istream& ins);
/* Precondition: If ins is a file input stream, then ins has been connected to a file.
* Action: The next text in the input stream ins, up to '\n', is coped to the calling object.
* If there is not sufficient room, then only as much as will fit is copied.
*/

friend ostream& operator <<(ostream& outs, const StringVar& the_string);
/* Overloads the << operator so it can be used to output values of type StringVar
* Precondition: If outs is a file output stream, then outs has already been connected to a file.
*/

friend istream& operator >>(istream& ins, StringVar& the_string);
/* Overloads the >> operator so it can be used to input values of type StringVar
* Reads only a single world rather than an entire line
*/

private:
char* value; // pointer to dynamic array that holds the string value.
int max_length; // declare max length of any string value.
};

#endif //STRVAR_H


I know in order to extract a word I need to be looking for either a space character ' ' or a newline character '\n'.

Share this post


Link to post
Share on other sites
jyk    2094
You still haven't answered questions 3 and 4 :)

Most if not all of the functionality you've shown in your header file is covered in one form or another by std::string, the SC++L stream classes, and a few SC++L free functions. If it's the functionality that you're after, then learn about and use the aforementioned tools from the standard library. If on the other hand this is intended to be a learning exercise of some sort, you might post the accompanying .cpp file so that we can take a look at it. (Oh, and by the way, don't use 'using' directive or declarations in header files - it pollutes the global namespace for any file that includes that header, either directly or indirectly.)

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by toogreat4u
I am trying to make a StringVar class that only reads in one word of input using a standard iostream.


Why? This is already exactly what happens if you read into a std::string object with operator>>.

Share this post


Link to post
Share on other sites
toogreat4u    127
Wow I can't believe I whiffed this but all I needed to do was have ins >> the_string.value; as the operation inside of the friend function just as zahlman said :)

Share this post


Link to post
Share on other sites
SiCrane    11839
And opened up your program to a buffer overrun in the process. Then again, your original code has the buffer overrun as well, so I guess that isn't really a change.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this