Project Help-Please look my last reply

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?

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
  #include<iostream>
#include<string>
#include<map>

using namespace std;
#define MAX_USERNAME_LENGTH 6
#define MAX_PROVIDER_LENGTH 3

string email;
string username;
string provider;
string password = "";

map<char, string> 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

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
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.
Please help me guys I stuck in second encoding part.
Last edited on
Topic archived. No new replies allowed.