229k views
2 votes
NOTE: in mathematics, division by zero is undefined. So, in C++, division by zero is always an error. Given a int variable named callsReceived and another int variable named operatorsOnCall write the necessary code to read values into callsReceived and operatorsOnCall and print out the number of calls received per operator (integer division with truncation will do). HOWEVER: if any value read in is not valid input, just print the message "INVALID".

User ProdigySim
by
4.2k points

2 Answers

1 vote

Final answer:

To calculate calls per operator in C++, read values into two integer variables, validate the input, and check for division by zero. If either the input is invalid or operators are zero, print "INVALID"; otherwise, perform the division and output the result.

Step-by-step explanation:

To handle the problem of dividing the number of calls received by the operators on call in C++, you will need to write code that includes input validation and conditional logic to check for division by zero. Here's an example of how you could write this code:

#include <iostream>

int main() {
int callsReceived, operatorsOnCall;
std::cin >> callsReceived >> operatorsOnCall;
if(std::cin.fail() || operatorsOnCall == 0) {
std::cout << "INVALID" << std::endl;
} else {
std::cout << (callsReceived / operatorsOnCall) << std::endl;
}
return 0;
}

This code reads the input for both callsReceived and operatorsOnCall. If the input is invalid or if operatorsOnCall is zero, it will print "INVALID". Otherwise, it will print the result of dividing callsReceived by operatorsOnCall, using integer division which truncates any decimal.

User Mceo
by
4.3k points
6 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int callsReceived, operatorsOnCall;

cout<<"Enter the number of received calls:";

cin >> callsReceived;

cout<<"Enter the number of operators on calls:";

cin >> operatorsOnCall;

if(operatorsOnCall !=0)

cout<< "Number of calls received per operator is: "<< callsReceived/callsReceived;

else

cout<<"INVALID";

return 0;

}

Step-by-step explanation:

Declare the variables

Ask the user for the inputs

Check if the second number is 0 or not. If it is not, calculate the number of calls received per operator - divide callsReceived by callsReceived, and print the result

Otherwise, print INVALID

User Sajjad Rezaei
by
3.9k points