String Subscript out of range!

Okay, I'm sure you can figure out what I'm trying to do but I will briefly explain in case not. I'm converting user input to uppercase letters. Then trying to compare each letter to the alphabet array to get a location that corresponds with the morse code array. Right now I believe the subscript error is coming from the for loop of the getInput(); function but I could be way off. I realize this code is probably inefficient and very ugly but I am just starting to learn and am open for constructive criticism. Please help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>

using namespace std;

		// Global data. Each valid character and its equivalent
		// morse code are stored in the parallel arrays.
		const int NUM_CHARS = 40;
		char alpha[NUM_CHARS] = { ' ', ',', '.', '?', '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'
		};

		string morseCode[NUM_CHARS] = { " ", "--..--" , ".-.-.-" , "..--.." ,
		"-----", ".----", "..---", "...--",
		"....-", ".....", "-....", "--...",
		"---..", "----.", ".-", "-...",
		"-.-.", "-..", ".", "..-.",
		"--.", "....", "..", ".---",
		"-.-", ".-..", "--", "-.",
		"---", ".--.", "--.-", ".-.",
		"...", "-", "..-", "...-",
		".--", "-..-", "-.--", "--.."
		};

	
	string getInput();
	string textTOmorse();
	
	string temp;
	string upperTemp;
	int locations[NUM_CHARS];
	

int main()
{

	getInput();
	cout << upperTemp;
	textTOmorse();
	for(int index = 0; index < 39; index++)
	{
		cout << "This is the " << index << " location: ";
		cout << locations[index] << endl;
	}

	return 0;
}

string getInput()
{
	cout << "Enter your text to be stored: " << endl;

	getline(cin, temp);
		for(int index=0; temp[index] != 0; index++)
			{
				upperTemp += toupper(temp[index]); //Uppercases one letter at a time and copies to the temp1 string
			}	
		return temp;
}

string textTOmorse()
{
	int count = 0;
	for(int index=0; upperTemp[index] != 0; index++)
	{	
		if(upperTemp[count] != alpha[index])
		{
			continue;
		}else
			{ 
			locations[count] = index;
			count += 1;
			}
	}
	
	return 0;

}
closed account (Dy7SLyTq)
please post the errors
string makes no guarantee that the data that is stored is terminated with a '\0'.
Only c_str() guarantees a terminating null.

Change your loop at line 56 as follows:
 
  for(int index=0; index<temp.size(); index++)


Ditto for line 66.


Thanks for the info Abstraction!

1>c:\users\user\documents\visual studio 2010\projects\morsecodetranslator\morsecodetranslator\main.cpp(56): warning C4018: '<' : signed/unsigned mismatch
1>c:\users\user\documents\visual studio 2010\projects\morsecodetranslator\morsecodetranslator\main.cpp(66): warning C4018: '<' : signed/unsigned mismatch

This is what I'm getting after making those changes. I ran into this before and wasn't sure how to fix it as I don't understand the error.

Also the popup said, "Invalid Null pointer"
Last edited on
should have been
 
for (size_t index=0; index<temp.size(); index++) 


string.size() returns a size_t (unsigned) value, rather than an int.

Not sure where you're getting the "invalid null pointer" message.




Line 56 is a problem because the string is not null terminated

On line 60, you are returning temp, not Uppertemp.


Using global variables is usually a bad idea - at least declare them in main and pass references to them, to functions that need them. The char & morse arrays should also be const.

I would have done this with an array of structs -the struct holds a pair of the char & the morse string.

HTH
Thanks that fixed the two errors, but the invalid null pointer is still there.

Do let me know if you think of something that might be causing this. I will be fooling around with it for the next little bit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include <string>

using namespace std;

		struct str {
			char alpha;
			string morse;

		};

		// Global data. Each valid character and its equivalent
		// morse code are stored in the parallel arrays.
		const int NUM_CHARS = 40;
		const char alpha[NUM_CHARS] = { ' ', ',', '.', '?', '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'
		};

		const string morseCode[NUM_CHARS] = { " ", "--..--" , ".-.-.-" , "..--.." ,
		"-----", ".----", "..---", "...--",
		"....-", ".....", "-....", "--...",
		"---..", "----.", ".-", "-...",
		"-.-.", "-..", ".", "..-.",
		"--.", "....", "..", ".---",
		"-.-", ".-..", "--", "-.",
		"---", ".--.", "--.-", ".-.",
		"...", "-", "..-", "...-",
		".--", "-..-", "-.--", "--.."
		};

	
	string getInput();
	string textTOmorse();

int main()
{

	string temp;
	string upperTemp;
	int locations[NUM_CHARS];
	str strArray[NUM_CHARS]; 

	for(int i = 0; i < (NUM_CHARS-1); i++)
	{
		strArray[i].alpha = alpha[i];
		strArray[i].morse = morseCode[i];

	}

			for(int i = 0; i < NUM_CHARS; i++)
		{
			cout << strArray[i].alpha << "   =   " << strArray[i].morse << endl;

		}
			cout << endl;

	

	//getInput();
	//textTOmorse();
	//cout << upperTemp << endl;
	//cout << "These are the values stored in the location array: " << endl;

	//	for(int i = 0; i < NUM_CHARS; i++)
	//	{
	//		cout << locations[i] << endl;

	//	}

	//

	return 0;
}

//string getInput()
//{
//	cout << "Enter your text to be stored: " << endl;
//
//	getline(cin, temp);
//		for(size_t index=0; index<temp.size(); index++)
//			{
//				upperTemp += toupper(temp[index]); //Uppercases one letter at a time and copies to the temp1 string
//			}	
//		return upperTemp;
//}
//
//string textTOmorse()
//{
//	int count = 0;
//	for(size_t index=0; index<upperTemp.size(); index++)
//	{	
//		if(upperTemp[count] != alpha[index])
//		{
//			continue;
//		}else
//			{ 
//			locations[count] = index;
//			count += 1;
//			}
//	}
//	
//	return 0;
//
//} 


I'm in the process of trying what you suggested TheIdeasMan but when I ran this, testing to make sure everything matched, I get a strange character for 'Z' and the morse code does not show on that line either.

    =
,   =   --..--
.   =   .-.-.-
?   =   ..--..
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   =   -.--
╠   =

Press any key to continue . . .

Last edited on
Nevermind, it was in line 45.

Changed to for(int i = 0; i < NUM_CHARS; i++) and it works fine! xD
AbstractionAnon wrote:
string makes no guarantee that the data that is stored is terminated with a '\0'.
Only c_str() guarantees a terminating null.

You may want to check out
http://stackoverflow.com/questions/7554039/is-stringc-str-no-longer-null-terminated-in-c11
str strArray[NUM_CHARS];

should be:

struct str strArray[NUM_CHARS];

Unless you typedef it.

This:
for(int i = 0; i < (NUM_CHARS-1); i++)

Should be:

for(int i = 0; i < NUM_CHARS ; i++)

So you don't miss the last one - the 'Z', at the moment it is a garbage value.

Put lines 6 to 35 inside main so they are not global. Just make sure you send references to these, to any function that needs them. To do this, just put an ampersand (&) after the variable name in the parameter list of the function.

str is not a particularly good name for a variable.

I was meaning for you to have 1 array of structs, and not have the existing arrays - There isn't much point in storing the data twice. You can initialise the whole thing like you did with your original arrays - just need to put braces on each line around the items that make the struct:

1
2
3
4
5
6
7
8
9
10
11
12
13
struct MorseRecord {
	char alpha;
	string morse;

};


const struct MorseRecord MorseTable[NUM_CHARS] = {

    {' ', " "}
    {',' , "--..--"}
    ......
};



If you want to comment out lots of lines, you can use the C method which encloses the text in /* */ :

1
2
3
4
5
6
/*  <----start comment
getInput();
	textTOmorse();
	cout << upperTemp << endl;
	cout << "These are the values stored in the location array: " << endl;
*/ 


HTH
Thanks for your help guys!
Last edited on
Two problems:
1) You need a comma after each row in your initializer list
1
2
3
4
struct MorseStruct MorseTable[NUM_CHARS] = {
    { ' ',  " "},	
    { ',' , "--..--" },
... etc


2) You subscript is in the wrong place on your cout.
 
    cout << MorseTable[0].alpha;

You want to refer to the alpha element of the 0'th entry in the MorseTable.
Topic archived. No new replies allowed.