String subscript out of range?
May 3, 2013 at 2:42am UTC
Hello, I am a beginner at C++ and I decided to make a translator-type of program that basically takes any message in english and outputs that phrase in binary. I still haven't implemented the decoding process yet, but when I ran it to test out the encoding process, it ran fine until I typed in a message and my compiler (visual studio 2010) displayed a pop-up window that stated:
"Debug Assertion Failed!
Expression: string subscript out of range"
What exactly does this mean and how can I fix this error? Any help is appreciated. Thanks!
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
string encoder(string notencoded);
string decoder(string notdecoded);
string binConvert(string englishmess, int howmany);
int main()
{
bool again = false ; //used to determine whether it should display option menu in loop
string encdec; //can be either encode or decode
cout << "Hello. This is an binary to english translator. Please choose whether you want to translate the message from english or binary (english/binary). If you wish to exit this program, please type in 'exit': \n" ;
while (true )
{
if (again == true )
cout << "If you would like to translate another message, please enter 'english', 'binary' or you may exit the program by typing in 'exit':\n" ;
getline(cin, encdec);
if (encdec == "english" )
{
string unraw;
string message;
cout << "Please enter the english message you would like to translate:\n" ;
getline(cin, unraw); //you can access the individual characters of a string using string_name[number] so I don't need a char array
message = encoder(unraw);
cout << "Your message has been successfully translated. Here it is: \n" << message << endl;
again = true ;
}
else if (encdec == "binary" )
{
string enraw;
string message;
cout << "Please enter the binary message you would like to translate:\n" ;
getline(cin, enraw);
message = decoder(enraw);
cout << "Your message has been successfully translated. Here it is: \n" << message << endl;
again = true ;
}
else if (encdec == "Exit" || encdec == "exit" )
break ;
else
{
cout << "Invalid response. Please type in 'english' or 'binary'. If you wish to exit this program, type 'exit'\n" ;
again = false ;
}
}
return 0;
}
string encoder(string notencoded)
{
int howmany;
string encodedmess, *point;
for (int amt = 0;; amt++)
{
if (notencoded[amt] == '\0' )
{
break ;
}
howmany = amt;
}
point = new string[howmany];
for (int n = 0; n < howmany; n++)
{
point[n] = binConvert(notencoded, howmany);
}
*point = encodedmess;
return (encodedmess);
}
string decoder(string notdecoded)
{
string decodedmess;
//do decoding stuff
return (decodedmess);
}
string binConvert(string englishmess, int howmany)
{
for (int i = 0; i < howmany; i++)
{
if (englishmess[i] == 'A' )
return ("01000001 " );
else if (englishmess[i] == 'B' )
return ("01000010 " );
else if (englishmess[i] == 'C' )
return ("01000011 " );
else if (englishmess[i] == 'D' )
return ("01000100 " );
else if (englishmess[i] == 'E' )
return ("01000101 " );
else if (englishmess[i] == 'F' )
return ("01000110 " );
else if (englishmess[i] == 'G' )
return ("01000111 " );
else if (englishmess[i] == 'H' )
return ("01001000 " );
else if (englishmess[i] == 'I' )
return ("01001001 " );
else if (englishmess[i] == 'J' )
return ("01001010 " );
else if (englishmess[i] == 'K' )
return ("01001011 " );
else if (englishmess[i] == 'L' )
return ("01001100 " );
else if (englishmess[i] == 'M' )
return ("01001101 " );
else if (englishmess[i] == 'N' )
return ("01001110 " );
else if (englishmess[i] == 'O' )
return ("01001111 " );
else if (englishmess[i] == 'P' )
return ("01010000 " );
else if (englishmess[i] == 'Q' )
return ("01010001 " );
else if (englishmess[i] == 'R' )
return ("01010010 " );
else if (englishmess[i] == 'S' )
return ("01010011 " );
else if (englishmess[i] == 'T' )
return ("01010100 " );
else if (englishmess[i] == 'U' )
return ("01010101 " );
else if (englishmess[i] == 'V' )
return ("01010110 " );
else if (englishmess[i] == 'W' )
return ("01010111 " );
else if (englishmess[i] == 'X' )
return ("01011000 " );
else if (englishmess[i] == 'Y' )
return ("01011001 " );
else if (englishmess[i] == 'Z' )
return ("01011010 " );
else if (englishmess[i] == 'a' )
return ("01100001 " );
else if (englishmess[i] == 'b' )
return ("01100010 " );
else if (englishmess[i] == 'c' )
return ("01100011 " );
else if (englishmess[i] == 'd' )
return ("01100100 " );
else if (englishmess[i] == 'e' )
return ("01100101 " );
else if (englishmess[i] == 'f' )
return ("01100110 " );
else if (englishmess[i] == 'g' )
return ("01100111 " );
else if (englishmess[i] == 'h' )
return ("01101000 " );
else if (englishmess[i] == 'i' )
return ("01101001 " );
else if (englishmess[i] == 'j' )
return ("01101010 " );
else if (englishmess[i] == 'k' )
return ("01101011 " );
else if (englishmess[i] == 'l' )
return ("01101100 " );
else if (englishmess[i] == 'm' )
return ("01101101 " );
else if (englishmess[i] == 'n' )
return ("01101110 " );
else if (englishmess[i] == 'o' )
return ("01101111 " );
else if (englishmess[i] == 'p' )
return ("01110000 " );
else if (englishmess[i] == 'q' )
return ("01110001 " );
else if (englishmess[i] == 'r' )
return ("01110010 " );
else if (englishmess[i] == 's' )
return ("01110011 " );
else if (englishmess[i] == 't' )
return ("01110100 " );
else if (englishmess[i] == 'u' )
return ("01110101 " );
else if (englishmess[i] == 'v' )
return ("01110110 " );
else if (englishmess[i] == 'w' )
return ("01110111 " );
else if (englishmess[i] == 'x' )
return ("01111000 " );
else if (englishmess[i] == 'y' )
return ("01111001 " );
else if (englishmess[i] == 'z' )
return ("01111010 " );
else if (englishmess[i] == ' ' )
return ("00000000 " );
}
}
Last edited on May 3, 2013 at 2:54am UTC
May 3, 2013 at 2:53am UTC
// you can access the individual characters of a string using string_name[number]
where
number
is in the range 0 to (string_name.size() - 1)
Your error is most likely an attempt to access a character beyond the end of the string.
Topic archived. No new replies allowed.