'The value of a must be 56 but when i run this program i am getting 0 [closed]
I am trying to test this program on google test but test cases fails. This code is supposed to give me 56 in the output but i am getting 0 in output. where is the mistake? Also tell me about the scope of static variables like how can i access private static variable outside the class
#include <iostream>
using namespace std;
class CSR {
private:
int complaintsResolved;
static int totalComplaintsResolved;
public:
void setComplaintsResolved(int cpsResolved)
{
if (cpsResolved < 0)
return;
complaintsResolved = cpsResolved;
}
static void setTotalCpsResolved(int totalCpsResolved)
{
totalComplaintsResolved = totalCpsResolved;
}
int getComplaintsResolved()
{
return complaintsResolved;
}
static int getTotalCpsResolved()
{
return totalComplaintsResolved;
}
};
int CSR::totalComplaintsResolved = 0;
void calcTotalComplaints(CSR employees[7])
{
int a = 0;
for (int i = 0; i < 7; i++)
a += employees[i].getTotalCpsResolved();
cout << a;
CSR::setTotalCpsResolved(a);
}
int main()
{
CSR employees[7];
for (int i = 0; i < 7; i++) {
employees[i].setComplaintsResolved((i + 1) * 2);
}
calcTotalComplaints(employees);
}
Test Cases
TEST(Q5, second)
{
CSR employees[7];
for (int i = 0; i < 7; i++) {
employees[i].setComplaintsResolved((i + 1) * 2);
}
calcTotalComplaints(employees);
ASSERT_EQ(56, CSR::getTotalCpsResolved());
Solution 1:[1]
You wrote
a += employees[i].getTotalCpsResolved();
instead of
a += employees[i].getCpsResolved();
and since CSR::totalComplaintsResolved is initially set to 0 and never changed before accessing CSR::getTotalCpsResolved it just keeps adding 0 to a
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tidemor |
