Question

#include<string>
#include<iostream>
using namespace std;

struct letter
{
    string lines[5];
}s[26];

int main()
{
    //struct letter s[25];
    s[0].lines =      // error while initializing here
    {
        "..*..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".*.*."
    };

    s[1].lines[5] =   // also if I do this
    {
        ".**..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".**.."
    };

  return 0;
}

Check here : Ideone Link

I can't figure out what is wrong with this code. I tried many ways. I even changed the structure member to 2d char array but getting same issue.Please do some help!

Compilation Info :

prog.cpp: In function 'int main()':
error: expected primary-expression before '{' token
error: expected ';' before '{' token
error: expected primary-expression before '{' token
error: expected ';' before '{' token

Was it helpful?

Solution

The way you are initializing the s[26] array is suitable only at declaration.

Here is how you can do it in your code:

struct letter
{
    string lines[5];
};

letter s[26] =
{
    {
        "..*..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".*.*."
    },
    {
        ".**..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".**.."
    }
};

Or even better, if you don't really need the letter structure:

string letter[26][5] =
{
    {
        "..*..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".*.*."
    },
    {
        ".**..",
        ".*.*.",
        ".***.",
        ".*.*.",
        ".**.."
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top