0

I'm new to coding (started yesterday) and I'm trying to do this thing where I get a custom output when I type in something specific.

#include <iostream>

using namespace std;

int main()
{

    if (cin >> "test") {

        cout << "test2";
    }

    system("pause");
    return 0;
}

C++ no operator matches these operands operand types are: std::istream >> const char [5]

melpomene
  • 84,125
  • 8
  • 85
  • 148
im very stupid
  • 31
  • 1
  • 1
  • 4
  • 5
    What exactly do you expect `cin >> "test"` to *do* ? – WhozCraig Jun 07 '19 at 15:29
  • How can you read input into a literal string? That doesn't make any sense. What are you trying to do? – Some programmer dude Jun 07 '19 at 15:29
  • 1
    maybe you meant `string whatever; cin >> whatever; if (whatever == "test") {...}`? – Federico klez Culloca Jun 07 '19 at 15:31
  • im trying to make the console give a custom response when you give it a specific word – im very stupid Jun 07 '19 at 15:33
  • While the above answers are fine, they teach you a bad habit, specifically `using namespace std;`, it's generally considered bad practice to have a `using namespace`. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice https://isocpp.org/wiki/faq/coding-standards#using-namespace-std https://stackoverflow.com/questions/2712076/how-to-use-an-iterator/2712125#2712125 (example of it causing problems) https://stackoverflow.com/questions/13402789/confusion-about-pointers-and-references-in-c (another example of it causing problems; using std::swap; would cau – JohnkaS Jun 07 '19 at 15:38
  • 1
    It seems like you're getting ahead of yourself. Get [a good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), and you should learn all you need for this exercise (and more). – Some programmer dude Jun 07 '19 at 21:11
  • see the problem is that im 14 and i got no money lol. – im very stupid Jun 08 '19 at 14:15

4 Answers4

3

You're trying to read input to a string literal.

If you want to output string test2 in case that string test is input, do the following

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    if (s == "test") {

        cout << "test2";
    }

    system("pause");
    return 0;
}
bobra
  • 615
  • 3
  • 18
1

There is a syntactic error in your code. Inside the if test case, you are declaring cin >> "test". But thats wrong.

If you want to test the user input, you need to create a variable, and read that variable with cin >> variable. Then, use that variable in the test case. It would be something like this:

#include <iostream>

using namespace std;

    int main()
    {
        String text;          //Here, you declare a variable of the type string(text)
        cin >> text;          //Here, you read the user input, for that variable
        if (text == "test") { //here you test if that variable equals "test"
            cout << "test2";
        }
        system("pause");
        return 0;
    }
  • in your code you're using String instead of string, which doesn't exist in c++, However, it exists in c++/cli, which is a version of C++ that runs under .NET CLI – Eugenio Miró Jul 28 '21 at 21:30
1

just add the header file #include<string> I hope this helps you.

0

std::cin is to input something in a variable (or some variables), but not for checking if the inputting thing is same to you want. If you want to do that, please input thing in a std::string variable, then use if to check.

Like this:

std::string str;
std::cin >> str;
if (str == "something") {
    //do something
}
Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160