How can I change my input to accept a string from the user? Currently my program will only work with a char value

  arrays, c++, string

I am almost complete this program, however I need to change it to where a user can input a string instead of only a char. Currently it will only accept a char, and adding anything more will terminate the program abruptly. I have used strings in the past, but this assignment is a little different. I am required to input anything a user types, and see if that can be converted into only the letters included in the array. These are { ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, ‘h’, ‘k’, ‘l’, ‘m’, ‘n’, ‘p’, ‘w’}** If the user enters anything other than the alphabet, it should give an error, saying to only accept words. If the user enters only alphabet, then the program should check whether it can be written in the reducedAlphabet array. If it cannot, it should say no. If all the input can be converted into those specific alphabetic characters, my program will output "Ae".

I will attach some examples for how I want the program to run. I have tried creating a loop that will validate each character the user types and see if it is a letter, however I have had trouble making it. What would be a good way for me to validate a string the user types, and see if it will fit in the alphabet? How can I also make sure that the input does not matter whether they are capital or not? I am also a beginner to coding btw. Here is a basic pseudocode:

Enter String: >$5&

Error. Only Words. Please try again.

Enter String: >-23

Error. Only Words. Please try again.

Enter String: >abc12

Error. Only Words. Please try again.

Enter String: >12asd

Error. Only Words. Please try again.

Enter String: >Talk

No

Enter String: >mE

Ae

Enter String: >yOu

No

Enter String: >nOW

Ae

  // Example program
   #include <iostream>
   #include <cctype>

using namespace std;

int searchList(char[],int, char);              //Function prototype



int main()
{
 char again;                                    //Will hold a y or n
 
 char input;                                    //Will hold the users input
 
 
 int max = 13;
 char reducedAlphabet[max] = { 'a', 'e', 'i', 'o', 'u', 'h', 'k', 'l', 'm', 'n', 'p', 'w'};         //Declaration of alphabet
 int results;

 

 do
   {
    cout << "Enter any input " << endl;
    cin >> input;
    
    if (isalpha(input)) 
    {
        results = searchList(reducedAlphabet, max, input);
        
     {
        if (results == -1)
             cout << "No " << endl;
        else
            cout << "Ae " << endl;
    }
    }
   
    else
    {
        cout << "Error. Words only. Please try again. " << endl;
    }
                
     cout << "Do you want to input another value? (Y/N) " << endl;
     cin >> again;
    
}while (toupper(again) == 'Y');

     
 return 0;
}

int searchList(char *list, int num, char value)
{
     int index = 0;
     int position = -1;
     bool found = false;
            
         while (index < num && !found && index<=value)
            {
                if (list[index] == value)
                {
                    found = true;
                    position = index;
                }
                index++;                                        
            }return position;    
}

Source: Windows Questions C++

LEAVE A COMMENT