Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a loop that reads strings from standard input where the string is either \

ID: 3651684 • 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.

This is what I have so far.
char land, air, water;
int l, a, w, j;

land = 'l';
air = 'a';
water = 'w';

l = 0, a = 0, w = 0;

cin>> j;

if (j = land, l = l + 1)
{
cout<< "Land";
}
if (j = air, a = a + 1)
{
cout<< "Air";
}
if (j = water, w = w + 1)
{
cout<< "Water";

Please HELP...

Explanation / Answer

I think you've missunderstood your assignment a bit! You're asked to read user input. Everytime the user inputs land, air or water, their respective counter will increase. After the user has finished (by typing "xxxxx"), the program will tell you how much you've typed everything in. It should be something like this: int land = 0, air = 0, water = 0; String input = ""; do{ System.out.printf("Type in land, air or water. Type xxxxx to quit: "); input = stdin.nextLine(); switch (input) { case "land": land++; break; case "air": air++; break; case "water": water++; break; } }while(input != "xxxxx"); System.out.println("land:" + land); System.out.println("air:" + air); System.out.println("water:" + water); First, three integers are initialised, with values 0. These will be the counters for respecively land, air and water. Then, a string that will serve to hold your input is initialized. Now, I use a do / while to first capture and check the input (I use a switch as you suggested but you could just as well do a if(input == "land") land++; else if(input == "air") etc etc After this the while gets executed. It controls the input value. As long as the input is not "xxxxx" the while loop keeps running. Finally, three lines are printed with their respective counter values.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote