Write a C++ program that Prompts the user for input and store their response int
ID: 3666738 • Letter: W
Question
Write a C++ program that Prompts the user for input and store their response into a string variable.
Prompt the user for a second string input and store the new response into
a second string variable.
If the first string is less than the second, write out a message in the form
string1_contents “ is less than “ string2_contents, where the
_contents are the actual contents of the string variables.
Otherwise, write out a message saying
“both strings contained “ string_contents, where string_contents is
the contents of either string variable.
Note: string input may contain blank spaces
Explanation / Answer
Program in C++, Comments are given in bold letters, please have a look
#include<iostream>
#include<string>
using namespace std;
int main()
{
string string1, string2; //taken two string
cout<<"Provide the first input"<<endl;
std::getline(std::cin, string1); //stored the first response into first string, used getline to include spaces in string
cout<<"Provide the second input"<<endl;
std::getline(std::cin, string2); //stored the second response into second string, used getline to include spaces in string
if(string1.length() < string2.length()) //used if-else to compare the lenght of two strings
{
cout<<"string1_contents is less than string2_contents"<<endl;
}
else if(string1.length() == string2.length())
{
cout<<"string1_contents is equal to string2_contents"<<endl;
}
else
{
cout<<"string1_contents is greater than string2_contents"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.