user2901565
user2901565

Reputation: 9

While Loop Character Sequence

Hi i am trying to create a program that reads in characters until the user enters the correct two character sequence (cs) to open the door. The input should be only c followed by s and also both characters. I am not sure where i am going wrong! Please help. Right now it allows access even when i enter a single word !

int main()
{
   char A;
   int done = 0;

   cout << "You have before you a closed door !" << endl;
   cin >> A;

   while (!done)
   {
      if (A='cs')
        break;

      else
        cin >> A;
   }

   cout << "Congratulations ! The door has opened !" << endl;

   return 0;
}

Upvotes: 0

Views: 135

Answers (2)

Arvind kr
Arvind kr

Reputation: 101

int main()
{
char A,B='cs';

cout << "You have before you a closed door !" << endl;
cin >> A;

while (1)
{
  if (A==B)
    break;

  else
    cin >> A;
}

cout << "Congratulations ! The door has opened !" << endl;

return 0;
}

You have done mistake in the comparison of the multicharacter constant.

Upvotes: 0

P0W
P0W

Reputation: 47794

'cs' is a multicharacter constant

And A='cs' is assignment not comparison, which also is not intended and incorrect

You should use std::string A;

and do comparison like following

if( A == "cs" ) { }

Upvotes: 4

Related Questions