Problems with strings and spaces

Hello,

I've been trying to program a simple encryptor but i have some problems with the C-strings and spaces, can you help me find the mistake, because i can't.
When i try to encrypt sentences with several words it only work with the first one and leaves the rest untouched.

Below i leave you the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
int main()
{
    char s[100];                //Vector que contendrĂ¡ a la cadena a encriptar
    char a;                     //Caracter para opciones

    cout << "ingrese una palabra o palabra que desee encriptar (100 caracteres maximo):" << endl;
    cin.getline(s, 100);

    cout << endl
        << "Elija con que metodo quiere encriptarla:" << endl
        << "a) Encriptador del Cesar." << endl
        << "b) Cifrado Atbash." << endl;

    do{
        cin >> a;

        if (a != 'a' && a != 'b')
        {
            cout << "Opcion incorrecta, vuelva a intentar." << endl << endl;
        }
    }while(a != 'a' && a != 'b');

    if (a == 'a')
    {
        Encriptar_cesar(s);
        cout << s << endl << endl
        << "Desea recuperar su cadena(a) o salir del programa directamente(b)?" << endl;
        do{
            cin >> a;
            if (a != 'a' && a != 'b')
            {
                cout << "Opcion incorrecta, vuelva a intentar." << endl;
            }
            if (a == 'a')
            {
                Desencriptar_cesar(s);
                cout << s;
                return 0;
            }
            if (a == 'b')
            {
                return 0;
            }

        }while(a!='a' && a!='b');
    }



Here is the function that does the encryptation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void Encriptar_cesar(char s[])
{
    int i = 0;
    bool f = false;
    while(s[i] != '\0')
    {

        switch (s[i])
        {
            case 'x': s[i]='a';f=true;break;case 'y': s[i]='b';f=true;break;case 'z': s[i]='c';f=true;break;
            case 'X': s[i]='A';f=true;break;case 'Y': s[i]='B';break;f=true;case 'Z': s[i]='C';f=true;break;
            case ' ': f=true; break;
        }

        if(!f)
        {
            s[i] = s[i] + 3;
            f=false;
        }

        i++;
    }
}


I'm from Argentina, so the code is in spanish but i think it's understandable.

Thank you for all your help!
Last edited on
if (!f) evaluates to true when f is false, so when f is false, you set it to false and when f is true there is no way it can ever become false.
Last edited on
I see...i put the f=false; statement inside the if conditional when it should have been outside. What a stupid mistake :/

Thank you a lot for helping me solve this little problem.
I also suggest you clean up your switch statement. Putting so much on one line makes following the logic very difficult. I recommend putting everything on it's own line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   switch (s[i])
   {
      case 'x':
         s[i]='a';
         f=true;
         break;
      case 'y':
         s[i]='b';
         f=true;
         break;
      case 'z':
         s[i]='c';
         f=true;
         break;
      case 'X':
         s[i]='A';
         f=true;
         break;
      case 'Y':
         s[i]='B';
         break;
         f=true;
      case 'Z':
         s[i]='C';
         f=true;
         break;
      case ' ':
         f=true;
         break;
   }


I did it that way because the code was getting reaaaally long, and it was annoying to scroll a lot.

In a usual switch i'd do that but here there are lots of cases and very little code in each one, the same in fact. Thanks for the tip, i'm still noob.
Topic archived. No new replies allowed.