0
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {
    string vowel[] = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "z"};
    string a;

    while (a != "quit!") {
        int b = 1;
        cin >> a;
        for (int i = 0; i < 20; i++) {

            if (a.length() > 4 && a.substr(a.length() - 3 ) == vowel[i] + "or") {
                a[a.length() - 2] = 'o';
                a[a.length() - 1] = 'u';
                a += 'r';
                cout << a << "\n";
                b = 0;
                break;
            }
            //cout << i;
        }

        if (b == 1) {
            cout << a << "\n";
        }
    }
}

For some reason, the entire program just stops if you enter a string that's length is greater than 4 and doesn't end with consonant + "or". The program never even goes in the if statement.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
avisk
  • 330
  • 2
  • 6
  • 16
  • 1
    Are you talking about your while or for loop? Because your description is now talking about a for loop, but your title is referring to the outer while loop – alex067 Nov 07 '19 at 23:19
  • Well yeah like the entire program just stops. I don't know exactly what's happening – avisk Nov 07 '19 at 23:22
  • 3
    Your program probably crashes because your array has fewer than 20 elements. – jxh Nov 07 '19 at 23:22
  • 1
    oh lol thanks so much – avisk Nov 07 '19 at 23:23
  • can you get the length of the array in any way or do i have to just do it manually? – avisk Nov 07 '19 at 23:24
  • 1
    Use `vector` instead, and then use the `size()` method. – jxh Nov 07 '19 at 23:24
  • Unrelated: `#include ` [loads the gun](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). `using namespace std;` [takes the safety off](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Be really cautious with this combination. – user4581301 Nov 07 '19 at 23:25
  • That or use `sizeof(array)/sizeof(array[0])`. But `std::vector` is better. –  Nov 07 '19 at 23:25
  • 2
    `string vowel[]`: do you mean `consonant`? – ph3rin Nov 07 '19 at 23:27
  • @Chipster: Also: `*(&vowel + 1) - vowel` – jxh Nov 07 '19 at 23:32
  • @jxh I don't want to get in a long discussion on it, but wouldn't that be dereferencing unallocated data? –  Nov 07 '19 at 23:34
  • The array `vowel` has dimension less than 20, since you have not included all consonants in the initialiser. The `for` loop accesses 20 elements of that array, so has undefined behaviour. Voting to close as a typo. (Don't even get me started on having an array named `vowel` that contains all consonants. Blechhhh!) – Peter Nov 07 '19 at 23:34
  • 1
    @Chipster: I decided to ask: https://stackoverflow.com/q/58758469/315052 – jxh Nov 07 '19 at 23:47
  • Whoops my bad. The only reason I used `#include ` was because it was already included when I started. Not really sure what it does though... – avisk Nov 08 '19 at 00:10
  • It has been pointed out to me you can use `std::size(array)` to get the number of elements. – jxh Nov 08 '19 at 01:49
  • Ok thanks I'll use that – avisk Nov 08 '19 at 20:44

1 Answers1

0

Try this:

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

using namespace std;

bool endswith(const string& a, string b) {
   return (b == a.substr(a.length() - b.length()));
}

bool isconsonant(char c) {
   vector<char> vowels = { 'a', 'e', 'i', 'o', 'u','y' };

   for (size_t i=0; i < vowels.size(); i++) {
      if (vowels[i] == c)
         return false;
   }

   return true;
}

int main() {
   string a;

   while (a != "quit!") {
      cin >> a;

      if ((a.length() > 4) && isconsonant(a[a.length()-3]) && endswith(a, string("or")))
            a = a.substr(0, a.length() - 2) + "our";

      cout << a << "\n";
   }
}
Laszlo
  • 769
  • 9
  • 19
  • This is a code-only answer. Since they have an annoying tendency to result in [Cargo Cult Programmers](https://en.wikipedia.org/wiki/Cargo_cult_programming), you should avoid making them. You can fix this problem by explaining what the problem was and how you fixed it. – user4581301 Nov 08 '19 at 03:19