Getting error when trying to make a cout and cin code

I am trying to make a text based game to help build my c ++ knowledge. I just started and I am already running into problems. When I build my code I get an error on the cin line. it says "|error: no match for 'operator<<' (operand types are 'std::istream {aka std::basic_istream<char>}' and 'int')| "
what have I done wrong? I am trying to ask the player there name and then have it print the name they entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
#include <string>

using namespace std;

int main()
{
    int yn;
    cout << "Enter name ";
    cin >> yn;
    cout << "entered" << yn;

}
Last edited on
* cin>>yn;

for cin, you use >> operator and for cout, you use <<.
Yes thank you I didn't even realize I did that. I guess I should call it quits for tonight and go to bed. lol
Enter name Help
entered0
Process returned 0 (0x0) execution time : 4.700 s
Press any key to continue.


Now I get this after I run the project.
Well, as we can't actually see the code you used to get this, that won't be much help.

If yn is an int, then what sort of value do you think you can input into it? Certainly not "Help". Maybe you intended a variable type of string (or, if you really only intended to input a y or a n, then a char would do).

I'm staggered it took 4.7s to run, though. Perhaps you're using Visual Studio.
Last edited on
Its the code from above. I am trying to have the user put in a name and then it print out the name the user chose. I just typed Help in the command box for the name.
Change
int yn;
to
string yn;
if you want to be able to put a string in it.

The name of the variable suggests that you might want a different type later, but this solves your immediate problem.
Have you ever tested input two words with a space between them?like "John Doe",if you use cin,you will find the output name is not as you expect,it will be John.In view of this problem,i suggest you using getline (cin, yn) instead of cin>>yn,then no matter how many words you input and how many space between every two words,the result will still as you expect.
Topic archived. No new replies allowed.