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?
- 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:
#include <iostream>
#include <algorithm>
#include <string>
usingnamespace 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?
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.
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.