Solved

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

Posted on 2016-11-11
4
27 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now