Исправление ошибок в C ++: Гавайская программа произношения - PullRequest
0 голосов
/ 21 сентября 2019

Я пишу программу на C ++, которая покажет вам, как произносить гавайское слово.

Правила:

  • p, k, h, l, m, n (произносится как)как в английских версиях.)
  • w-начало слова (произносится как звук w или av. Мы произносим его как звук w).
  • w - после буквы «a» (Произносится как звук aw или av. Мы произносим его как звук aw.)
  • w -a после 'i' или 'e' (произносится как звук av).
  • w -after 'u 'или o (произносится как звук aw).

Мой прогресс: моя программа работает, но есть пара ошибок.

  1. Когда я набираю "iwa", произношение должно быть "ee-vah", но я получаю "ee-wah".

  2. Мой код не игнорирует пробелы.Поэтому, когда я набираю «e komo mai», я хочу «eh koh-moh meye», но вместо этого я получаю «e komo mai произносится как eh-eh-koh-moh-oh-meye».

Вот мой код:

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

string convertToHawaiian(string input);
bool vowelGroup(char first, char next, string& replacement);
bool consonantGroup(char prevchar, char currchar, string& replacement);
bool singleVowel(char current, string& replacement);

int main() {
   // Declare my variables
   string userInput;
   string replacement;

   //cout << "Enter a hawaiian word to pronounce ==>";
   getline(cin,userInput);

   // For loop that will call my Consonant function
   // Call my Hawaiian function and put it
   // into pronunciation 
   replacement = convertToHawaiian(userInput);

   // if My initial input has a capital, the replacement
   // will keep the capital letter at the beginning
   if (isupper(userInput.at(0))) {
      replacement.at(0) = toupper(replacement.at(0));
   }

   // Get rid of the '-' character at the end of the string
   if (replacement.at(replacement.size() - 1) == '-') {
      replacement.pop_back();
   }

   cout << userInput << " is pronounced " << replacement << endl;

   return 0;
}

// My main function that will convert my input
// into the hawaiian pronunciation
string convertToHawaiian(string input) {
   char nextChar;
   string replacement = "";
   string result = "";

   // Iterate through my string to check for vowels,
   // vowelgroups and consonants.
   for (unsigned int i = 0; i < input.size(); i++) {
      char character = input.at(i);
      character = tolower(character);

      if (i != input.size() - 1) {
         nextChar = input.at(i + 1);
         nextChar = tolower(nextChar);
      }
      if ((i != input.size() - 1) && (vowelGroup(character, nextChar, replacement))) {
         i++;
         result = result + replacement;
      }
      else if (singleVowel(character, replacement)) {
         result = result + replacement;
      }
      else {
         consonantGroup(character, nextChar, replacement);
         result = result + replacement;
      } 
   }
   return result;
}

bool vowelGroup (char first, char nextChar, string& result) {
   bool isVowel = true;
   if (first == 'a') {
      nextChar = tolower(nextChar);

      if ((nextChar == 'i') || (nextChar == 'e')) {
         result = "eye-";
      }
      else if ((nextChar == 'o') || (nextChar == 'u')) {
         result = "ow-";
      }
      else {
         return false;
      }     
   }
   else if (first == 'e') {
      nextChar = tolower(nextChar);

      if (nextChar == 'i') {
         result = "ay-";
      }
      else if (nextChar == 'u') {
         result = "eh-oo-";
      }
      else {
         return false;
      }
   }
   else if (first == 'i') {
      nextChar = tolower(nextChar);

      if (nextChar == 'u') {
         result = "ew-";
      }
      else {
         return false;
      }
   }
   else if (first == 'o') {
      nextChar = tolower(nextChar);

      if (nextChar == 'i') {
         result = "oy-";
      }
      else if (nextChar == 'u') {
         result = "ow-";
      }
      else {
         return false;
      }
   }
   else if (first == 'u') {
      nextChar = tolower(nextChar);

      if (nextChar == 'i') {
         result = "ooey-";

      }
      else {
         return false;
      }
   }
   else {
      isVowel = false;
      return isVowel;
   }
   return isVowel;
}

// Check to verify consonants
bool consonantGroup(char character, char nextChar, string& replacement) {
   bool isConson = true;
   if ((character == 'p') || (character == 'k') || (character == 'h') || (character == 'l') || (character == 'm') || (character == 'n')){
      replacement = character;
      return isConson;
   }
   if ((character == 'w') && (nextChar == 'a')) {
      replacement = 'w';
      return isConson;
   }
   else if (((character == 'u') || (character == 'o')) && (nextChar == 'w')) {
      replacement = 'w';
      cout << "Not replacing w" << endl;
      return isConson;
   }
   else if (((character == 'i') || (character == 'e')) && (nextChar == 'w')) {
      replacement = 'v';
      return isConson;
   }
   else {
      isConson = false;
      return isConson;
   }
}

bool singleVowel(char current, string& result) {
   bool isVowel = true;
   if (current == 'a') {
      result = "ah-";
      return isVowel;
   }
   else if (current == 'e') {
      result = "eh-";
      return isVowel;
   }
   else if (current == 'i') {
      result = "ee-";
      return isVowel;
   }
   else if (current == 'o') {
      result = "oh-";
      return isVowel;
   }
   else if (current == 'u') {
      result = "oo-";
      return isVowel;
   }
   else {
      isVowel = false;
      return isVowel;
   }
}

1 Ответ

0 голосов
/ 21 сентября 2019

Одним из наиболее распространенных источников ошибок является общее изменяемое состояние.
В вашем случае это общее состояние между итерациями цикла.

Здесь обрабатывается случай пропуска:

else {
    consonantGroup(character, nextChar, replacement);
    result = result + replacement;
}

, и в этом случае replacement будет строкой, которая была "пролита" из предыдущей итерации.

Средство очень простое: вместо этого объявите replacement внутри цикла.

Вы можете решить проблему "iwa", если будете смотреть на эти строки, пока не обнаружите ошибку:

if ((character == 'w') && (nextChar == 'a')) {
    replacement = 'w';
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...