Here is the code:

#include <iostream> #include <string> using namespace std; void verb() { cout << "verb" << endl; } void substantiv() { cout << "substantiv" << endl; } void adjectiv() { cout << "adjectiv" << endl; } int main() { do{ string intro; cout << "Scrie si vrei si stii: "; cin >> intro; switch (intro) { case (intro == "verb"): verb(); break; case (intro = "substantiv"): substantiv(); break; case (intro ="adjectiv"): adjectiv(); break; default: cout << "Nu am gasit nik :( scrii pi ok.ru Alin S si am si adaug" << endl; break; } /*if (intro == "verb") { verb(); }else if (intro == "substantiv") { substantiv(); }else if (intro == "adjectiv"); { adjectiv(); }*/ } while (true); system("pause"); return 0; } 

Closed due to the fact that it was off topic by Nicolas Chabanovsky Jan 3 '17 at 5:04 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nicolas Chabanovsky
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • and where did you learn to write case? and yes, = and == these are completely different things. - pavel

2 answers 2

Your code is incorrect in terms of syntax and grammar of the C ++ language.

You can use the following approach.

 #include <algorithm> #include <iterator> #include <string> #include <iostream> //... const char * words[] = { "verb", "substantiv", "adjectiv", }; enum Words : size_t { Verb, Substantiv, Adjectiv }; std::string s("verb"); auto it = std::find(std::begin(words), std::end(words), s); size_t n = std::distance(std::begin(words), it); switch ( n ) { case Verb: verb(); break; case Substantiv: substantiv(); break; case Adjectiv: adjectiv(); break; default: std::cout << "Nu am gasit nik :( scrii pi ok.ru Alin S si am si adaug" << std::endl; break; } 
  • thanks, I'm still new and haven't stooped in C ++, still learning - Alin Stirbu
  • @AlinStirbu Not at all. Ask more. :) - Vlad from Moscow

In C ++, the switch does not allow the use of a row for selection. But you can implement a certain analog switch for strings with the help of std::map . For example:

 #include <map> // ... const map<const string, void (*)()> functions = { {"verb", verb}, {"substantiv", substantiv}, {"adjectiv", adjectiv} }; do { cout << "Scrie si vrei si stii: "; string intro; cin >> intro; auto it = functions.find(intro); if (it != functions.end()) (*it->second)(); else cout << "Nu am gasit nik :( scrii pi ok.ru Alin S si am si adaug" << endl; } while (true);