Hi, everyone. I'm having a really big problem here. Everything in this program works fine except for the do-while loop, which won't repeat when I type in y for yes. Does anyone know what the problem is in here? Thanks for your help.
/*
File: count.cpp
Description: Define a class for a type called CounterType. An object of this type is used to count things, so it records
a count that is a nonnegative whole number. Include a default constructor that sets the counter to 0 and a constructor
with one argument that sets the counter to the value specified by its argument. Include member functions to increase the
count by one and to decrease the count by one. Be sure that no member function allows the value of the counter to become
negative. Also, include a member function that returns the current count value and one that outputs the count to a stream.
The member function for doing output will have one formal parameter of type ostream for the output stream that receives
the output. Embed your class definition in a test program.
Created: November 11, 2014
*/
#include <iostream>
#include <cstdlib>
usingnamespace std;
// The counting class used in this program.
class CounterType
{
public:
CounterType(int number);
// Assigns the number of digits in the number to the user.
CounterType( );
// Initializes the counter to 0.
void output1(ostream& out);
// Outputs the current count to the user.
void output2(ostream& out);
// Outputs the new count to the user.
void output3(ostream& out);
// Outputs the count to the user.
private:
int counting;
void increase();
// Increases the count by 1.
void decrease();
// Decrease the count by 1.
};
int main( )
{
CounterType count1;
count1.output2(cout);
return 0;
}
CounterType::CounterType()
{
counting = 0;
}
CounterType::CounterType(int number)
{
counting = number;
}
void CounterType::output1(ostream& out)
{
out << "Test Program running..." << endl;
out << "Current count: " << counting << endl;
}
void CounterType::increase()
{
counting += 1;
}
void CounterType::decrease()
{
counting -= 1;
if (counting < 0)
{
cout << "Negative count not permitted..." << endl;
cout << "Setting count equal to 0." << endl;
counting = 0;
}
}
void CounterType::output2(ostream& out)
{
int ans = 0;
char response;
CounterType choice;
choice.output1(cout);
do
{
cout << "Would you like to modify the count?" << endl;
cout << "Type in 1 to increase it by 1," << endl;
cout << "Type in 2 to decrease it by 1," << endl;
cout << "Type in 3 to display the count," << endl;
cout << "Or type in 4 to exit the program: ";
cin >> ans;
while (ans != 1 && ans != 2 && ans != 3 && ans != 4)
{
cout << "Invalid number." << endl;
cout << "Type in a number for the four choices above: ";
cin >> ans;
}
if (ans == 1)
{
choice.increase();
}
if (ans == 2)
{
choice.decrease();
}
if (ans == 3)
{
choice.output3(cout);
}
if (ans == 4)
{
cout << "The program will now exit." << endl;
exit(1);
}
choice.output3(cout);
cout << "To repeat, type in y for yes, and n for no: ";
cin >> response;
} while (ans == 'y' or ans == 'Y');
cout << "The program will now exit." << endl;
}
void CounterType::output3(ostream& out)
{
cout << "Count is currently equal to " << counting << endl;
}