In this lab, you add a loop and the statements that make up the loop body to a C++ program that is provided. When completed, the program should calculate two totals: the number of left-handed people and the number of right-handed people in your class. Your loop should execute until the user enters the character X instead of L for left-handed or R for right-handed.

The inputs for this program are as follows: R, R, R, L, L, L, R, L, R, R, L, X

Variables have been declared for you, and the input and output statements have been written.

Instructions
Ensure the source code file named LeftOrRight.cpp is open in the code editor.

Write a loop and a loop body that allows you to calculate a total of left-handed and right-handed people in your class.

Execute the program by clicking the Run button and using the data listed above and verify that the output is correct.

// LeftOrRight.cpp - This program calculates the total number of left-handed and right-handed
// students in a class.
// Input: L for left-handed; R for right handed; X to quit.
// Output: Prints the number of left-handed students and the number of right-handed students.

#include
#include
using namespace std;
int main()
{
string leftOrRight = ""; // L or R for one student.
int rightTotal = 0; // Number of right-handed students.
int leftTotal = 0; // Number of left-handed students.

// This is the work done in the housekeeping() function
cout << "Enter an L if you are left-handed, a R if you are right-handed or X to quit: ";
cin >> leftOrRight;

// This is the work done in the detailLoop() function
// Write your loop here.


// This is the work done in the endOfJob() function
// Output number of left or right-handed students.
cout << "Number of left-handed students: " << leftTotal << endl;
cout << "Number of right-handed students: " << rightTotal << endl;

return 0;
}

Respuesta :

Answer:

#include <iostream>

#include <string>

//  R, R, R,  L, L, L,  R,  L,  R, R,  L,  X  ==>  6R 5L  X

void getLeftOrRight(std::string &l_or_r, int &total_l, int &total_r) {

 // notice how the above line matches:   getLeftOrRight(leftOrRight, leftTotal, rightTotal);

 // the values provided from the loop are given nicknames for this function getLeftOrRight()

 if (l_or_r == "L") {  // left-handed

   total_l++;

 } else if (l_or_r == "R") {  // right-handed

   total_r++;

 } else {  // quit

   // option must be "X" (quit), so we do nothing!

 }

 return;  // we return nothing, which is why the function is void

}

int main() {

 std::string leftOrRight = "";  // L or R for one student.

 int rightTotal = 0;            // Number of right-handed students.

 int leftTotal  = 0;            // Number of left-handed students.

 while (leftOrRight != "X") {  // whilst the user hasn't chosen "X" to quit

   std::cout << "Pick an option:" << std::endl;

   std::cout << "\t[L] left-handed\n\t[R] right-handed\n\t[X] quit\n\t--> ";

   std::cin  >> leftOrRight;  // store the user's option in the variable

   getLeftOrRight(leftOrRight, leftTotal, rightTotal);  // call a function, and provide it the 3 arguments

 }

 

 std::cout << "Number of left-handed students:  " << leftTotal  << std::endl;

 std::cout << "Number of right-handed students: " << rightTotal << std::endl;

 

 return 0;

}

Explanation:

This is my take on the question! I had no idea what was meant by the housekeeping(), detailLoop(), endOfJob() functions provided in their source code.

What my code does should be pretty explanatory via the comments and variable names within it — but feel free to make a comment to ask for more info! I've attached a screenshot of my code because I think it's easier to read when it's syntax highlighted with colors.

Ver imagen KennyOliver