ahmed0saber

Guard , thief and money Puzzle in C++

Apr 25th, 2021
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     string _input ;
  6.     bool _protected=false , money_reached , valid=false ;
  7.     again :
  8.     cout<<"Enter the input : ";
  9.     cin>>_input;
  10.     for(int i=0;i<_input.length();i++)
  11.     {
  12.         if(_input[i]=='$')
  13.         {
  14.             valid=true;
  15.             break;
  16.         }
  17.     }
  18.     if(!valid)
  19.     {
  20.         cout<<"Input must contain $\n";
  21.         goto again ;
  22.     }
  23.     for(int i=0;i<_input.length();i++)
  24.     {
  25.         if(_input[i]=='T')
  26.         {
  27.             _protected = false ;
  28.         }
  29.         else if(_input[i]=='G')
  30.         {
  31.             _protected = true ;
  32.         }
  33.         if(money_reached&&(_input[i]=='T'||_input[i]=='G'))
  34.         {
  35.             break ;
  36.         }
  37.         else if(_input[i]=='$')
  38.         {
  39.             if(_protected)
  40.             {
  41.                 cout<<"Protected from left";
  42.             }
  43.             else
  44.             {
  45.                 cout<<"Not Protected from left";
  46.             }
  47.             _protected = false ;
  48.             money_reached = true;
  49.         }
  50.     }
  51.     cout<<endl;
  52.     if(_protected)
  53.     {
  54.         cout<<"Protected from right";
  55.     }
  56.     else
  57.     {
  58.         cout<<"Not Protected from right";
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment