C++ forbids converting a `string` constant to `char*` – Alphabets to Morse converting program

So i was working on this assignment, i need to convert normal text into Morse code. We’re studying basic c++ at the moment so I’m not allowed to use the string data type or any other complex built-in functions.So I tried doing it through a char array. When i try running it,the following error shows up ” ISO C++ forbids converting a string constant to ‘char*’ “

#include <iostream>
using namespace std;

int len = 0;
char string[45] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', ',', '?', '[', '!', '(', ')', '&' };
char* morse[45] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", ".-.-.-", "--..--", "..--..", ".----.", "-.-.--", "-..-.", "-.--.", "-.--.-", ".-..." };

void size(char* arr)
{
    for (int i = 0; arr[i] != 0; i++) {
        len++;
    }
}

int main()
{
    char str[100];
    cout << "Enter string: ";
    cin.getline(str, 100);
    size(str);
    for (int i = 0; i < len; i++) {
        for (int j = 0; j < 45; j++) {
            if (str[i] == string[j]) {
                cout << morse[j];
                break;
            }
        }
    }
    return 0;
}

Leave a Comment