How can I construct the array problem?

C++ Function/ Array Vowel Input Problem TRYING TO CREATE A ERROR MESSAGE!! :D?

  • I am trying to prompt the user to input 5 vowels. I did that. However, what if the user input a letter other then the 5 vowels, i need to print a error message. I need HELP with this. Main: calls one function to convert the vowels to their respective codes. calls one print function that prints the codes User-Defined Function: Asks the user to input a vowel in any order and stores in the array Converts the entered vowel to 1,2,3,4,5. User-Defined Function 2: prints the original vowels entered by the user to the screen prints the corresponding array to the screen. Program Output: the original vowels entered by the user the corresponding array of digits 1-5 an error message if a vowel is not entered by the user Here is my code: #include <iostream> #include <iomanip> using namespace std; void inputVowels( char[], int[] ); void outputVowels( char[], int[] ); int main() { char vowels[5]; int codes[5]; char choice; system("clear"); do { inputVowels(vowels, codes); outputVowels(vowels, codes); cout<<"Do you want to continue? (y or n)"<<endl; cin>>choice; } while ( choice == 'y' ); return 0; } void inputVowels ( char vowels[], int codes[] ) { bool value=false; do { cout<<"Please enter 5 vowels (a, e, i, o, u): "<<endl; for (int a=0; a < 5; a++) cin>>vowels[a]; for (int a=0; a < 5; a++) { if(vowels[a]!='a'&&vowels[a]!='e'&&vowel… value=true; } } while ( value=false ); for (int a=0; a < 5; a++) { if ( vowels[a] == 'a' ) codes[a] = 1; else if ( vowels[a] == 'e' ) codes[a] = 2; else if ( vowels[a] == 'i' ) codes[a] = 3; else if ( vowels[a] == 'o' ) codes[a] = 4; else if ( vowels[a] == 'u' ) codes[a] = 5; } } void outputVowels( char array[], int code[]) { cout<<left<<setw(15)<<"Vowels"<<right<… Code"<<endl; cout<<left<<setw(15)<<"------"<<right<… cout<<left<<setw(15)<<array[0]<<right<… cout<<left<<setw(15)<<array[1]<<right<… cout<<left<<setw(15)<<array[2]<<right<… cout<<left<<setw(15)<<array[3]<<right<… cout<<left<<setw(15)<<array[4]<<right<… }

  • Answer:

    Printing of an error message would go right before this line: } while ( value=false ); if value had not been set to true by the preceding code: if (!value) cout << "Error: you did not enter a vowel" << endl; But looking at the preceding code that is supposed to validate the user input: for (int a=0; a < 5; a++) { if(vowels[a]!='a'&&vowels[a]!='e'&&vow… value=true; } it has a couple of problems: it doesn't need to loop (you are already looping), and it sets the value to true if the user input is NOT a vowel, as opposed to if it IS a vowel. Try this instead: value = vowels[a] == 'a' || vowels[a] == 'e' || vowels[a] == 'i' || vowels[a] == 'o' || vowels[a] == 'u'; or more concisely by taking advantage of a simple C function (also available to C++ programs) that searches for characters in strings: value = strchr("aeiou", vowels[a]) != NULL;

Dr. Weny yi at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.