Problems in do/while loop

I think I'm having problems with my do/while loop in my program.

I am creating a car wash program. The program is almost done except for this one error. When it asks the user "Would you like a car wash?" if the user inputs no, my program simply repeats the menu. I need to fix it to where if the user enters no, my program exits and simply says "Thank you. Please come again."

Thank you in advance!

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
/*************************************************
Name: Brittany White
Part: eDrive Thru Car Wash
Date: 4-22-14
*************************************************/

#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;

int main ()

{
    // Variables used
    char answer;
    char choice;
    bool response;
    const streamsize Max =
    std::numeric_limits<std::streamsize>::max();

    // Displays a menu
    cout << "\n\t\tWelcome to the Soaps N' Suds Car Wash.\n\n";

    do
    {
        // Grabs user answer
        cout << "\n\nWould you like a car wash today?\n";
        cin >> answer;
        cin.ignore (Max, '\n');
        answer = toupper (answer);

        if (answer == 'Y')
        {
            // Grabs user choice
            cout << "\nWhich wash would you like: Basic, Premium, or Deluxe?\n";
            cin >> choice;
            cin.ignore (Max, '\n');
            choice = toupper(choice);

            switch (choice)
            {
                case 'B': case 'b':

                    // Verifies user choice
                    cout << "\nYou have selected the Basic Car Wash. Is that okay?\n";
                    cin  >> answer;
                    cin.ignore(Max, '\n');
                    answer = toupper(answer);

                    if ('Y' == toupper(answer))
                    {

                        // Displays Basic car wash features
                        cout << "\n\nThis option includes our basic soft cloth wash.\n";
                        cout << "\nYour vehicle is now nice and clean.\n";
                        cout << "\nYour total will be $5.00.\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                    response = true;
                    break;

                    case 'P': case 'p':

                        // Verifies user choice
                        cout << "\nYou have selected the Premium Car Wash. Is that okay?\n";
                        cin  >> answer;
                        cin.ignore(Max, '\n');
                        answer = toupper(answer);

                    if ('Y' == toupper (answer))
                    {

                        // Displays Premium car wash features
                        cout << "\n\nThis wash leaves your vehicle sparkling clean.\n";
                        cout << "\nOur special coating is included for extra shine.\n";
                        cout << "\nYour total will be $10.00.\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                    response = true;
                    break;

                    case 'D': case 'd':

                        // Verifies user choice
                        cout << "\nYou have selected the Deluxe Car Wash. Is that okay?\n";
                        cin >> answer;
                        cin.ignore (Max, '\n');
                        answer = toupper(answer);

                    if ( 'Y' == toupper(answer))
                    {
                        // Displays Deluxe car wash features
                        cout << "\n\nThis option is our very best wash. It includes our special coating and wax.\n";
                        cout << "\nYou now have the cleanest and shiniest car in town!\n";
                        cout << "\nYour total will be $15.00\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else (answer == 'N');
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                        response = true;
                        break;
             }
                    }
                }
            while ('N' == toupper(answer));
            return 0;
        }
while ('N' == toupper(answer));

The user tries to quit by typing N, this loop continues while it is equal to N. Change your == to !=.

Also, in the same way you used toupper to compare answers, you could shorten your code a little bit by using tolower before passing it into a switch statement, so you can have just a single case.
Last edited on
Your welcome! Glad I could help.
It worked. However, now it interferes with the other part of my program.

It will no longer repeat the menu when it asks the user "You have selected the ___ car wash. Is that okay?" if the user enters no, it just exits the program instead of repeating the menu.
There are a number of ways you could go about this.

You could use a bool value.

bool programOver = false;

while ( !programOver )

and only in the one statement you want to ask if they want to continue the program, use a bool value. For instance.

cout << "Do you want to continue?" << endl;
cin >> answer;
if ( answer == 'n' )
{ programOver = true; }
I'm a bit confused about what exactly you want me to do.
SNIPPETS OF CODE I CHANGED BELOW
1
2
3
4
5
char answer;
    char choice;
    bool response, programOver; //<--------------------------------- I ADDED THIS
    const streamsize Max =
    std::numeric_limits<std::streamsize>::max();

1
2
3
 if ( answer == 'N' ) { programOver = true; break; } // <--- I ADDED THIS

        else if (answer == 'Y')

 
 while ( !programOver );



THE WHOLE PROGRAM I CHANGED
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
int main ()

{
    // Variables used
    char answer;
    char choice;
    bool response, programOver; //<--------------------------------- I ADDED THIS
    const streamsize Max =
    std::numeric_limits<std::streamsize>::max();

    // Displays a menu
    cout << "\n\t\tWelcome to the Soaps N' Suds Car Wash.\n\n";

    do
    {
        // Grabs user answer
        cout << "\n\nWould you like a car wash today?\n";
        cin >> answer;
        cin.ignore (Max, '\n');
        answer = toupper (answer);

       if ( answer == 'N' ) { programOver = true; break; } // <--- I ADDED THIS

        else if (answer == 'Y')
        {
            // Grabs user choice
            cout << "\nWhich wash would you like: Basic, Premium, or Deluxe?\n";
            cin >> choice;
            cin.ignore (Max, '\n');
            choice = toupper(choice);

            switch (choice)
            {
                case 'B': case 'b':

                    // Verifies user choice
                    cout << "\nYou have selected the Basic Car Wash. Is that okay?\n";
                    cin  >> answer;
                    cin.ignore(Max, '\n');
                    answer = toupper(answer);

                    if ('Y' == toupper(answer))
                    {

                        // Displays Basic car wash features
                        cout << "\n\nThis option includes our basic soft cloth wash.\n";
                        cout << "\nYour vehicle is now nice and clean.\n";
                        cout << "\nYour total will be $5.00.\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                    response = true;
                    break;

                    case 'P': case 'p':

                        // Verifies user choice
                        cout << "\nYou have selected the Premium Car Wash. Is that okay?\n";
                        cin  >> answer;
                        cin.ignore(Max, '\n');
                        answer = toupper(answer);

                    if ('Y' == toupper (answer))
                    {

                        // Displays Premium car wash features
                        cout << "\n\nThis wash leaves your vehicle sparkling clean.\n";
                        cout << "\nOur special coating is included for extra shine.\n";
                        cout << "\nYour total will be $10.00.\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                    response = true;
                    break;

                    case 'D': case 'd':

                        // Verifies user choice
                        cout << "\nYou have selected the Deluxe Car Wash. Is that okay?\n";
                        cin >> answer;
                        cin.ignore (Max, '\n');
                        answer = toupper(answer);

                    if ( 'Y' == toupper(answer))
                    {
                        // Displays Deluxe car wash features
                        cout << "\n\nThis option is our very best wash. It includes our special coating and wax.\n";
                        cout << "\nYou now have the cleanest and shiniest car in town!\n";
                        cout << "\nYour total will be $15.00\n\n";

                        cout << "\n\nThank you for using the Soaps N' Suds Car Wash! :)\n";
                        break;
                    }
                    else (answer == 'N');
                    {
                        // Returns to menu
                        cout << "\nI'm sorry. Please try again.\n";
                    }
                        response = true;
                        break;
             }
                    }
                }
            while ( !programOver ); // <--------------------------- I CHANGED THIS
            return 0;
        }
Last edited on
Thank you so much. I wasn't expecting you to edit the whole code but I appreciate it. That was very helpful!! Now I know for future reference what I can do to solve that problem.

Thanks again!!

You're welcome, glad I could help. Sometimes it's hard, at least for me, to learn without someone editing my code and showing me how to incorporate things into it. After you see it used, you're like, oh, ok!
Yes, exactly!

Also, there still seems to be another little bug in my code. We have fixed the main issue but now there is another small problem.

After the user has already received a car wash and it says "Thank you for using the Soaps N' Suds Car wash!" it will repeat the menu "Would you like a car wash?" again like an infinite loop.

I need to change it to where it quits the program after the user has already gone through the process once.

Any ideas?

Edit: I thought the breaks in my program were supposed to take care of that.
Last edited on
Short of starting over and re-thinking how we structure the program, an easy/temp fix ( not the best fix ) would be to at the end of each car wash, set programOver to true.
Okay, I will get it figured out. Thanks for your help! :)
A break simply ends the current function prematurely, you are using breaks at the end of a function, so it's in effect not doing anything. Your program is going to keep on executing until programOver is true, and currently the only way it is being set to true is at the very beginning when the user says no.
I will post an example in a few of a reworked version, look it over carefully :)
Okay :)
MaxDaniels wrote:
A break simply ends the current function prematurely, you are using breaks at the end of a function, so it's in effect not doing anything.


A break; breaks out of a looping structure (switch, while, for, do-while). The OP has them at the end of each case, which is normal practice. To leave a function, one uses a return statement.

@imbritty

If it was me I wouldn't bother asking if they want a car wash as this seems to be the purpose of the program. So you could get rid of the do-while loop altogether. Same for asking if each user selection is OK, from a GUI point of view, I would find that a little irritating.

Line 106: An else cannot have a condition associated with it, it is meant to catch anything that doesn't match any of the else if, so it is useful for catching error conditions. Also you have a semicolon, which negates the compound statement on lines 107 to 110, and that should have attracted a compiler error. Which compiler are you using? Set the warning level to it's maximum.

Just like an else if chain should have an else to catch errors, so should a switch have a default: case - it will save you one day. Also search on this site for TheIdeasMan bool controlled while loop for posts I have made about this.

It seems to me that you have too much code in main(), there is an unwritten rule that says that functions should be 40 LOC max, some people try for even less. Each case in the switch could call a function, say and displaying a menu could be in it's own function. With line 36, it might be better to show a menu, and have the user select from a choice B, P, D or 1, 2, 3 : with each option labelled as to what it does. At the moment what would happen if the user entered "Basic" ?

If using functions in your code, declare them before main, and define them in order after main.

Most importantly, ALWAYS initialise your variable to something this is often the biggest cause of errors. Declare & initialise 1 variable per line, and only have 1 statement per line generally in your code.

Don't have using namespace std;, google why that is so - I have written a number of replies about that.

There you go, some stuff to think about :+)


Topic archived. No new replies allowed.