Question
QUESTION: Write a C++ program that prompts the user for three words. Ask the user to prompt 3 numbers, that should be less than the
QUESTION: Write a C++ program that prompts the user for three words. Ask the user to prompt 3 numbers, that should be less than the length of each word to indicate length of substring in each word. Create a new word, which merge the substring of 3 words. If the entered number is even, the substring starts from the first letter; if it is odd, the substring starts from the second letter
Everything works fine until the new generated word. Nothing is displayed. please help!
#include
#include
using namespace std;
int main()
{
// Declaring variables
string word1; // First word
string word2;
string word3;
string newWord; // New generated word
int num1;
int num2;
int num3;
// Prompt user for the 3 words
cout << "Please enter 3 words : ";
cin >> word1 >> word2 >> word3;
cout << endl;
// Prompt user for first number
cout << "Enter the 1st number less than "<<(word1.length())<< ": ";
getline(cin, word1);
cin >> num1;
cout << endl;
// Prompt user for second number
cout << "Enter the 2nd number less than "<<(word2.length())<<": ";
getline(cin,word2);
cin >> num2;
cout << endl;
// Prompt user for third number
cout << "Enter the 3rd number less than "<<(word3.length())<<": ";
getline(cin,word3);
cin >> num3;
cout << endl;
// Generate the new word
if (num1%2==0 && num2%2==0 && num3%2==0){ // If number entered is even
string word_1 = word1.substr(0, num1); // Substring starts from first letter
string word_2 = word2.substr(0, num2);
string word_3 = word3.substr(0, num3);
cout << "The new word is: "< } if (num1%2!=0 && num2%2!=0 && num3%2!=0){ // If number entered is odd string word_1_odd = word1.substr(1, num1); // Substring starts from second letter string word_2_odd = word2.substr(1, num2); string word_3_odd = word3.substr(1, num3); cout << "The new word is: "< } return (0); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started