#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

/**
Test case 1: test words that start with a vowel.
When inputting the words air and ear, then outputs are airay and earay.
Test case 2: test words that start with a consonant.
When inputting the words top and dog, then outputs are optay and ogday.
Test case 3: test words that start with multiple consonants.
When inputting the words stop and blythe, then outputs are opstay and eblythay.
Test case 4: test the sentinels.
Inputting # ends the loop, translates to pig latin, and prompts the user
to start over. Inputting opstay completely ends the program.
Test case 5: test that punctuations are handled properly.
Inputting a punction at the end of the word such as ! or , results in
the word being outputted followed by the punctuation.
*/

// this function takes in a char and returns whether it's a vowel or not.
bool check_vowel(char current_char)
{
	if (current_char == 'a' || current_char == 'e'
		|| current_char == 'i' || current_char == 'o' 
		|| current_char == 'u')
	{
		return true;
	}
	else { return false; }
}

int main()
{
	bool run = true;

	while (run == true)
	{
		cout << "Write text so that it can be converted to ";
		cout << "pig latin (type # when you are done). ";
		cout << "If you want to stop the program completely, ";
		cout << "type \"opstay\": ";

		string user_input;
		int count_words = 0;
		vector<string> inputs;

		/* each word seperated by a space is saved in the order it was typed
		and added to the vector: inputs */
		while (user_input != "#" && run == true)
		{
			cin >> user_input;

			if (user_input != "#")
			{
				inputs.push_back(user_input);
				count_words++;
			}
			if (user_input == "opstay") { run = false; }
		}

		// handles the input
		if (user_input != "opstay")
		{
			for (int i = 0; i < count_words; i++)
			{
				// saves the length of the input into an int
				int current_size = inputs[i].length();

				char last_char = inputs[i][current_size - 1];
				char print_last_char = '!';
				bool punct = false;

				// handles inputs that contain punctuations
				if (ispunct(last_char))
				{
					inputs[i] = inputs[i].substr(0, current_size - 1);
					print_last_char = last_char;
					current_size--;
					punct = true;
				}

				string ay = "ay";
				string vowel_first;
				vector<char> char_inputs;
				bool keep_going = true;

				// saves all the characters of the input into the vector: char_inputs
				for (int j = 0; j < current_size; j++)
				{
					char_inputs.push_back(inputs[i][j]);
				}

				bool check_vowel_1 = check_vowel(inputs[i][0]);

				// handles inputs that start with a vowel, simply adds "ay"
				if (check_vowel_1)
				{
					vowel_first = inputs[i] + ay;
					keep_going = false;
					cout << vowel_first;
				}

				// handles inputs that don't start with vowels
				else if (!check_vowel_1)
				{
					keep_going = true;
					char number_char = ' ';
					bool dont_run = false;

					/* deletes the first letter of the input and adds it to the
					end. does this until every consonant is taken care of. */
					for (int w = 0; w < current_size && keep_going; w++)
					{
						number_char = char_inputs[0];

						for (int j = 1; j < char_inputs.size(); j++)
						{
							char_inputs[j - 1] = char_inputs[j];
						}
						char_inputs.pop_back();
						char_inputs.push_back(number_char);

						bool check_vowel_2 = check_vowel(inputs[i][w + 1]);

						if (check_vowel_2) { keep_going = false; }

						if (w == current_size) { dont_run = true; }
					}
				
					if (dont_run == true)
					{ 
						cout << vowel_first;
					}
					else
					{
						char_inputs.push_back('a');
						char_inputs.push_back('y');

						for (int j = 0; j < current_size + 2; j++)
						{
							cout << char_inputs[j];
						}
					}
				}
				if (punct == true)
				{
					cout << print_last_char;
				}
				cout << " ";
			}
		}
		cout << endl << endl;
	}


	system ("pause");
	return 0;
}

The point of the program is to receive input and output it as pig latin. The only problem is when the input consists ONLY of consonants (ex: try). I'm using visual studio to compile and when I input an all consonant word, I am prompted:

"
Debug assertion failed!

Program: ...
Line: 1441

Expression: String subscript out of range
"

The loop that I marked in red deals with inputs that have consonants.
If anyone has any idea what the problem is I would really appreciate it!

in my opinion, you should write your own function specific for testing cases where input is strictly 'all consonants', and handle this case accordingly.

just because your teacher didn't put this in your assignment doesn't mean ye' shouldn't take the initiative.

The problem is with the line:
bool check_vowel_2 = check_vowel(inputs[w + 1])
It's the [w+1]. When w gets close enough to current_size, i guess w + 1 does not exist. Any suggestions on how I could fix this without having to make a whole new loop?

This article has been dead for over six months. Start a new discussion instead.