error c2678 and c2065

Pages: 123
I am getting an error c2678 binary'>>': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)

Also i am getting an error c2065 'north':undeclared identifier.
if you cant tell by the code that i have and the "dialogue" I am trying to "move" to area 2 almost like they do in Zork. But i am having trouble finding the means to do so. Any help would be really appreciated.

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


#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <istream>
using namespace std;

int main()
{
	wchar_t direction;
	cout << "Welcome to a world of imagination. You are in Area 1." << endl
		<< "Go North to Area 2." << endl;
	cin >> direction;
	if (direction = north)
	{
		cout << "Good you made it to Area 2." << endl;
	}
	else
	{
		cout << "No you imbecile the only way out of Area 1 is to go North."
			<< endl;
	}

	system("PAUSE");
    return 0;
}

Last edited on
PS: I am new to coding so if this is a simple fix or it is just plain wrong I apologize for wasting peoples time :P
Hi,
Firstly,
You already include #include <iostream>, you don't need to include the following :
#include <istream>
Secondly,
You didn't declare the variable 'north'. But there is a quick way to fix it :
1
2
int north = 3;
if (direction == north)
Thirdly, change the variable type of 'direction'.
int direction;
Does that help? :)
closed account (E0p9LyTq)
A wchar_t can hold only one character.

To get a wchar_t you need to use std::wcin, not std::cin.
wcin >> direction;

= is the assignment operator, not the "equals" comparison operator (==).

Making a comparison with a wchar_t you need to use a wide char character constant.
if (direction == L"n" || direction == L"N")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
   std::cout << "Welcome to a world of imagination. You are in Area 1." << std::endl
      << "Go North to Area 2." << std::endl;
   wchar_t direction;
   std::wcin >> direction;
   
   if (direction == L'n' || direction == L'N')
   {
      std::cout << "Good you made it to Area 2." << std::endl;
   }
   else
   {
      std::cout << "No you imbecile the only way out of Area 1 is to go North."
         << std::endl;
   }
}


Welcome to a world of imagination. You are in Area 1.
Go North to Area 2.
n
Good you made it to Area 2.


Why are you using a wchar_t instead of a char?

Using a std::string would be better, the user can enter more than one character.

@Jao

If you are going to use wide characters, then you need to use std::wcout , std::wcin etc. If you are going to write to a file, then you need wofstream

http://en.cppreference.com/w/cpp/io/cout
http://en.cppreference.com/w/cpp/io/basic_ofstream

http://www.cplusplus.com/reference/iostream/wcin/?kw=wcin
http://www.cplusplus.com/reference/fstream/wofstream/?kw=wofstream
Yes that works. Also while i was waiting for reply I was fiddling with the code and changed the variable type to string and it worked as well. I understand i didnt have north declared but what i dont understand is why now is the error c2678 gone?
closed account (E0p9LyTq)
why now is the error c2678 gone?


std::string use regular one-byte characters, not wide characters. std::cout/std::cin are designed to work with regular std::strings.
closed account (E0p9LyTq)
@TheIdeasMan,

I forgot to mention std::wcout in my earlier comment, ooops!
Ok I get it now. I appreciate the help. Thank you all.
Glad it helped :)
Hi, there are other strings, here:

http://en.cppreference.com/w/cpp/string

Notably std::wstring

@FurryGuy

No worries - you picked up on all the other problems - so good work !
closed account (E0p9LyTq)
You didn't declare the variable 'north'. But there is a quick way to fix it :

And if north is supposed to be a string constant? By the context that would be a reasonable conjecture (direction being a character variable), so redefining to an integer variable could be a step back instead of merely enclosing north in quotes: "north",
@FurryGuy
Concerning your earlier post telling me if(direction == L'n' || direction == L'N') how would I apply that same outcome if i was to change wchar_t direction; to string direction;? Because as you said the user could input more than just a letter.
Last edited on
> How would I apply that same outcome if i was to change (wchar_t direction) to (string direction)?
You can use this :
if(direction[0] == L'n'|| direction[0] == L'N')
Or :
if(direction == "n"|| direction == "N")
Or even this :
if(direction == "north" || direction == "North")
Does that help? :)
Pages: 123