Strings & Error Msg

Pages: 12
I'm trying to write a program to prompt a user to input a first and last name, and then alter the name that was entered. The program should first replace all the vowels with the letter 'z', and then take the original unaltered name and print it our backwards (so John Smith would be htimS nhoJ). I also put in a string length counter to help me make sure things were working (which they aren't).

It's not complete yet (I haven't done the backwards thing) but I am having some problems... When I run it I get an error message saying "Debug Assertion Failed" and "Expression: string subscript out of range". If I ignore that error, it does determine the string length and output it to the screen, but then I get another error message saying the same thing. I can't get past that point, because no matter how many times I ignore the error it keeps popping up. I don't know why I'm getting these messages... anyone have any ideas?

Here's the code:
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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string replace_vowels (int, string);
int length (string);

int main (void)
{
	string first_name;									
	string last_name;
	cout << "Please enter your first name: " << endl;
	cin >> first_name;
	cout << "Please enter your last name: " << endl;
	cin >> last_name;

	string temp = first_name + ' ' + last_name;				//concatenates strings including a space between the words.
	cout << "Here's what you entered: " << temp << endl;

	int string_length = length(temp);     // Calls function to determine the string length.
	cout << "The length of your string is " << string_length << "." << endl;
	
	string new_string1 = replace_vowels (string_length, temp);
	cout << "Here's your adjusted entry " << new_string1 << endl;

	system ("BREAK");
	return (0);
}

string replace_vowels (int length, string full_name)
{
	string replaced_letters;
	for (int i = 1; 1 < length; i++)
	{
		switch (full_name[i])
		{
			case 'a':
				replaced_letters[i] = 'z';
				break;
			case 'e':
				replaced_letters[i] = 'z';
				break;
			case 'i':
				replaced_letters[i] = 'z';
				break;
			case 'o':
				replaced_letters[i] = 'z';
				break;
			case 'u':
				replaced_letters[i] = 'z';
				break;
			default:
				replaced_letters[i] = full_name[i];
				break;
		}
	}
	return (full_name);
}

int length (string temp)
{
	int i = 0;
	do
	{
		i++;
	}
	while (temp [i] != '\0');
	return i;
}


Also, could I shorten the switch by using or statements in the case section? (for example -- case 'a' || 'e' || 'i' || 'o' || 'u')
on line 34 you write 1 insted of i
hentaiw is wrong. '\0' is a null character. It should work fine even with std::string.
hentaiw is wrong. '\0' is a null character. It should work fine even with std::string.

So ?
I compiled with for (int i = 1; i < length; i++) it still has same error.
Last edited on
temp [i] != '\0' with no '\0' in string class.

So temp[i] out of range
In replace_vowels replaced_letters is an empty string so in the switch you are accessing indexes out of bounds.
hentaiw, temp[temp.size()] gives you a reference to a char with value '\0' that you are not allowed to modify so it isn't a problem.
the string class has its own length function,why even bother ?

You (or I) may not know how string class is implemented.

@Peter87 : If you ever compile the code...it send the error after the

cout << "Here's what you entered: " << temp << endl;

so the length function got some problems...furthermore,I commented the replace_vowels out and the error still exists.

EDIT : Like this :

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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string replace_vowels (int, string);
int length (string);

int main (void)
{
	string first_name;									
	string last_name;
	cout << "Please enter your first name: " << endl;
	cin >> first_name;
	cout << "Please enter your last name: " << endl;
	cin >> last_name;

	string temp = first_name + " " + last_name;				//concatenates strings including a space between the words.
	cout << "Here's what you entered: " << temp << endl;

	int string_length = length(temp);     // Calls function to determine the string length.
	cout << "The length of your string is " << string_length << "." << endl;
	
	//string new_string1 = replace_vowels (string_length, temp);
	//cout << "Here's your adjusted entry " << new_string1 << endl;

	system ("BREAK");
	return (0);
}
/*
string replace_vowels (int length, string full_name)
{
	string replaced_letters;
	for (int i = 1; i < length; i++)
	{
		switch (full_name[i])
		{
			case 'a':
				replaced_letters[i] = 'z';
				break;
			case 'e':
				replaced_letters[i] = 'z';
				break;
			case 'i':
				replaced_letters[i] = 'z';
				break;
			case 'o':
				replaced_letters[i] = 'z';
				break;
			case 'u':
				replaced_letters[i] = 'z';
				break;
			default:
				replaced_letters[i] = full_name[i];
				break;
		}
	}
	return (full_name);
}
*/
int length (string temp)
{
	int i = 0;
	do
	{
		i++;
	}
	while (temp [i] != '\0');
	return i;
}


I may not good at supporting ,maybe you could teach me although i'm not the thread starter.
Last edited on
I have no problem with that code. I even run it through valgrind.
I have no problem with that code. I even run it through valgrind.


I got this error with VS2010.
What are you trying to do at line 68?
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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string replace_vowels (int, string);
int length (string);

int main (void)
{
	string first_name;									
	string last_name;
	cout << "Please enter your first name: " << endl;
	cin >> first_name;
	cout << "Please enter your last name: " << endl;
	cin >> last_name;

	string temp = first_name + ' ' + last_name;				//concatenates strings including a space between the words.
	cout << "Here's what you entered: " << temp << endl;

	int string_length = temp.length();     // Calls function to determine the string length.
	cout << "The length of your string is " << string_length << "." << endl;
	
	string new_string1 = replace_vowels (string_length, temp);
	cout << "Here's your adjusted entry " << new_string1 << endl;

	system ("BREAK");
	return (0);
}

string replace_vowels (int length, string full_name)
{
	string replaced_letters;
	for (int i = 0; i < length; i++)
	{
		switch (full_name[i])
		{
			case 'a':
				full_name[i] = 'z';
				break;
			case 'e':
				full_name[i] = 'z';
				break;
			case 'i':
				full_name[i] = 'z';
				break;
			case 'o':
				full_name[i] = 'z';
				break;
			case 'u':
				full_name[i] = 'z';
				break;
			default:
				full_name[i] = full_name[i];
				break;
		}
	}
	return (full_name);
}

int length (string temp) //BOGUS,for me
{
	int i = 0;
	do
	{
		i++;
	}
	while (temp [i] != '\0');
	return i;
}


After editing,this code is fine,because I use the length for string class, change 1 < length to i < length and return the thing the poster might want
Last edited on
#OPTIMAL NOTES :

- Should have switch cases for uppercase vowels.
- #include <algorithm> unused.
- int length (string temp) like i said,better delete it and use string's length function (already defined in STD)
the string class has its own length function,why even bother ?


Clearly I don't have a good grasp on classes... I'm a total beginner here. I thought writing my own length function would help me learn how this stuff works.

In replace_vowels replaced_letters is an empty string so in the switch you are accessing indexes out of bounds.


Thanks! I got rid of the second series of error messages, but it's not replacing the vowels... I realized I was returning the wrong thing and fixed that, but it still doesn't work. It just reprints the original string. Here's my new code:
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
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

string replace_vowels (int, string);
int length (string);

int main (void)
{
	string first_name;									
	string last_name;
	cout << "Please enter your first name: " << endl;
	cin >> first_name;
	cout << "Please enter your last name: " << endl;
	cin >> last_name;
    //concatenate strings including a space between the words.
	string temp = first_name + ' ' + last_name;				
	cout << "Here's what you entered: " << temp << endl;
    //call function to determine the string length.
	int string_length = length(temp);     
	cout << "The length of your string is " << string_length << "." << endl;
	//call function to replace vowels in string.
	string new_string1 = replace_vowels (string_length, temp);
	cout << "Here's your adjusted entry " << new_string1 << endl;

	system ("BREAK");
	return (0);
}

string replace_vowels (int length, string full_name)
{
	string replaced_letters = full_name;    //gives the two strings the same characters to start
	for (int i = 0; i < length; i++)
	{
		switch (full_name[i])
		{
			case 'a' || 'e' || 'i' || 'o' || 'u':
				replaced_letters[i] = 'z';    //supposed to replace the vowels in the string, but apparently does nothing
				break;
			default:
				replaced_letters[i] = full_name[i];
				break;
		}
	}
	return (replaced_letters);    //returns the string with vowels replaced
}

int length (string temp)    //function counts the characters in the string.
{
	int i = 0;
	do
	{
		i++;    //counter
	}
	while (temp [i] != '\0');    //stops the loop when the last character in the string is reached.
	return i;
}


I also still have the first error message when the length function is called (it pops up after it prints the concatenated string, but before it prints the length of the string). Ideas?
When you wrote,


#include <string>

And

string first_name;

U used a string class,If you don't want to use class don't use it too.

Use C-String instead,it's what I know that have '\0' at the end of the string.

I don't know how string class is implemented,but according to my book a string from string class don't end with '\0',maybe it doesn't have that.

1
2
3
4
5
6
7
8
9
10
int length (string temp)    //function counts the characters in the string.
{
	int i = 0;
	do
	{
		i++;    //counter
	}
	while (temp [i] != '\0');    //stops the loop when the last character in the string is reached.
	return i;
}


while (temp [i] != '\0');

If what I said is true (string class doesn't end with '\0') so the while loop go non-stop...because it never reaches '\0' in string class temp.

EDIT : And when the i in the loop goes higher than temp's size then temp[i] is out of bound...

If you want a C-String Reference,google it or use documents on this website.
Last edited on
int length (string temp) //function counts the characters in the string.

No, it doesn't. You don't declare temp as an array, but you treat it as one:

while (temp [i] != '\0');

To access the value stored at a particular position of a string (or each individual character if you prefer), use the at () function:

http://cplusplus.com/reference/string/string/at/
Last edited on
case 'a' || 'e' || 'i' || 'o' || 'u':
This is wrong. You have to write all the cases separately
1
2
3
4
5
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
Last edited on
case 'a' || 'e' || 'i' || 'o' || 'u':

The if condition support this,switch case doesn't
About the null terminated string, it appears that it's only guaranteed in the latest standard so Visual Studio has probably not updated that part yet.
Thanks for the pointers everyone! After sleeping on it, I don't know why I'm using a switch with case statements when an if-else statement would probably work just fine.

I'll have to poke around and research the string thing, too, to see what the consensus is on the '\0' ending character.

Unfortunately, that will all have to wait until I get off work... ugh.

Thanks again!
Pages: 12