0

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;
}
1
  • A='cs' is your problem. Look up the comparison operator. Commented Oct 19, 2014 at 3:30

2 Answers 2

4

'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" ) { }
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.