input prolem

Hey, I was making a '\n' terminated string when I ran into a problem:
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
std::istream& operator >> (std::istream & in, ztring & z)
{
	GlobalFree ((HANDLE)z.buf);
	    char dumb;
	    int count=0;
	    char* buf;
	    while (dumb != '\n')
	    {
	        dumb=std::cin.get ();
	        if (dumb=='\n'&&count!=0)
	        {
	            char* buf2;
	            buf2=(char*)GlobalAlloc (NULL,count+1);
	            buf2=buf;
	            *(buf2+count+1)='\n';
	            z.buf=buf2;
	            GlobalFree ((HANDLE)buf);
	            GlobalFree ((HANDLE)buf2);
	            break;
	        }
	        char* buf2 =(char*)GlobalAlloc (NULL,count+1);
	        int counter=count;
	        while (counter>0)
	        {
	        	*(buf2+counter)=*(buf+counter);
	        	std::cout<<"*(buf2+counter) = "<<*(buf2+counter)<<std::endl;
	        	counter--;
	        }
	        *(buf2+count+1)=dumb;
	        buf=buf2;
	        GlobalFree((HANDLE)buf2);
	        count++;
	    }
}

please enter a ztring: hello
*(buf2+counter) = 1
*(buf2+counter) = û
*(buf2+counter) = 1
*(buf2+counter) =
*(buf2+counter) = û
*(buf2+counter) = 1
*(buf2+counter) = ─
*(buf2+counter) =
*(buf2+counter) = û
*(buf2+counter) = 1
z=

As you can see, there is a problem here. could you help me identify it?
btw, z is the string you just enter so the output should be z=hello
Why don't you just wrap std::string instead? It would be easier.

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
#include <iostream>
#include <string>
using namespace std;

class String
{
    string str;
public:
    friend ostream & operator << (ostream & os, const String & str);
    friend istream & operator >> (istream & is, String & str);
    //...
};

ostream & operator << (ostream & os, const String & str)
{
    os << str.str;
    return os;
}

istream & operator >> (istream & is, String & str)
{
    getline(is,str.str);
    return is;
}

int main()
{
    String name;

    cout << "What's your name?\n";
    cin >> name;
    cout << "Hello, " << name << endl;

    return 0;
}
You shouldn't be using GlobalAlloc/GlobalFree without good reason. All the WIN32 functions that give you memory allocated with GlobalAlloc are like that because they're being backward compatible with WIN16, which had a different memory model.
@m4ster r0shi: Let's say I was crazy and DIDN'T want to use string in ANY way ok? what would fix this?
@kbw: what would you recommend that assigns blocks of memory to pointers?
sargon94 wrote:
what would you recommend that assigns blocks of memory to pointers?

http://cplusplus.com/doc/tutorial/dynamic/
Hello sargon94,

sargon94 wrote:
As you can see, there is a problem here. could you help me identify it?
sure...

The problem is here:
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
std::istream& operator >> (std::istream & in, ztring & z)
{
	GlobalFree ((HANDLE)z.buf);
	    char dumb;
	    int count=0;
	    char* buf;
	    while (dumb != '\n')
	    {
	        dumb=std::cin.get ();
	        if (dumb=='\n'&&count!=0)
	        {
	            char* buf2;
	            buf2=(char*)GlobalAlloc (NULL,count+1);
	            buf2=buf;
	            *(buf2+count+1)='\n';
	            z.buf=buf2;
	            GlobalFree ((HANDLE)buf);
	            GlobalFree ((HANDLE)buf2);
	            break;
	        }
	        char* buf2 =(char*)GlobalAlloc (NULL,count+1);
	        int counter=count;
	        while (counter>0)
	        {
	        	*(buf2+counter)=*(buf+counter); // <---- if((counter>0) and not_before(dumb == '\n')) buf = ???
	        	std::cout<<"*(buf2+counter) = "<<*(buf2+counter)<<std::endl;
	        	counter--;
	        }
	        *(buf2+count+1)=dumb;
	        buf=buf2;
	        GlobalFree((HANDLE)buf2);
	        count++;
	    }
}
Topic archived. No new replies allowed.