By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

end of Line peeking

P: 6
if(data.peek()=="\n")

I'm trying to use an if statement to break out of a for loop while looking for a end of line statement and that's the code I'm using now but I keep getting compiler error about comparison between a pointer and an integer

FYI: data is an outside text document
so any help would be appreciated
Sep 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: 44
You cant compare two strings using the '==' operator. To compare two strings you must use the function strcmp(string), from the string library. So, in your case it would be: if((data.peek()).strcmp("\n")).

Regards
Sep 16 '06 #2

P: 6
71 C:\Dev-Cpp\Lab 1.cpp `strcmp' is not a type

is the error message I got when I tryed implement your coding
Sep 16 '06 #3

P: 6
for(int sub=0;sub<20;sub++)
{
cout << studentNames[sub];
if((data.peek()).strcmp("\n"))
break;
else
char test=data.get();
if(test==correctAns[sub])
{
cout << "true\n";
}
else
{
cout << "false\n";
}
}

this is the entire for loop I'm using if that helps any
Sep 16 '06 #4

P: 44
Im sorry for induce you in error about the strcmp function. Try the following instead:

if(strcmp(data.peek(), "\n") == 0)
Sep 16 '06 #5

Banfa
Expert Mod 5K+
P: 8,916
if(data.peek()=="\n")

I'm trying to use an if statement to break out of a for loop while looking for a end of line statement and that's the code I'm using now but I keep getting compiler error about comparison between a pointer and an integer

FYI: data is an outside text document
so any help would be appreciated
The problem is that data.peek() returns an integer type (and int probably) which is the character peeked. However "\n" is a string and therefore of type char *. You need to use a character constant instead of a string constant,

if(data.peek()=='\n')

Note single quotes instead of double quotes.

If future when asking questions it is important to give all the information. Even in you code listing you have not given the type of data which makes it hard to know if the information I have looked up about peek is true. I have assumed that data is a derivertive of basic_stream.
Sep 17 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.