why isnt this printing 500

#include <iostream>

using namespace std;

int main()

{
string input;

cout << "Enter the name!" << endl;
cin >> input;

if (input == "pick 5 hamburger meat") {
cout << 500 << endl;
}
return 0;
}
why isnt this printing 500

Logically, because this condition evaluates as false:
 
(input == "pick 5 hamburger meat")


More specifically, cin >> input; skips leading spaces, then stores non-whitepace characters into the string input until it reaches the first space or newline.

If you want to read a string containing spaces, use getline instead:
 
    getline(cin,input);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
        std::string input;

        std::cout << "Enter the name!\n";
        std::getline(std::cin, input);

        if (input = "pick 5 hamburger meat")
        {
                std::cout << 500 << '\n';
        }

        return 0;
}
Last edited on
Topic archived. No new replies allowed.