Reading a line into a pointer char

Hello guys,

I am working on a small assignment and I got it to compile but it is not returning what it is suppose to. I would like to read a line from the keyboard and assign a "const char" to it but I don't know what I am doing wrong. Here is the code I have:

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>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>

using namespace std;

int main()
{

	char firststring;
	char search;

	cout << "Enter the first sentence: " << endl;
	cin >> firststring;

	cout << "Enter the string you are looking for: " << endl;
	cin >> search;


	const char *string1 = &firststring;
	const char *string2 = &search;
	

	cout << "String1 = " << string1 << "\nstring2 = " << string2
		 << "\n\nThe remainder of string1 beginning with the\n"
		 << "first occurrence of string2 is: "
		 << strstr(string1, string2) << endl;

	return 0;

}


Can someone point out the mistake I am making?

Thanks,

Richard.

Last edited on
You cannot read a full sentence into a variable of type char. You need an array of chars. I would imagine this is something you are told in the first couple of sessions.

Alternatively, since you are using C++, you could use std::string, a String class that has all sorts of useful methods, including one called find() that does exactly what you want (and what the name implies).

In any case, to correct your code you must read sentences using std::string or char arrays (not single chars). Also don't use the extraction operator (>>) and instead use std::getline().
Jose,

thanks for the reply. I can't use fin() because the assignments requires me to use function strstr().

I made the following modifications:

1
2
3
4
5
6
	char firststring[80];
	char search;

	cout << "Enter the first sentence: " << endl;
	cin.getline(firststring, 80);


but when I try to compile it, it tells me

"error C2440: 'initializing' : cannot convert from 'char (*)[80]' to 'const char *'"

Before I posted here, I tried using a string instead of char but then I bumped into the same problem converting the string to a const char.

Any other sugestions?

If you intend to seach for a single character, then that is good, but if not, the 'search' variable also needs to be an array of chars.

Second, I told you to use std::getline(), not cin.getline(). The former is a global function while the latter is a member function of the cin variable. The one I speak of is described here: http://www.cplusplus.com/reference/string/getline/ . However, I just saw in this document that this global function only works with std::string objects, so alas, it was wrong of me telling you to use this function if you are required to use char arrays. My bad. Continue using cin.getline(), but use the overload that takes 3 parameters; the third argument should be '\n' so it stops reading when the user hits the ENTER key.

Finally, since firstString is now an array of chars, line 22 in your original code listing should just read const char *string1 = firstString;. The & operator is no longer required.
Topic archived. No new replies allowed.