My code works perfectly I just want to know how I can modify my code so if the user enters anything other than the numbers 1, 2, 3, or 4 the program will print the menu again asking for the user to select an option. Or even maybe write "you have entered an incorrect option, please choose again."
#include <iostream>
#include <stdlib.h>
usingnamespace std;
void print_menu();
void reverse();
void matrix();
void palindrome();
int main ()
{
int option;
do
{
print_menu();
cin >> option;
cout << endl;
if (option == 1)
{
reverse();
}
elseif (option == 2)
{
matrix();
}
elseif (option == 3)
{
palindrome();
}
elseif (option == 4)
{
cout << "Your program will exit.\n";
break;
}
} while (option <=4 && option >=1);
system ("Pause");
return 0;
}
void print_menu()
{
cout << "Please read the menu below and choose from one of the following options by pressing 1, 2, 3, or 4:\n";
cout << "1. Given an input a String and output its reverse .\n";
cout << "2. Given an Integer n and an input character C, print an n x2n matrix of C's .\n";
cout << "3. See if the input string is a palindrome.\n";
cout << "4. Exit program.\n";
cout << endl;
}
void reverse()
{
string word;
cout <<"Enter string.\n";
cin >> word;
int length = word.length();
for (int i = length-1; i >= 0; i--)
{
cout << word[i]<<endl;
}
}
void matrix ()
{
int value;
char letter;
cout << "Input an integer value: ";
cin >> value;
cout << "Enter any character: ";
cin >> letter;
for ( int row = 0; row < value*2; row++)
{
for ( int col = 0; col < value; col++)
{
cout << letter;
}
cout << endl;
}
}
void palindrome ()
{
string word;
cout <<"Enter string.\n";
cin >> word;
int end = word.length();
int mid = end/2;
end = end - 1;
int start = 0;
while (mid > start)
{
if (word[start] == word[end])
{
end--;
start++;
}
else
{
cout << "This is not a palindrome.\n";
break;
}
}
if (word[start] == word[end])
{
cout << "This is a palindrome!\n";
}
}