Why isn't this string if statement working?

Sep 11, 2014 at 8:54am
I could've sworn iv'e done this a million times

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
int main()
{
	std::cin >> reaction;

	if (reaction == "look at")
	{
		std::cout << "shackles\n";
		std::cout << "jail uniform\n";
		std::cout << "bench\n";
		std::cout << "cell door\n";
		std::cout << "jail bars\n";
		std::cout << "jail guard\n";
	}
}
Sep 11, 2014 at 9:02am
Where is reaction declared?
Sep 11, 2014 at 9:08am
sorry

#include <iostream>
#include <string>
int main()
{
std::string reaction = "none";

std::cin >> reaction;

if (reaction == "look at")
{
std::cout << "shackles\n";
std::cout << "jail uniform\n";
std::cout << "bench\n";
std::cout << "cell door\n";
std::cout << "jail bars\n";
std::cout << "jail guard\n";
}
}
Sep 11, 2014 at 9:13am
The >> operator only reads one word. If you want to read the whole line you could use std::getline.

http://www.cplusplus.com/reference/string/string/getline/
Sep 11, 2014 at 10:05am
That didnt work
Sep 11, 2014 at 10:08am
Just kidding
Topic archived. No new replies allowed.