Question: C++ code. MY CODE WORKS FINE, I JUST DONT UNDERSTAND THE ERROR IM GETTING. Okay so this is my assignment: Define a class counterType to

C++ code.

MY CODE WORKS FINE, I JUST DONT UNDERSTAND THE ERROR IM GETTING.

Okay so this is my assignment:

Define a class counterType to implement a counter. Your class must have a private data member counter of type int. Define a constructor that accepts a parameter of type int and initializes the counter data member. Add functions to: Set counter to the integer value specified by the user. Initialize counter to 0. Return the value of counter with a function named getCounter. Increment and decrement counter by one. Print the value of counter using the print function. Example output: Counter = 0.

and that works fine, however this is on cengage Mindtap, so when i run the checks, this is what i get

counterType's print prints the value of counter,

test 1 Build Status Build Failed Test Output [==========] Running 1 test from 1 test case. [----------] Global test environment set-up. [----------] 1 test from print [ RUN ] print.1 /root/sandbox1773d70c/nt-test-aee6a942.cpp:12: Failure Value of: "Counter = 4" Expected: output Which is: "counter: 4" [ FAILED ] print.1 (0 ms) [----------] 1 test from print (0 ms total) [----------] Global test environment tear-down [==========] 1 test from 1 test case ran. (0 ms total) [ PASSED ] 0 tests. [ FAILED ] 1 test, listed below: [ FAILED ] print.1 1 FAILED TEST Test contents: TEST(print, 1) { testing::internal::CaptureStdout(); counterType counter(5); counter.decrementCounter(); counter.print(); std::string output = testing::internal::GetCapturedStdout(); output.erase(std::remove(output.begin(), output.end(), ' '), output.end()); ASSERT_EQ(output, "Counter = 4"); }

This is my code, for your convenience:

Main.cpp:

#include

#include "counterType.h"

using namespace std;

int main()

{

counterType counter1;

counterType counter2(5);

counter1.print();

cout << endl;

counter1.incrementCounter();

cout << "After Increment counter1: " << counter1.getCounter() << endl;

cout << "Counter2 = " << counter2.getCounter() << endl;

counter2.decrementCounter();

cout << "After decrement counter2 = " << counter2.getCounter() << endl;

counter1.setCounter(-6);

cout << "After resetting counter1: " << counter1.getCounter() << endl;

return 0;

}

counterType.h

#ifndef COUNTER_H

#define COUNTER_H

#include

class counterType {

private:

int counter;

public:

counterType(int c=0);

void incrementCounter();

void decrementCounter();

void print();

int getCounter();

void setCounter(int c=0);

};

#endif

counterTypeImp.cpp

#include "counterType.h"

counterType::counterType(int c) {

setCounter(c);

}

void counterType::incrementCounter() {

counter++;

}

void counterType::decrementCounter() {

counter--;

if(counter < 0) {

counter = 0;

}

}

void counterType::print() {

std::cout << "counter: " << counter << std::endl;

}

int counterType::getCounter() {

return counter;

}

void counterType::setCounter(int c) {

if(c < 0) {

c = 0;

}

counter = c;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!