Homework Help: Questions and Answers: String inputWord is read from input and integer sumCounts is initialized with 0. Write a while loop that iterates until inputWord is equal to “End”. In each iteration of the loop:
- Read integer furnitureCount from input.
- Increase sumcounts by the value of furnitureCount.
- Output the value of inputWord, followed by a newline.
- Read string inputWord from input.
Example: If the input is Wardrobe 37 Cupboard 1 Cabinet 8 End, then the output is:
Wardrobe
Cupboard
Cabinet
46 items
Answer:
Let’s write the code in C++ step by step, first we
- Initialize variables
- Set up the while loop
- Inside the loop: a. Read furnitureCount b. Update sumCounts c. Output inputWord d. Read next inputWord
- After the loop, output the total count
C++ Code
#include <iostream>
#include <string>
using namespace std;
int main() {
string inputWord;
int sumCounts = 0;
// Reading the first input word
cin >> inputWord;
// Loop until "End" is encountered
while (inputWord != "End") {
int furnitureCount;
// Read the furniture count
cin >> furnitureCount;
// Increase sumCounts by the value of furnitureCount
sumCounts += furnitureCount;
// Output the value of inputWord
cout << inputWord << endl;
// Read the next input word
cin >> inputWord;
}
// Output the final sum of furniture counts
cout << sumCounts << " items" << endl;
return 0;
}
Explanation:
- The program starts by reading
inputWord
from the input. - It enters a
while
loop that runs untilinputWord
is equal to"End"
. - Inside the loop:
- It reads the integer value
furnitureCount
. - Adds
furnitureCount
tosumCounts
. - Prints the value of
inputWord
followed by a newline. - Reads the next
inputWord
.
- It reads the integer value
- After the loop ends (when
"End"
is encountered), it prints the total count of furniture items.
Example:
If the input is:
Wardrobe 37 Cupboard 1 Cabinet 8 End
The output will be:
Wardrobe
Cupboard
Cabinet
46 items
Learn More: Homework Help
Q. A type of technology that can be attached to a tag and used to identify postal packages is a(n)?
Q. When I attach a file to my e-mail, will SMTP send it to the e-mail server?
Q. Which of the following statements best describes the permanency of online activity?
Q. Smart home voice assistants and non-computer devices that can access the internet are known as