Too Many Initializers (Probably)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shock

    Too Many Initializers (Probably)

    Hi everybody,

    I have posted here before and got great help, so thanks in advance. I
    am working myself through deitel how to program c++ 4th edition and I
    am having a small problem with some code. Below is my code and the
    error message I am receiving. I am pretty sure the error is somewhere
    in the morsecode array, but not sure where. I am very frustrated.
    Any help is appreciated.

    // Exercise 5.47 - Chapter 5
    // Author: Corey Perkins
    // Description: Convert plaintext to morsecode

    #include <iostream>
    #include <cctype>

    using std::cin;
    using std::cout;
    using std::endl;

    int main()
    {

    char morsecode[36] = {"-----", // 0
    ".----", // 1
    "..---", // 2
    "...--", // 3
    "....-", // 4
    ".....", // 5
    "-....", // 6
    "--...", // 7
    "---..", // 8
    "----.", // 9
    ".-", // 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

    char alphabet[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    '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'};

    char plaintext[80];

    int where;

    while ( cin.getline (plaintext, 80, '\n'))
    {
    for ( int i = 0; i < strlen( plaintext ); i++ )
    {
    char c = toupper(plainte xt (i));
    if ( isupper (c) )
    {
    where = c - 'A' + 10;
    cout << morsecode[ where ] << " ";
    }
    else if ( isdigit (c) )
    {
    where = c - '0';
    cout << morsecode[ where ] << " ";
    }
    else if ( isspace (c) )
    {
    cout << ' ';
    }
    }
    cout << "\nNext:";
    }
    cout << endl;
    return 0;
    }
  • Kevin Goodsell

    #2
    Re: Too Many Initializers (Probably)

    Shock wrote:
    [color=blue]
    > Hi everybody,
    >
    > I have posted here before and got great help, so thanks in advance. I
    > am working myself through deitel how to program c++ 4th edition and I
    > am having a small problem with some code. Below is my code and the
    > error message I am receiving. I am pretty sure the error is somewhere
    > in the morsecode array, but not sure where. I am very frustrated.
    > Any help is appreciated.
    >
    > // Exercise 5.47 - Chapter 5
    > // Author: Corey Perkins
    > // Description: Convert plaintext to morsecode
    >
    > #include <iostream>
    > #include <cctype>
    >
    > using std::cin;
    > using std::cout;
    > using std::endl;
    >
    > int main()
    > {
    >
    > char morsecode[36] = {"-----", // 0
    > ".----", // 1
    > "..---", // 2
    > "...--", // 3[/color]

    <snip>

    morsecode is an array of char. It is only suitable for holding 36 chars,
    not for holding 36 other char arrays. Did you mean to use

    const char *morescode[36] = { /* ... */};

    ??

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • Gianni Mariani

      #3
      Re: Too Many Initializers (Probably)

      Shock wrote:[color=blue]
      > Hi everybody,
      >
      > I have posted here before and got great help, so thanks in advance. I
      > am working myself through deitel how to program c++ 4th edition and I
      > am having a small problem with some code. Below is my code and the
      > error message I am receiving. I am pretty sure the error is somewhere
      > in the morsecode array, but not sure where. I am very frustrated.
      > Any help is appreciated.
      >
      > // Exercise 5.47 - Chapter 5
      > // Author: Corey Perkins
      > // Description: Convert plaintext to morsecode
      >
      > #include <iostream>
      > #include <cctype>
      >
      > using std::cin;
      > using std::cout;
      > using std::endl;
      >
      > int main()
      > {
      >
      > char morsecode[36] = {"-----", // 0[/color]

      This should really be:

      const char * morsecode[36]

      [color=blue]
      > ".----", // 1
      > "..---", // 2
      > "...--", // 3
      > "....-", // 4
      > ".....", // 5
      > "-....", // 6
      > "--...", // 7
      > "---..", // 8
      > "----.", // 9
      > ".-", // 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
      >
      > char alphabet[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
      > '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'};
      >
      > char plaintext[80];
      >
      > int where;
      >
      > while ( cin.getline (plaintext, 80, '\n'))
      > {
      > for ( int i = 0; i < strlen( plaintext ); i++ )
      > {
      > char c = toupper(plainte xt (i));[/color]

      You really want

      plaintext[ i ] .... not plaintext(i)
      [color=blue]
      > if ( isupper (c) )
      > {
      > where = c - 'A' + 10;
      > cout << morsecode[ where ] << " ";
      > }
      > else if ( isdigit (c) )
      > {
      > where = c - '0';
      > cout << morsecode[ where ] << " ";
      > }
      > else if ( isspace (c) )
      > {
      > cout << ' ';
      > }
      > }
      > cout << "\nNext:";
      > }
      > cout << endl;
      > return 0;
      > }[/color]

      Comment

      • jeffc

        #4
        Re: Too Many Initializers (Probably)


        "Shock" <shock56@charte r.net> wrote in message
        news:ef0f2e3d.0 309221607.4fe1d 029@posting.goo gle.com...[color=blue]
        > Hi everybody,
        >
        > I have posted here before and got great help, so thanks in advance. I
        > am working myself through deitel how to program c++ 4th edition and I
        > am having a small problem with some code. Below is my code and the
        > error message I am receiving. I am pretty sure the error is somewhere
        > in the morsecode array, but not sure where. I am very frustrated.
        > Any help is appreciated.
        >
        > // Exercise 5.47 - Chapter 5
        > // Author: Corey Perkins
        > // Description: Convert plaintext to morsecode
        >
        > #include <iostream>
        > #include <cctype>
        >
        > using std::cin;
        > using std::cout;
        > using std::endl;
        >
        > int main()
        > {
        >
        > char morsecode[36] = {"-----", // 0
        > ".----", // 1
        > "..---", // 2
        > "...--", // 3
        > "....-", // 4
        > ".....", // 5
        > "-....", // 6
        > "--...", // 7[/color]

        You don't say what your error is, but you've defined an array of 36
        characters. At this point (//7), you're already up to 40 characters in
        there.


        Comment

        • Victor Bazarov

          #5
          Re: Too Many Initializers (Probably)

          "jeffc" <nobody@nowhere .com> wrote...[color=blue]
          >
          > "Shock" <shock56@charte r.net> wrote in message
          > news:ef0f2e3d.0 309221607.4fe1d 029@posting.goo gle.com...[color=green]
          > > Hi everybody,
          > >
          > > I have posted here before and got great help, so thanks in advance. I
          > > am working myself through deitel how to program c++ 4th edition and I
          > > am having a small problem with some code. Below is my code and the
          > > error message I am receiving. I am pretty sure the error is somewhere
          > > in the morsecode array, but not sure where. I am very frustrated.
          > > Any help is appreciated.
          > >
          > > // Exercise 5.47 - Chapter 5
          > > // Author: Corey Perkins
          > > // Description: Convert plaintext to morsecode
          > >
          > > #include <iostream>
          > > #include <cctype>
          > >
          > > using std::cin;
          > > using std::cout;
          > > using std::endl;
          > >
          > > int main()
          > > {
          > >
          > > char morsecode[36] = {"-----", // 0
          > > ".----", // 1
          > > "..---", // 2
          > > "...--", // 3
          > > "....-", // 4
          > > ".....", // 5
          > > "-....", // 6
          > > "--...", // 7[/color]
          >
          > You don't say what your error is, but you've defined an array of 36
          > characters. At this point (//7),[/color]

          Actually, at '// 0' point (or, rather, before it, at the comma) he
          has initialised the array (from the "-----" literal). Any comma is
          a syntax error. What he needs is to declare 'morsecode' as

          char * morsecode[] = { ...

          (an array of pointers to char).
          [color=blue]
          > you're already up to 40 characters in
          > there.[/color]

          Victor


          Comment

          • jeffc

            #6
            Re: Too Many Initializers (Probably)


            "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message
            news:zaZbb.5571 53$o%2.243135@s ccrnsc02...[color=blue][color=green][color=darkred]
            > > >
            > > > char morsecode[36] = {"-----", // 0
            > > > ".----", // 1
            > > > "..---", // 2
            > > > "...--", // 3
            > > > "....-", // 4
            > > > ".....", // 5
            > > > "-....", // 6
            > > > "--...", // 7[/color]
            > >
            > > You don't say what your error is, but you've defined an array of 36
            > > characters. At this point (//7),[/color]
            >
            > Actually, at '// 0' point (or, rather, before it, at the comma) he
            > has initialised the array (from the "-----" literal). Any comma is
            > a syntax error.[/color]

            True, but I was just trying to give him a hint.....


            Comment

            • Shock

              #7
              Re: Too Many Initializers (Probably)

              Gianni Mariani <gi2nospam@mari ani.ws> wrote in message news:<bko3fj$99 8@dispatch.conc entric.net>...[color=blue]
              > Shock wrote:[color=green]
              > > Hi everybody,
              > >
              > > I have posted here before and got great help, so thanks in advance. I
              > > am working myself through deitel how to program c++ 4th edition and I
              > > am having a small problem with some code. Below is my code and the
              > > error message I am receiving. I am pretty sure the error is somewhere
              > > in the morsecode array, but not sure where. I am very frustrated.
              > > Any help is appreciated.
              > >
              > > // Exercise 5.47 - Chapter 5
              > > // Author: Corey Perkins
              > > // Description: Convert plaintext to morsecode
              > >
              > > #include <iostream>
              > > #include <cctype>
              > >
              > > using std::cin;
              > > using std::cout;
              > > using std::endl;
              > >
              > > int main()
              > > {
              > >
              > > char morsecode[36] = {"-----", // 0[/color]
              >
              > This should really be:
              >
              > const char * morsecode[36]
              >
              >[color=green]
              > > ".----", // 1
              > > "..---", // 2
              > > "...--", // 3
              > > "....-", // 4
              > > ".....", // 5
              > > "-....", // 6
              > > "--...", // 7
              > > "---..", // 8
              > > "----.", // 9
              > > ".-", // 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
              > >
              > > char alphabet[36] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
              > > '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'};
              > >
              > > char plaintext[80];
              > >
              > > int where;
              > >
              > > while ( cin.getline (plaintext, 80, '\n'))
              > > {
              > > for ( int i = 0; i < strlen( plaintext ); i++ )
              > > {
              > > char c = toupper(plainte xt (i));[/color]
              >
              > You really want
              >
              > plaintext[ i ] .... not plaintext(i)
              >[color=green]
              > > if ( isupper (c) )
              > > {
              > > where = c - 'A' + 10;
              > > cout << morsecode[ where ] << " ";
              > > }
              > > else if ( isdigit (c) )
              > > {
              > > where = c - '0';
              > > cout << morsecode[ where ] << " ";
              > > }
              > > else if ( isspace (c) )
              > > {
              > > cout << ' ';
              > > }
              > > }[/color]
              > cout << "\nNext:";[color=green]
              > > }
              > > cout << endl;
              > > return 0;
              > > }[/color][/color]


              Thanks guys, you hit the nails on the heads. Program is running fine now.

              Corey

              Comment

              Working...