Too many initializers for char b []

This is Morse code.
I get an error message too many initializers for char b[]. How can I get rid of this error?

#include<iostream>
using namespace std;

int main(){
    char a[72]={'A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h','I','i','J','j','K','k','L','l','M','m','N','n','O','o','P','p','Q','q','R','r','S','s','T','t','U','u','V','v','W','w','X','x','Y','y','z','Z','0','1','2','3','4','5','6','7','8','9','.',',','?','\'','!','/','(',')','&','@'};
    char b[]={".-",".-","-...","-...","-.-.","-.-.","-..","-..",".",".","..-.","..-.","--.","--.","....","....","..","..",".---",".---","-.-","-.-",".-..",".-..","--","--","-.","-.","---","---",".--.",".--.","--.-","--.-",".-.",".-.","...","...","-","-","..-","..-","...-","...-",".--",".--","-..-","-..-","-.--","-.--","--..","--..","-----","-----",".----",".----","..---","..---","...--","...--","....-","....-",".....",".....","-....","-....","--...","--...","---..","---..","----.","----.",".-.-.-",".-.-.-","--..--","--..--","..--..","..--..",".----.",".----.","-.-.-","-.-.--","-..-.","-..-.","-.--.","-.--.","-.--.-","-.--.-",".-...",".-..."};

    char c[40]; 
    cout<<"Enter code ";
    cin.getline(c,40);
    for(int i=0;i<1;i++){
        for(int j=0;j<72;j++){ 
            if(b[j]==c[i]){
                cout<<a[j];
            }
        }
    }
    return 0;
}
+4
source share
6 answers

You said that bthis is an array char. But you supply string literals, not single characters. It is impossible to understand what you really want to do. Perhaps you really want it to bbe an array of strings:

const char* b[] = {".-", ".-", "-...", "-...", ...};
+10
source

A chararray cannot contain strings! You must initialize it with an individual chars!

For instance: char b[] = { 'I', 'O', 'U' };

If you need string literals, you can use the following:

const char* b[] = { "II", "OO", "UU" };
+3
source

b const char*, char .

const char* b[]={".-",".-", ...}

char b[] = {'a', 'b', ...};
+2

You are not trying to save charin b, but strings. Declare bhow const char *b[72] = ....

+1
source

write down

static const char *a[72] =
{
    'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g',
    'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n',
    'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u',
    'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'z', 'Z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '.', ',', '?', '\'', '!', '/', '(', ')',
    '&', '@'
};

static const char *b[] =
{
    ".-", ".-", "-...", "-...", "-.-.", "-.-.", "-..", "-..", ".", ".",
    "..-.", "..-.", "--.", "--.", "....", "....", "..", "..", ".---", ".---",
    "-.-", "-.-", ".-..", ".-..", "--", "--", "-.", "-.", "---", "---",
    ".--.", ".--.", "--.-", "--.-", ".-.", ".-.", "...", "...", "-", "-",
    "..-", "..-", "...-", "...-", ".--", ".--", "-..-", "-..-", "-.--", "-.--",
    "--..", "--..", "-----", "-----", ".----", ".----", "..---", "..---", "...--",
    "...--", "....-", "....-", ".....", ".....", "-....", "-....",
    "--...", "--...", "---..", "---..", "----.", "----.", ".-.-.-",
    ".-.-.-", "--..--", "--..--", "..--..", "..--..", ".----.", ".----.",
    "-.-.-", "-.-.--", "-..-.", "-..-.", "-.--.", "-.--.", "-.--.-", "-.--.-",
    ".-...", ".-..."
};

I think this will help you ...

0
source
#include <iostream>
using namespace std;

int n = 10, m = 12;
char field[10][12] = {"W",".",".",".",".",".",".",".",".","W","W","."};

int main() {

}

I declared an array. but it returns the same error. I did not include the string among the characters. Anyone to explain what is wrong?

-1
source

All Articles