Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- // Receive input from the user. If it starts with a vowel,
- // store the word. If not, ask to re-enter the input.
- // This function takes a character and returns
- // true if it is a vowel. Otherwise, it returns false.
- int is_vowel(char letter)
- {
- // The character must be one of these.
- string vowels = "AaEeIiOoUu";
- // Go from the beginning to the
- // end of the list of vowels.
- for (size_t i = 0; i < vowels.size(); ++i)
- {
- // If the letter matches that vowel.
- if (letter == vowels[i])
- {
- // The letter is a vowel.
- return true;
- }
- }
- // The letter didn't match any vowel.
- return false;
- }
- int main()
- {
- string input; // User input.
- cout << "Enter a word that starts with a vowel:" << endl;
- getline(cin, input); // Get the user's input.
- char first_letter = input[0]; // Get the first letter.
- bool valid = is_vowel(first_letter); // Test the first letter.
- while (!valid) // While the letter is not a vowel,
- {
- cout << "Your word doesn't start with a vowel.\n";
- cout << "Please enter a word that starts with a vowel:" << endl;
- getline(cin, input); // Get new input.
- first_letter = input[0]; // Get the first letter.
- valid = is_vowel(first_letter); // Test the first letter.
- }
- cout << "Thank you!";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement