getline function

I am stuck on my getline function. I need it to output to a delimiter, and it does store up to the delimiter, but it continues to output.


This is the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
{
    int num= 0;
    char sp = ' ';
    
    char * t = new char[min];

    while (a.peek() != '\n')
    {
        a >> t[num];
		num++;
      
    return a;
}


How could i fix this to only output the words, and not the random characters as well?
Last edited on
What is xString and why are you not using std::string?
This is the function:

No, it's not. The code you've provided here will not compile under any circumstances, so it isn't the code that is producing your result.

I made a string class on my own using dynamic memory allocation and arrays of characters, xString is that class.

and it does compile.
Last edited on
Most likely, your custom string implementation has a mistake that results in memory corruption. Use std::string instead of reinventing the wheel.
My assignment calls for me to make my own string class, hence the lack of strings.
 
typedef string myString;
You should then look through your string class code for mistakes.
this is part of the xString.h file ... and it does compile.

I have no doubt it does. The code in the original post will not, thus it cannot be the function producing the result you described. It's difficult to tell you what your problem might be when we don't see code that actually produces the results described.

I suspect, however, that you are not nul-terminating your string.
You should really consider using std::string
iluv41 wrote:
My assignment calls for me to make my own string class, hence the lack of strings.
TuiCall wrote:
You should really consider using std::string
Unfortunately, not an option here.
closed account (SECMoG1T)
I would be willing to correct your getline if possible, post the definition of your class then i'll check see if it's possible to get it working.
Oh my bad,didn't see that.:)
Topic archived. No new replies allowed.