!!!!!!IT HAS TO BE WRITTEN IN C++ PLEASE DO NOT COPY AND PASTE PREVIOUS ONE SINC
ID: 3731681 • Letter: #
Question
!!!!!!IT HAS TO BE WRITTEN IN C++ PLEASE DO NOT COPY AND PASTE PREVIOUS ONE SINCE IT IS NOT CORRECT HAS TO BE C++ NOT JAVA!!!!!!!!!!!!!
NAME: DUE: 3IO Write a program to implement the counter of programming project 3, page 314-315 of your textbook. Turn in your source code and this verification sheet. Verify your code by one of the LTA's or myself. The Counter class must have at least two constructors. The default constructor should count up to a maximum value of 9999. The other constructor should allow data less than 9999 to determine the maximum count. Write a main() driver to test the Counter. Choose any output format that you want. The following is just an example. However, the test values should be the same. The user entries in bold should be same for the program verification.. Maximum value of the counter is 9999. Current value of the counter is 0. Enter a key, either a, s, d, f, followed by 1-9. or o for overflow cents, s>dimes, d-dollars, ftens of dollars a a9 $9 d9 There is no overflow. Verified Enter a key, either a, s, d, f, followed by 1-9. or o for overflow a>cents, s->dimes, d dollars, ftens of dollars al You have gone over the amount. Verified Please enter an integer amount less than 9999 to initialize the co 5230 Maximum value of the counter is 5230 Current value of the counter is 0. Verified Date Verified By Comments:Explanation / Answer
#include<iostream>
#include<string>
#include <sstream>
using namespace std;
class Counter {
private:
int counter;
int max_count;
public:
Counter(){
counter = 0;
max_count = 9999;
}
Counter(int a){
counter = 0;
max_count = a;
}
void setCounter(int a){
counter = a + counter;
}
int getCounter(){
return counter;
}
int getMaxCount(){
return max_count;
}
};
int main(){
string data;
int d;
int a;
Counter *c = new Counter();
while(true){
cout << "Maximum value of the counter is " << c->getMaxCount() << endl;
cout << "Current value of the counter is " << c->getCounter() << endl;
cout << "Enter a key, either a, s, d, f, followed by 1-9 or o for overflow: ";
while (true) {
cin >> data;
if (data[0] == 'a'){
d = data[1] - '0';
c->setCounter(d);
}
else if (data[0] == 's'){
d = data[1] - '0';
c->setCounter(d*10);
}
else if (data[0] == 'd'){
d = data[1] - '0';
c->setCounter(d*100);
}
else if (data[0] == 'f'){
d = data[1] - '0';
c->setCounter(1000*d);
}
else if (data[0] == 'o'){
//cout << c.getCounter() << endl;
if (c->getCounter() > c->getMaxCount()){
cout << "You have gone over the amount ";
break;
}
else {
cout << "There is no overflow ";
}
}
}
cout << "Please enter an integer amount less than 9999 to initialize the counter: ";
cin >> a;
c = new Counter(a);
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.