For class I am required to write a program that takes input from a .txt file, converts it to pig latin using c_strings and then outputs the results to another .txt file. I have tested each of my functions on their own as well as using a for() loop to iterate through about 40 calls to the functions. When I pass around 60 calls though I get a Debug Assertion Failed! popup stating
"File: minkernel\crts\ucrt\inc\corect_internal_string_templates.h
Line: 50
Expression:(L"Buffer is too small" && 0)"
Here is my call to the functions.
while(!inputFile.eof())
{
engStr = readDataFromFile(inputFile);
pLatinStr = convertToPigLatin(engStr, pLatinStr);
outPutResults(outputFile, engStr, pLatinStr);
}
and here are the functions being called.
char * readDataFromFile(ifstream &inFile)
{
char * arr = new char[20];
inFile.getline(arr, 15);
return arr;
delete[] arr;
}
bool checkVowel(char * engStr, int count)
{
if (
engStr[count] == 'a' ||
engStr[count] == 'e' ||
engStr[count] == 'i' ||
engStr[count] == 'o' ||
engStr[count] == 'u'
)
return true;
else
return false;
}
char * convertToPigLatin(char * engStr, char * pLatinStr)
{
char * arr = new char[20];
int count = 0;
pLatinStr = arr;
while (engStr[count] != '\0')
{
bool vowel = checkVowel(engStr, count);
if (vowel && count == 0)
{
stringCopyVowelFirst(pLatinStr, engStr);
break;
}
else
if (vowel)
{
stringCopyAfterVowel(pLatinStr, engStr, count);
break;
}
count++;
}
return pLatinStr;
delete[] arr;
}
void stringCopyAfterVowel(char *& pLatinStr, char * engStr, int count)
{
int LtnIndex = 0;
char ayStr[] = "ay\0";
char tempStr[20];
tempStr[0] = 0;
strcpy_s(pLatinStr, 15, engStr);
strncat_s(tempStr, strlen(tempStr) + 10, engStr, count);
while (engStr[count] != '\0')
{
pLatinStr[LtnIndex] = engStr[count];
count++;
LtnIndex++;
}
pLatinStr[LtnIndex] = '-';
pLatinStr[LtnIndex + 1] = '\0';
strcat_s(pLatinStr, 15, tempStr);
strcat_s(pLatinStr, 15, ayStr);
}
void stringCopyVowelFirst(char *& pLatinStr, char * engStr)
{
char wayStr[] = "-way\0";
strcpy_s(pLatinStr, 15, engStr);
strcat_s(pLatinStr, 15, wayStr);
}
void outPutResults(ofstream &outFile, char * engStr, char * pLatinStr)
{
outFile.open("pigLatinOut.txt", std::ofstream::app);
outFile << left << setw(12) << engStr << "\t";
outFile << setw(14) << pLatinStr << endl;
outFile.close();
}
Any help is greatly appreciated. This is about my third week studying c++.