Too Many Initializers (Probably)

S

Shock

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(plaintext (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;
}
 
K

Kevin Goodsell

Shock said:
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

<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
 
G

Gianni Mariani

Shock said:
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

This should really be:

const char * morsecode[36]

".----", // 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(plaintext (i));

You really want

plaintext[ i ] .... not plaintext(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;
}
 
J

jeffc

Shock said:
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

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.
 
V

Victor Bazarov

jeffc said:
Shock said:
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

You don't say what your error is, but you've defined an array of 36
characters. At this point (//7),

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).
you're already up to 40 characters in
there.

Victor
 
J

jeffc

Victor Bazarov said:
char morsecode[36] = {"-----", // 0
".----", // 1
"..---", // 2
"...--", // 3
"....-", // 4
".....", // 5
"-....", // 6
"--...", // 7

You don't say what your error is, but you've defined an array of 36
characters. At this point (//7),

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.

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

Shock

Gianni Mariani said:
Shock said:
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

This should really be:

const char * morsecode[36]

".----", // 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(plaintext (i));

You really want

plaintext[ i ] .... not plaintext(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;
}


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

Corey
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Blue J Ciphertext Program 2
Crossword 2
My Status, Ciphertext 2
Fibonacci 0
Lexical Analysis on C++ 1
How to play corresponding sound? 2
Remove Space, Stuck on lab 1
Cannot find my infinite loop 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top