Why is this do-while loop not working???

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.

And by the way, sorry if my code is hard to read.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
    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>

using namespace 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;
}
Last edited on
You are checking the wrong variable in your condition.
I'm checking the wrong variable? How?
I made the do-while loop so that if the user types in y or Y, it repeats? Or is my logic incorrect?
Last edited on
ans has int as type while you use this as char in your loop test.
1
2
3
 cout << "To repeat, type in y for yes, and n for no: ";
        cin >> response;
    } while (ans == 'y' or ans == 'Y');
I'm using the variable response, which is of type char, for the repeating of the do-while loop, not ans.
I'm using the variable response, which is of type char, for the repeating of the do-while loop, not ans."

while (ans == 'y' or ans == 'Y');
No you are not.
1
2
3
4
5
6
    do
    {
        // ...
        cout << "To repeat, type in y for yes, and n for no: ";
        cin >> response;
    } while (ans == 'y' or ans == 'Y');
OH I'm so stupid. Thanks for your guy's help, and sorry if I pulled a tantrum T-T
Topic archived. No new replies allowed.