Solved

Error C2678: binary '!=': no operator found...

Posted on 2016-11-11
4
72 Views
Last Modified: 2016-11-12
Hello all.

I'm doing some basic work with C++ streams on MS Visual Studio 2015, and I am encountering error C2678 when I attempt to compile this code:

#include <iostream>

using namespace std;

int main()
{
	char ch;
	cout << "enter a phrase: ";
	while ( cin.get(ch) != 0 )
	{
		if (ch == '!')
			cin.putback('$');
		else
			cout << ch;
		while (cin.peek() == '#')
			cin.ignore(1, '#');
	}
	return 0;
}

Open in new window


For line 9, I receive this error output:
C2678      binary '!=': no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion)

Many thanks in advance!
0
Comment
Question by:blossompark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 41884329
According to this link, you are using a form of get that returns basic_istream& and I did not find a != operator that addresses a LHS of basic_istream& and a RHS of int.
http://www.cplusplus.com/reference/istream/basic_istream/get/

The revised while test condition should get your program to build.
But you still have a peek to deal with when you reach the EOF. Here is one approach. Adding in a test for EOF in a peek() allows your program to run smoothly in VS until you hit the eof state. At that point, to emulate an eof in the Visual Studio console, you type in the single char, ctrl/Z, followed by the <ENTER>, at which point you hit the break and your program ends.
	while (cin.get(ch) )
	{
		if (cin.peek() == std::char_traits<char>::eof())
			break;

Open in new window

or you can combine these three lines as follows:
	while( ( ch = cin.get() ) != std::char_traits<char>::eof() )

Open in new window

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
ID: 41884333
Just curious - I see that you just started C++ questions after a good deal of DB questions. Did you volunteer to take on new projects where you have to figure this stuff out?
0
 

Author Comment

by:blossompark
ID: 41884618
Thank you, it worked like a charm.

And, I'm actually borrowing this account at the moment. The DB questions aren't mine (though I hope to work my way through that subject as well!)

Many thanks.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41884808
I think that you can create your own account for free and ask your own questions for free. There is also a premium membership that gives you extra privileges.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question