ROT13 encrypter help

I'm new to programming in c++, but i do have two years of experience in python, but I am moving into c++ because i think it's better for the field of work I want to do. But a problem I am having with c++ is that the errors are really complicating. I ran this program and i get so many errors. I know that usually it's one error that results in another error so fixing the original error will fix the other errors. But this made it so that i have no clue on what needed to be fixed, because A. a lot of reading and B. It could be any of them. If someone could please help me on what i could do to better myself with debugging c++ code, and possibly give me a fix for my ROT13 encrypter, that would be awesome.

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
  #include <iostream>
#include <string>

char *alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "x", "z"};

int findAlphabetIndex(std::string letter)
{
  for(int i = 0; i <= (sizeof(alphabet)/sizeof(long)); i++)
  {
    if(alphabet[i] == letter)
    {
      return i;
    }
  }
}

std::string encrypt(std::string unencryptedPhrase)
{
  std::string encryptedPhrase;
  for(int i = 0; i <= unencryptedPhrase.length(); i++)
  {
    int index = findAlphabetIndex(unencryptedPhrase[i])
    int encryptedIndex = index += 13;
    if(encryptedIndex >= 26)
    {
      encryptedIndex -= 26;
    }
    encryptedPhrase += alphabet[encryptedIndex];
  }
  return encryptedPhrase;
}

int main()
{
  using namespace std;
  std::string phrase;
  cout << "->";
  cin >> phrase;
  cout << phrase << endl;
  cout << phrase.length() << endl;
  cout << encrypt(phrase) << endl;
  
}
also, i wanted to post the errors i'm getting so yeah

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
47
48
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
 char *alphabet[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "y", "x", "z"};
                                                                                                                                                     ^
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp:4:149: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
main.cpp: In function 'std::__cxx11::string encrypt(std::__cxx11::string)':
main.cpp:22:55: error: could not convert 'unencryptedPhrase.std::__cxx11::basic_string<char>::operator[](((std::__cxx11::basic_string<char>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}' to 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}'
     int index = findAlphabetIndex(unencryptedPhrase[i])
                                                       ^
main.cpp:24:8: error: 'encryptedIndex' was not declared in this scope
     if(encryptedIndex >= 26)
        ^~~~~~~~~~~~~~
main.cpp:24:8: note: suggested alternative: 'encryptedPhrase'
     if(encryptedIndex >= 26)
        ^~~~~~~~~~~~~~
        encryptedPhrase
main.cpp:28:33: error: 'encryptedIndex' was not declared in this scope
     encryptedPhrase += alphabet[encryptedIndex];
                                 ^~~~~~~~~~~~~~
main.cpp:28:33: note: suggested alternative: 'encryptedPhrase'
     encryptedPhrase += alphabet[encryptedIndex];
                                 ^~~~~~~~~~~~~~
                                 encryptedPhrase

exit status 1


for me this is like reading a language i've never even known existed.
Last edited on
never mind this post, i found the answer after looking back and taking a look at my code, it was just that i was taking the char from the alphabet array and putting it into my function that takes a string
It takes a while to learn to read the error messages, but once you 'get' it, they tell you exactly what to do.

Alphabet is at least part of your woes. I would have made it a string and set it to "abcdef ..."
rather than a char array. But that is just cleanup, it will work if you fixed it another way.

If you are aware of the ascii table.... you don't even need alphabet. You can just use the built-in ascii table as alphabet. Any lowercase letter - 'a' as an example... that formula gives 0 for 'a', 1 for 'b', etc. Then you don't need the index function.... the whole program becomes much less trouble.

But nice work figuring it out yourself and writing the code. You did well all around, working with what you knew and finding and fixing the issue.


Topic archived. No new replies allowed.