-5

I'm trying to use std::cin >> but Visual Studio 2017 says:

"binary '>>': no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)"

Full code:

#include "stdafx.h"
#include <iostream>

void verb()
{
    std::cin >> "Enter a verb";
}



int main()
{
    std::cout << "help";
    return 0;
}

(The "help" is temporary until i can get void verb(); working.)

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Leo Brown
  • 39
  • 1
  • 6
  • 1
    `cin` is used for input. How is `cin` supposed to input into `"Enter a verb"`? – NathanOliver Mar 14 '17 at 17:38
  • That is not how you use `std::cin`. You are suppose to place a variable to the right of `>>` in which to store what the user provides. If you want to prompt the user, place a `std::cout` with your message before `std::cin`. – François Andrieux Mar 14 '17 at 17:38
  • Even if `std::cin >>` magically allowed you to print a prompt message as a side effect, which it does not, what would you expect to happen to the input after the user has pressed enter? Where should the string go? Where should it be stored? – Christian Hackl Mar 14 '17 at 17:42

3 Answers3

3

Reading input with std::cin

std::cin is used for input, and you have to store the value you want to read into a variable.

For example,

std::string word;
std::cin >> word;

std::cin >> word; will assign to word the word the user has entered. Hence, it makes no sense to pass a string literal ("hello" for example) to std::cin, because it won't know what to do with it.

If you want to show a message to the user to tell them to enter something, just use std::cout as you did to print your other message.

Other interesting things about std::cin

You may also use some other types such as int, float or others to use directly with std::cin.

Do note that when inputting a string with std::cin, it will only read one word (separated by whitespace), which means that if the user inputs hello world, word's value will be hello - if you do std::cin >> word; again, you will get world. To read a whole line from std::cin, refer to this thread.

If you want to read multiple things at once (to avoid putting std::cin >> a lot of times in your code), you can "chain" the inputs :

std::string word1, word2;
int number;
std::cin >> word1 >> number >> word2;

This will work as expected for an input such as firstword 443351 lastword.

asu
  • 1,875
  • 17
  • 27
  • I already figured this out but this gave me more information that will come in handy. Next time don't forget to say that you must have `#include ` – Leo Brown Mar 14 '17 at 18:36
  • That is correct, even though most implementations probably include `` with `` (but that is undefined behavior if I remember well). You can find some more info on cppreference [here for std::cin](http://en.cppreference.com/w/cpp/io/cin) and [here for std::istream](http://en.cppreference.com/w/cpp/io/basic_istream) – asu Mar 14 '17 at 18:42
1

You have to write it in a variable

#include "stdafx.h"
#include <iostream>
#include <string>

std::string variable_a;

void verb()
{
    std::cin >> variable_a;
}



int main()
{
     std::cout << "help";
     return 0;
}
dfsg76
  • 504
  • 1
  • 6
  • 22
-4
#include <iostream> // Include input/output stream objects
#include <string> // Include string data type
using namespace std; // Use standard namespace library

int main()
{
    string verb; // Declare verb as type string

    cout << "Enter a verb: "; // Asks the user to enter a verb
    cin >> verb; // Takes input as variable verb

   system("pause"); // Pauses console
   return 0; // Return value from function
}

Also, try this when you create a program in the future.

  1. Remove the '#include "stdafx.h"' and copy your original code.
  2. Start Visual Studio 2017.
  3. Under where it says 'New Project', choose the project type.
  4. When you get to the 'Win32 Application Wizard', click 'Next' and under 'Additional Options', click 'Empty Project'.
  5. You will now see a blank screen.
  6. Under the 'Project' menu tab, click 'Add New Item' (or Control + Shift + A)
  7. Copy your existing code into this project.

This should resolve the issue

  • 1
    I fail to see how this would resolve the issue. OP is having issues using `std::cin`, he doesn't know the correct syntax. He isn't asking how to compile source code under Windows. – François Andrieux Mar 14 '17 at 18:12
  • @FrançoisAndrieux True, although #include "stdafx.h" has given me many problems in the past and by removing it, the problems are fixed. Ill update my answer though. – FutureProgrammer Mar 14 '17 at 18:15