Hi everyone,i have this program for an school assignment. the goal of program is that encodes the user name part of a given e-mail address and creates a password.

Degug Assertion Failed

Expression:String Subscript out of range

I couldn't find the wrong part.How can I fix this problem?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173`` `````` #include #include #include using namespace std; #define MAX_USERNAME_LENGTH 6 #define MAX_PROVIDER_LENGTH 3 string email; string username; string provider; string password = ""; map encoding; /* * Parses and returns back user name from given email * */ string getusername(string email) { int atpos = email.find('@', 0); return email.substr(0, atpos); } /* * Parses and returns back provider name from given email * */ string getprovider(string email) { int atpos = email.find('@', 0); int dotpos = email.find('.', 0); return email.substr(atpos + 1, dotpos - atpos - 1); } /* * Converts an input to lowercases * */ void tolower(string& input) { for(int i = 0; input[i]; i++) { if(input[i] >= 'A' && input[i] <= 'Z') input[i] = input[i] - 'A' + 'a'; } } /* * Checks whether given input contains given character. * */ bool containsChar(string input, char c) { for(int i = 0; input[i]; i++) { if(input[i] == c) { return true; } } return false; } /* * Checks whether given input contains only Alphanumerical characters. * */ bool containsOnlyEnglishAndDigit(string input) { for(int i = 0; input[i]; i++) { if(!((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z') || (input[i] >= '0' && input[i] <= '9'))) return false; } return true; } /* * Checks whether user name is correct. * */ bool isValidUsername(string input) { return input.length() >= MAX_USERNAME_LENGTH && containsOnlyEnglishAndDigit(input); } /* * Checks whether given input ends with .com or .edu * */ bool endsWithComorEdu(string input) { int len = input.length(); bool dotcom = input[len-1] == 'm' && input[len-2] == 'o' && input[len-3] == 'c' && input[len-4] == '.'; bool dotedu = input[len-1] == 'u' && input[len-2] == 'd' && input[len-3] == 'e' && input[len-4] == '.'; return dotcom || dotedu; } /* * Checks whether email address is correct * */ bool isValidEmailAddress(string input) { return containsChar(input, '@') && endsWithComorEdu(input); } /* * Checks wheter provider name is correct * */ bool isValidProvidername(string input) { return input.length() >= MAX_PROVIDER_LENGTH && containsOnlyEnglishAndDigit(input); } /* * Checks whether username or providername is correct * */ bool isCorrect(string username, string providername) { return isValidUsername(username) && isValidProvidername(providername); } /* * Read Email, parse Username and Providername out of it. * */ bool readEmail() { cin >> email; tolower(email); username = getusername(email); provider = getprovider(email); return !isCorrect(username, provider); } /* * Encodes username with given details in Project description. * */ void encodePart1() { for(int i = 0; username[i]; i++) { string s = encoding[username[i]]; if(!s.empty()) password += s; else password.append(1, username[i]); } } /* * Encoding part 2. * */ void encodePart2() { } int main() { encoding['a'] = "97"; encoding['e'] = "101"; encoding['i'] = "105"; encoding['o'] = "111"; encoding['u'] = "117"; while(readEmail()); encodePart1(); encodePart2(); cout << password << endl; return 0; }``````
Last edited on
VC++ doesn't like `str[str.size()]` even though it is permitted in C++11. Until they have fixed this bug you better write the loops another way.
For example this function is invalid

 ``12345678`` ``````void tolower(string& input) { for(int i = 0; input[i]; i++) { if(input[i] >= 'A' && input[i] <= 'Z') input[i] = input[i] - 'A' + 'a'; } }``````

It is not necessary that an object of type std::string has the terminating zero-byte. You should use member function size() as the upper limit of the loop. For example

 ``12345678`` ``````void tolower( string &input ) { for ( std::string::size_type i = 0; i < input.size(); i++) { if ( input[i] >= 'A' && input[i] <= 'Z' ) input[i] = input[i] - 'A' + 'a'; } }``````
Thanks a lot, I fixed my problem through yours guidence.
Hi everyone again. I want to help from you for my code writing. I want to write second encoding part on my program according to the task but I couldn't find proper way. Can anyone help me to write second encoding part?

I wrote first part, you can see my code above

Encoding Steps:
1. Replace any vowels (i.e., "a", "e", "i", "o", "u") in the user name with their ASCII values.
For example, if the user name is "canangunicen", then you have to replace "a" with "97", "u" with "117", "i" with "105", and lastly "e" with "101". Thus, the encoded version becomes "c97n97ng117n105c101n". After this process is done, you have one more step to finish the encoding.
2. When the first part of the encoding is finished, in the encoded string, you will have only digits and consonants (e.g., "c97n97ng117n105c101n"). Now, you have to encode the consonants as follows:
a. For each consonant in the encoded user name, if you can find the same character in the provider name, then you will replace it with the one that is on the left side of the found character in the provider name. For example:
user name = canangunicen provider name = sabanciuniv
After the vowels are replaced with their corresponding ASCII values, the encoded string will be the following:
c97n97ng117n105c101n
Then, you will replace the consonants according to their positions in the provider name. For instance, if you are processing 'c' currently, you would find the position of 'c' as 5 in the provider name. Thus, in the encoded string, you will replace 'c' with 'n', since 'n' is the character which is on the left side of 'c' in the provider name. At this point, the encoded string will become "n97n97ng117n105c101n".
If you find the character you are searching for at the 0th index in the provider name, then in the encoded string, you have to replace it with the last character of the provider name.
If the character you are searching for exists in multiple positions in the provider name, then you have to consider the one with the smallest index.
b. For any consonant in the user name, if you cannot find the same character in the provider name, then you will replace it with the first character of the provider name.
For example, since there is no 'g' in the provider name, the encoded string will become "n97a97as117n105c101n" after processing 'g' in the encoded user name.