I have a bit of code that is to make a reference code from a string of a last name. It should take the first char whether vowel or not, then search each remaining char in the array discarding the vowels storing consonants only in a temp string, then return the temp string to string refCode; The code was given to me in C and I converted it to C++. The code compiles assigns the first value correctly, but if the if returns true it will fail upon attempting to assign the second value to the temp string. The code is over 5 external .cpps and 4 .hs so I’ll start with the minimal amount and will post more as needed.
Protytpe:
string makeRefCode(string lastname, int cNo);
Call:
refCode = makeRefCode(e[c].lastname, cNo); cout << refCode;//Prints nothing
Function Defs:
string makeRefCode(string lastname, int cNo)
{
string tStr;
unsigned int i, j = 1;
unsigned int x;
x = lastname.length();
tStr[0] = lastname[0];
cout << tStr[0];//Prints correct value
for (i = 1; i < x; i++)
{
if (!isVowel(toupper(lastname[i])))
{
//tStr[j] = lastname[i];//
j++;
}
}
//refCode[j] = '0'; // add string terminator
return tStr;
}
bool isVowel(char aChar)
{
switch (aChar) //<ctype>
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': return true; break;
default: return false;
}
}
As I’ve tried resolving this issue I’ve gotten Assertion, Access violation, and a string error that seems like it’s saying the string isn’t big enough. Any help would be greatly appreciated.
String don’t automatically grow in size. If your string starts at zero length (like
tStrdoes) then you need to usepush_backto add characters to the end of the string.and