1 Error, why?

Using (Microsoft Visual Studio C++ 6.0) software, follow the sample output to write and run a program that reads one line of text and then uses a function called remove which will remove the blank spaces between the text for and then print.
The function prototype is as follow:
voidremove(char *s);

Sample Output:

Enter a line of text:
Your FUTURE is created by what you do TODAY not TOMORROW.

The text after removing the blank spaces is:
YourFUTUREiscreatedbywhatyoudoTODAYnotTOMORROW.


------------------------------------------------------------------------

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


#include<iostream>
using namespace std;

void remove(char *s);
int main()
{
char *s;
char *str;

cout<<"Enter a line of text: ";
cout<<endl;
cin.getline(str,100, '\n');
remove(&s);

return 0;

}


void remove(char *s)
{

char *ptr;
char *str;
while(*str != '\0')
{
	if(*str==' ')
	{
		ptr=str;

		do
		{
			*ptr=*(ptr+1);
			++ptr;
		} while(*(ptr-1) != '\0');

		--str;
	}
	++str;
}
}














Why when i call the function remove(&s) in the main it simply says error?

s is a char* (pointer to char) and that is the type that the remove function expects. &s gives you a char** (pointer to pointer to char) and that is not what you want.
 
remove(&s);


Another problem is that s does not yet point to anything. You need to make sure it points to some memory so that getline has somewhere to store the string.
Last edited on
Lets remove the unrelated code:
1
2
char * s;
remove( &s );

You have two errors here:
1. remove() takes a pointer, and 's' is a pointer. Address of a pointer is wrong type.
2. Even if you would pass the 's' correctly, you never did initialize that pointer.


MSVC6 was released 1998. Last millenium. It is ancient. Deprecated.
Which memory should s point to?

To the memory that you have allocated for the purpose.

For example: http://www.cplusplus.com/reference/istream/istream/getline/
i have the same error ! .. can you please tell us the right code so i can compare it with mine
How about now? still error...


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

#include<iostream>
using namespace std;
void remove(char *s);
int main()
{
char s[100];
char n;
cout<<"Enter a line of text: ";
cout<<endl;
cout<<endl;
cin.getline(s,100, '\n');
cout<<endl;
cout<<endl;
n = remove(s);
cout<<"The text after removing the blank spaces is: "<<n<<endl;
return 0;
}
void remove(char *s)
{
char *ptr;

while(s[100] != '\0')
{
	if(s[100]==' ')
	{
	ptr=s;

		do
		{*ptr=*(ptr+1);
			++ptr;
		} while(*(ptr-1) != '\0');

		--s;
	}
	++s;
}
}





There is no array[N] in an array that has N elements. Lines 23 and 25.


Hint: std::remove_if
Last edited on
There is no use still errors :''''( i give up.. what's the right code?
The "right code" does not benefit you in any way if you don't understand it.

You say that there are errors, but you don't show what they are. Learning to read error messages is useful, psychic ability to know the untold is rare.

std::remove_if has the "right code".
Topic archived. No new replies allowed.