So i've been working on this for while and i keep getting error after error, after i posted here last i cleaned it up but here is my latest debug error. I feel like that my code and algorithms are all there but im messing up in one of my loops. Any help is appreciated.

Heres my code:

/*
	Write a program that reads in a text file 
	and converts it to pig Latin. i.e. if the 
	first letter is a consonant, move it to the
	end and add “ay” to the end, if it is a vowel
	add “way” to the end. Here’s an example:
	“end the war” becomes “endway hetay arway”.
	Include a structure chart.
*/

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);


int main()
{
	ifstream infile;
	string newstr;
	int length;
	char achar = ' ';
	string str; 
	string str1;
	string str2;
	string str3;
	string pStr;
	char ch = ' ';

	infile.open("C:\\201\\PigLatin.txt");

	if(infile.is_open() == false)
	{
		std::cerr << "Error, unable to open file." << std::endl;
		return 1;
	}

	std::string word;
	while(infile.fail() == false)
	{
	    infile >> word;
	    std::cout << pigLatinString(word) << ' ';
	}

	cout << endl;
	cout << "Pig Latin String Is: " << endl;
	cout << endl;
	while (!infile.eof())
	{
		infile.get(achar);

		if (achar != ' ') 
		{
			str = str + achar;
	
		}	
	else 
	{
		cout << pigLatinString(str)<< " ";
		str = "";
	}
	}
	if (achar == '!')
	{
		length = static_cast<unsigned int>(str.length());
		newstr = str.substr(0,(length-2));
		cout << " " << pigLatinString(newstr) << "!";
	}
	if (achar == '?')
	{
		length = static_cast<unsigned int>(str.length());
		newstr = str.substr(0,(length-2));
		cout << " " << pigLatinString(newstr) << "?";
	}
	if (achar == '.')
	{
		length = static_cast<unsigned int>(str.length());
		newstr = str.substr(0,(length-2));
		cout << " " << pigLatinString(newstr) << ".";
	}

	cout << endl;
	str = "";
	cout << endl;
	isVowel(ch);
	rotate (pStr);
	pigLatinString(pStr);
	infile.close();


	return 0;
}
bool isVowel(char ch)
{
switch (ch)
{
	case 'A': case 'E':
	case 'I': case 'O':
	case 'U': case 'Y':
	case 'a': case 'e':
	case 'i': case 'o':
	case 'u': case 'y': return true;
	default: return false;
}
}

string rotate(string pStr)
{
	string::size_type len = pStr.length();

	string rStr;

	rStr = pStr.substr(1, len - 1) + pStr[0];

	return rStr;
}

string pigLatinString(string pStr)
{
{
	string::size_type len;

	bool foundVowel;

	string::size_type counter;

	if (isVowel(pStr[0]))
	{
		pStr = pStr + "way";
	}
	else
	{
		pStr = pStr + '-';
		pStr = rotate(pStr);
		len = pStr.length();
		foundVowel = false;
	}
for (counter = 1; counter < len - 1; counter++)
	if (isVowel(pStr[0]))
	{
		foundVowel = true;
		break;
	}
	else 
	{
	pStr = rotate(pStr);
	}
	if (!foundVowel)
	{
		pStr = pStr.substr(1,len) + "way";
	}
	else
	{
		pStr = pStr + "ay";
	}
}
return pStr;
}

Here is the error message:

That error likely means you are accessing an index outside of the contents of a string:

string a = "hello"; // valid indices are 0-4
cout << a[7]; // should cause that error

Check the bounds of your loops, etc.

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.