0

I have made a loop which should encrypt the phrases I tell it to, but didn't finish because of the problem. It should detect when I say "stop" in the console and shut down the loop. It doesn't work. What i want it to do is to detect if i said stop and break the loop. I shouldn t get any random missfires from getting the letters s t o p from other words. As you can see, every time there is a letter out of order, it resets the vectors which locks all of the ifs until 'c' gets the correct letters in the correct order.

using namespace std;

int main()
{
  char c,v[5];
  int i=0;
    while(i!=1)
    {
        cin.get(c);
        if(c=='s' or v[1]=='s')
        {
            v[1]='s';
            if(c=='t' or v[2]=='t')
            {
                v[2]='t';
                if(c=='o' or v[3]=='o')
                {
                    v[3]='o';
                    if(c=='p' or v[4]=='p')
                    {
                        v[4]='p';
                        v[1]=v[2]=v[3]=v[4]=0;
                        i=1;
                    }
                    else
                        v[1]=v[2]=v[3]=0;
                }
                else
                    v[1]=v[2]=0;
            }
            else
              v[1]=0;
        }
        cout<<c;
        if (i==1)
            break;
    }
    return 0;
  }
  • 3
    `c` is a single `char` that you read from user input. It cannot be `s`, `t`, `o` and `p` at the same time, but thats the only case where you modify `i` – 463035818_is_not_an_ai Nov 27 '19 at 17:38
  • it has to be only one time, each of the above letters. Once c becomes s, the first if is locked , then if it becomes t the second if locks open because of the vector becoming s until c becomes the wrong letter and resets the vectors – Amariei Sebastian Lucian Nov 27 '19 at 17:41
  • 3
    It can't "become" anything; you read one character then never change it – Lightness Races in Orbit Nov 27 '19 at 17:43
  • it is in a loop, it reads character by character, updating the vectors – Amariei Sebastian Lucian Nov 27 '19 at 17:44
  • 2
    for example, if c is 's' then the first if gets unlocked and makes the vector v[1] ='s' and the if remains open until c has the wrong value – Amariei Sebastian Lucian Nov 27 '19 at 17:45
  • Maybe you should edit your question and better explain what exactly you expect the behavior of your code to be. I know your logic can't possibly work but I am not 100% sure what you want to happen. – drescherjm Nov 27 '19 at 17:53
  • 2
    @drescherjm The array ("vector") holds the characters of "stop" that have already been typed. Or rather, it's meant to. – user253751 Nov 27 '19 at 17:55
  • 2
    You type `s` and now c contains 's'. The computer checks whether c contains 's'. It does so it runs the if statement. The computer sets `v[1]='s';`. The computer then checks whether c contains 't' or v[2] contains 't'. It does not so the computer runs the 'else' statement which sets `v[1]=0;` again. End result: nothing happens. – user253751 Nov 27 '19 at 17:57
  • 1
    Also `v` is never initialized, so it contains random values. – Lukas-T Nov 27 '19 at 17:59
  • ah yes, thats true, i am starting to wonder why didn i see this until now – Amariei Sebastian Lucian Nov 27 '19 at 18:11
  • Do you just want to write code that checks if the characters 's', 't', 'o', and 'p' were entered in order followed by a single hit of the return key? Or do you have to have the return key pressed in between characters? Because there are easier ways to achieve what you're aiming for. – JohnFilleau Nov 27 '19 at 18:11
  • i added break in every if, it works now by stopping the chain when the correct value has been detected – Amariei Sebastian Lucian Nov 27 '19 at 18:14
  • it works now, thanks to everyone that helped me. – Amariei Sebastian Lucian Nov 27 '19 at 18:14

2 Answers2

2

That should the work and is not indented hell code. It assumes that you are entering one character at a time.

#include <iostream>

int main(int argc, char const *argv[])
{
    char keyword[] = "stop";
    char* matching_char = keyword;
    char char_from_user;
    while(*matching_char != '\0')
    {
        std::cin.get(char_from_user);
        // Reset if different character
        if(*matching_char != char_from_user)
            matching_char = keyword;

        // Increment position of match
        if(*matching_char == char_from_user)
            ++matching_char;
        // Ignore rest in buffer
        std::cin.ignore();
    }

    return 0;
}
bialy
  • 193
  • 2
  • 14
0

Following your logic, you just need to assign the v array values after each if/else condition otherwise it will just get immediately reassigned to 0. For example, you first assign v[1] = 's', and then right after you assign it to v[1] = 0, because the if returns false in first iteration. The following code should solve the problem.

#include <iostream>

using namespace std;

int main()
{
  char c,v[5];
  int i=0;
    while(i!=1)
    {
        cin.get(c);
        if(c=='s' || v[1]=='s')
        {
            if(c=='t' || v[2]=='t')
            {
                if(c=='o' || v[3]=='o')
                {
                    if(c=='p' || v[4]=='p')
                    {
                        v[4]='p';
                        v[1]=v[2]=v[3]=v[4]=0;
                        i=1;
                    }
                    else
                        v[1]=v[2]=v[3]=0;
                    v[3]='o';
                }
                else
                    v[1]=v[2]=0;
                v[2]='t';
            }
            else
              v[1]=0;
            v[1]='s';
        }
        if (i==1)
            break;
    }
    return 0;
  }
Saisai3396
  • 311
  • 1
  • 8