cringyfudge420 cringyfudge420 - 1 year ago 133
C++ Question

C++ If-else statement using strings and comparing strings

when I enter (Im qwerty) as (y), the program shows "Your account has been deactivated" instead of "Your password is incorrect". I've searched for same problems but const. char and using strcmp is too complicated for me and my instructor does not use that kinds of codes.I'm very eager to know what must I do to make my program right. (Tnx in advance)

#include <iostream>

using namespace std;

int main () {
string y;
cout << "Enter Icode: ";
cin >> y;

if (y == "Im qwerty")
cout << "Your password is incorrect.";

cout << "Your account has been deactivated.";

return 0;


Answer Source

The problem is that cin >> y; reads one word, whereas there are two words in "Im qwerty". In other words, this program always outputs "Your account has been deactivated." because one word never matches two words.

If you would like to read multiple words, the easiest is to read an entire line, e.g. replace cin >> y; with getline(cin, y);.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download