18

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;
}
Ilaya Raja S
  • 415
  • 5
  • 18
Zarish
  • 205
  • 1
  • 2
  • 5
  • 6
    That error shows up when you ***compile*** the program, not when you ***run*** it. The difference between compiling and running is very important to understand. – abelenky Feb 06 '17 at 17:57
  • 11
    `string` is not a great name for a variable in C++, particularly if you do something like `using namespace std;`. –  Feb 06 '17 at 17:58
  • 1
    Do not call your variable `string`, especially if you're including ``. Some compilers have remnants of `std::string` being used inside the `` headers or headers that `` includes. – PaulMcKenzie Feb 06 '17 at 18:00
  • 1
    @abelenky: That's why it's not a good idea to get NBs into the "press this button and run" IDE philosophy. – 3442 Feb 06 '17 at 18:10
  • @Zarish -- Off topic, but your solution is not optimal, as you are looping a maximum of 45 * length_of_string. If the string is 100 characters, you could be looping a maximum of 4500 times. What would usually be done is use a `std::map` that would map the character to the morse code symbol, so that you're not doing a one-by-one lookup to see which morse symbol matches the character. – PaulMcKenzie Feb 06 '17 at 18:27
  • You forgot to assigned the length of str to len – willll Feb 06 '17 at 18:29
  • @willll, The global `len` is initialized once, and `size` increments it. It will work once, but of course, there is a cleaner way to do it (reset `len` inside `size` before incrementing), or better yet, return the length from `size`. – donjuedo Feb 06 '17 at 19:33

2 Answers2

26

You're defining an array of char* objects. You initialize those pointers with string literals. But as the error explains, converting a string literal (called constant by the compiler) to char* is not allowed.

Solution: Converting a string literal to const char* is allowed, so you could declare an array of const char* instead. You don't appear to modify the strings pointed by the array, so this shouldn't be a problem.


PS. You've chosen to include a standard library header, and have chosen to use using namespace std; and defined an identifier with the same name as an identifier declared by the standard library (string). That will very likely be a problem for the compiler.

Solution: Do not use using namespace std.

Workaround: Come up with another variable name than string. The trick is to know all identifiers declared by the standard library. Since this trick isn't trivial and new identifiers will be added in future versions of the standard, I recommend the solution above instead.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • I changed the variable name from string to string1 and changed char* to const char*. **now** there are no errors. But i'm only asked to enter a string and it ends. I get no output. – Zarish Feb 06 '17 at 18:24
5

The issue, as described in the error, is that "...", for example, is a string constant and you are trying to assign to a non-constant char*. Instead you should be assigning to a const char*:

const char* morse[45] = { ".-" // ...
clcto
  • 9,530
  • 20
  • 42
  • I tried doing that. Now it says " reference to 'string' is ambiguous if (str[i]==string[j])" – Zarish Feb 06 '17 at 18:08
  • @Zarish that would be a different question, but my guess is that it is related to the comment by Neil Butterworth above. – clcto Feb 06 '17 at 18:12