What is the output of the following code int main string ci

What is the output of the following code?

int main() {

   string cityState = \"Kansas City, Kansas.\";

   cout << cityState.find(\"City\");

   return 0;

}


why is the output 7? every time i input this code into xcode i get unknown type name \'string\' and use of undeclared idetifier \'cout\'

Solution

You have used string data type incorrectly. and error code: undeclared identifier \'cout\' is producing error because maybe you are compiling your code in a wrong compiler please make sure your compiler is right.

I am sharing with you a running code tested on ideone by selecting compiler c++ 5.1 you can run the code.

#include <iostream>
using namespace std;


int main() {
// char cityState [] = \"Kansas City, Kansas.\";
std::string str (\"Kansas City, Kansas.\");
std::string str2 (\"City\");
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << \"Your city is found at: \" << found << \'\ \';

// cout << cityState.find(\"City\");
// cout << \"Hello\";
return 0;
}

Output is 7 because if you count from starting of the string the first letter of the string which you are searching (C) is at index 7.

I hope this will help you but still if you have some doubt just comment below.

What is the output of the following code? int main() { string cityState = \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site