Getting const strings from input

I've got the following code (instructions are at the top) and I can't seem to work out how I would get the user input, then make than into a const char * data type so I can use strstr on it. Works fine when I provide the strings as a constant of course, but I need to convert a variable to a constant pointer...

Any hints you guys can offer maybe?

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
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{
	// Input a line of text and a search string.
	// Using strstr, locate the first occurrence of searchstring in text.
	// If the searchstring is found:
		// Assign the location to searchPtr (type char *).
		// Print the remainder of the line (beginning with searchPtr).
		// Use strstr again (using searchPtr + 1) to find the next
		// occurence of searchstring.
		// Print the remainder of the line again.

	char *Text;
	char *SearchString;

	cout << "\nInput a line of text to search: " << endl;
	cin >> Text;

	cout << "\n\nInput a search string to find: " << endl;
	cin >> SearchString;

	char *searchPtr = strstr(Text, SearchString);

	cout << searchPtr << endl;
	cout << strstr(searchPtr+1, SearchString);

	cout << endl;
	return 0;
}
strstr can be used with (non-const) char* arguments. The fact that it takes const char* simply means that it is guranteed that it won't modify your strings.

The problem here is that Test and SearchString are pointers. No memory is reserved to hold the user input, thus the lines cin >> Text; and cin >> SearchString; overwrite data that may be useful to your program and the program crashes as a result (if it didn't crach you simply were lucky. It could crash anytime!). A solution to this could be declaring Text and SearchString as arrays of characters, like this:

1
2
char Text[1024];
char SearchString[32];

Another thing is that using operator >> for input doesn't allow you to have spaces in the data inputted. Use getline instead, like this:

1
2
cin.getline(Text,1024,'\n');
cin.getline(SearchString,32'\n');

It should be fine now! :P
Last edited on
Thanks - that worked! I'm trying to modify it to accept multiple lines of text though and having a helluva time... Trying to use get to get the string and loop through a search like so:

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

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

int main()
{

	char Text[1024];
	char SearchString[32];

	cout << "\nInput a line of text to search: " << endl;			// Input a line of text and a search string.
	cin.get(Text, 1024, EOF);

	cout << "\n\nInput a search string to find: " << endl;
	cin.get(SearchString, 32);

	char *searchPtr;

	if (searchPtr = strstr(Text, SearchString))						// If you find the searchstring
	{
		cout << searchPtr << endl;									// Print the remainder of the line (beginning with searchPtr)

		while (searchPtr = strstr(searchPtr+1, SearchString))		// Use strstr again (using searchPtr + 1) to find 
		{															// subsequent occurences of searchstring.
			cout << searchPtr << endl;						
		}
	}
	cout << endl;



	cout << endl;
	cout << endl;
	return 0;
}


It compiles and runs but after I input the search strings and do a CTRL+z, nothing happens. I do another CTRL+z and I get an eruption of garbage:

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


Input a line of text to search:
asd asdasd asdasd asdasd
asdasdasd asdasdad ^Z
^Z


Input a search string to find:
asd asdasd asdasd asdasd
asdasdasd asdasdad →
sd asdasd asdasd asdasd
asdasdasd asdasdad →
d asdasd asdasd asdasd
asdasdasd asdasdad →
 asdasd asdasd asdasd
asdasdasd asdasdad →
asdasd asdasd asdasd
asdasdasd asdasdad →
sdasd asdasd asdasd
asdasdasd asdasdad →
dasd asdasd asdasd
asdasdasd asdasdad →
asd asdasd asdasd
asdasdasd asdasdad →
sd asdasd asdasd
asdasdasd asdasdad →
d asdasd asdasd
asdasdasd asdasdad →
 asdasd asdasd
asdasdasd asdasdad →
asdasd asdasd
asdasdasd asdasdad →
sdasd asdasd
asdasdasd asdasdad →
dasd asdasd
asdasdasd asdasdad →
asd asdasd
asdasdasd asdasdad →
sd asdasd
asdasdasd asdasdad →
d asdasd
asdasdasd asdasdad →
 asdasd
asdasdasd asdasdad →
asdasd
asdasdasd asdasdad →
sdasd
asdasdasd asdasdad →
dasd
asdasdasd asdasdad →
asd
asdasdasd asdasdad →
sd
asdasdasd asdasdad →
d
asdasdasd asdasdad →

asdasdasd asdasdad →

asdasdasd asdasdad →
asdasdasd asdasdad →
sdasdasd asdasdad →
dasdasd asdasdad →
asdasd asdasdad →
sdasd asdasdad →
dasd asdasdad →
asd asdasdad →
sd asdasdad →
d asdasdad →
 asdasdad →
asdasdad →
sdasdad →
dasdad →
asdad →
sdad →
dad →
ad →
d →
 →
→

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠8Ѽ ╘°+
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠^CPress a
ny key to continue . . .


What the heck? Why doesn't it stop at my CTRL+z and wait for another input?
Found a way to get multiple lines of text into a string but I can't use strstr on a string and can't convert it to a char *... Pulling my hair out 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
35

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

int main()
{


	string Text;
	string Body;
	char SearchString[32];

	cout << "Lines: " << endl;							// Enter lines of text
	while (Text != "end") // (int i = 1; i <= 10; i++)
	{
		cin >> Text;
		cout << Text << endl << endl;
		Body.append(Text);								// Append them together
	}

	string *BodyPtr = &Body;

	char *searchPtr = strstr(BodyPtr, &SearchString);	// <---- BAD ASSIGNMENT HERE

	while (searchPtr != NULL)
	{
		searchPtr = strstr(searchPtr + 1, SearchString);
		cout << searchPtr;
	}
char * searchPtr = strstr(Body.c_str(), SearchString);

Oh, and don't forget to also get SearchString from the user :P
Last edited on
Tried your line above and get:

1
2
3

cannot convert from 'const char *' to 'char *'


Is that c_str() function creating a constant? That seems to be forcing strstr to return a const instead of a char *.
wkwork wrote:
Is that c_str() function creating a constant? That seems to be forcing strstr to return a const instead of a char *.


Yes. c_str() returns const char*
change searchPtr to match
Sorry... my bad... fix it like guestgulkan suggests, i.e. like this:

const char * searchPtr = strstr(Body.c_str(), SearchString);
Last edited on
Topic archived. No new replies allowed.