Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

14 Experts available now in Live!

Get 1:1 Help Now