0

I'm creating a simple cipher and i got stuck on a weird problem with which i believe is with my IF's.

    char name[25];
    char cipher[25];
    gets(name);

    int i;
    for( i = 0; i <= 25; i++ )
     {
         cipher[i] = name[i];
         if( name[i] = 'g' ) {
            cipher[i] = 'a';
         };
         cout << cipher[i];
     };

cipher[i] = name[i]; is there to copy one array to another, so then only one array gets changed. It works. And rest of the program, in my understanding, works like this: if there is a letter g in name[0], make it a letter a in cipher[0] and if there isn't, just skip, repeat 25 times. But the results are so random to me i don't understand what's going on at all...

img

Dano
  • 55
  • 6
  • 2
    Assignment (`=`) is not equality (`==`) – crashmstr Nov 03 '20 at 19:37
  • 2
    Probably not related to your error, but `i <= 25;` will loop one too many times. – scohe001 Nov 03 '20 at 19:37
  • 1
    Outside of the typo, please [don't use `gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). It's so dangerous that it was removed completely in modern C++. – Yksisarvinen Nov 03 '20 at 19:40
  • 1
    @Yksisarvinen well, in my school we don't learn anything modern so that's alright i guess! teacher made us use this method... for real though i'll make sure to not use it in any serious program! – Dano Nov 03 '20 at 19:44
  • @scohe001 ah, didn't notice that, thank you! – Dano Nov 03 '20 at 19:44
  • @crashmstr it works... thank you! – Dano Nov 03 '20 at 19:45

1 Answers1

2

In your if statement, you're doing an assignment rather than comparison:

if( name[i] = 'g' ) should have == like so: if( name[i] == 'g' )

Kon
  • 4,023
  • 4
  • 24
  • 38
  • Honestly i don't know how to react to that now. I've tried it before and it didn't work, i was brute forcing anything that came to my mind, and now it just started to work... thank you! – Dano Nov 03 '20 at 19:43