Write a loop that reads strings from standard input where the string is either \
ID: 3654054 • Letter: W
Question
Write a loop that reads strings from standard input where the string is either "land", "air", or "water". The loop terminates when "xxxxx" (five x characters) is read in. Other strings are ignored. After the loop, your code should print out 3 lines: the first consisting of the string "land:" followed by the number of "land" strings read in, the second consisting of the string "air:" followed by the number of "air" strings read in, and the third consisting of the string "water:" followed by the number of "water" strings read in. Each of these should be printed on a separate line.Explanation / Answer
Please rate...
Program:
================================================
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
string land="land";
string air="air";
string water="water";
int l=0,a=0,w=0;
while(true)
{
cout<<"Enter string: ";
cin>>s;
if(s.compare(land)==0)l++;
if(s.compare(air)==0)a++;
if(s.compare(water)==0)w++;
if(s.compare("xxxxx")==0)break;
}
cout<<"Land: "<<l;
cout<<" Air: "<<a;
cout<<" Water: "<<w;
}
=================================================
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.