Jack Drayton
Jack Drayton

Reputation: 11

The "continue" statement doesnt transfer control to the starting of the loop

I'm a beginner when it comes to programming and recently my professor asked me to make a program for the parking lot. The problem I'm facing is that when my compiler encounters a continue statement inside my switch statement, it doesn't transfer the control to the starting of the loop where the user is required to choose an option.

Here's my code:-

#include <iostream>
#include <string.h>
#include <fstream>
#include <vector>

int rick_amnt = 100, car_amnt = 200, busses_amnt = 300;
int car_parked = 0;

using namespace std;

int main()
{
    int type, menu;
    char confirm;

    do {
        cout << "-------MENU--------\n";
        cout << "01 : Parking tickets" << endl;
        cout << "02. Parked cars" << endl;

        cout << "Choose an option : ";
        cin >> menu;

        system("cls");

        if (menu == 1) {

            cout << "01: Rickshaw\n02: Car\n03: Bus\n";
            cout << "what is your car type : ";
            cin >> type;

            switch (type) {
            case 1: {
                cout << "Rickshaw parking ticket = " << rick_amnt << endl;
                cout << "do you wish to park your Rickshaw?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successfull payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            case 2: {
                cout << "Car parking ticket = " << car_amnt << endl;
                cout << "do you wish to park your Car? (y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successfull payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            case 3: {
                cout << "Buses parking ticket = " << busses_amnt << endl;
                cout << "do you wish to park your Bus?(y/n) ";
                cin >> confirm;
                if (confirm == 'y') {
                    cout << "Successful payment!" << endl;
                    car_parked++;
                    break;
                }
                else {
                    cout << "Car parking declined" << endl;
                    continue;
                }
            }

            default:
                cout << "invalid identity" << endl;
            }
        }

        else if (menu == 2) {
            cout << "Parked cars = " << car_parked << endl;
        }
        else
            cout << "Invalid entry" << endl;

        cout << "Ty again?(y/n) ";
        cin >> confirm;
        system("cls");
    } while (confirm == 'y');

    cout << "Successfully exited the parking lot.\nHave a nice day!" << endl;

    return 0;
}

I want my code to function in a way that if the user inputs any other character besides 'y' when asked to confirm parking, the parking is declined and control is transferred to the beginning of the loop where user should choose an option again. But, when I input any other character, my code just ends all the lines of code and exits the loop. Isn't the continue statement supposed to transfer the control towards the beginning of the loop?

Upvotes: 1

Views: 784

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51815

Isn't the continue statement supposed to transfer the control towards the beginning of the loop?

Your error comes from a misunderstanding of what a continue statement does in C++: It does not transfer control to the beginning of its containing loop! Rather, it causes the remaining code in that loop to be skipped – thus transferring control to the end of the loop (cppreference page).

Now, in (most) for and while loops, when reaching the end of the loop body (typically, the closing }), control is (effectively) transferred immediately to the beginning of the loop, where the loop condition is re-evaluated, to see if another iteration of that loop is to be performed.

However, in the case of a do { ... } while(condition); loop, that doesn't happen. When control reaches the closing }, the condition is evaluated and only if that evaluation yields "true" does control pass to the beginning of the loop.

In your code, when any of the three continue statements are encountered, the confirm variable must have a value that is not equal to 'y' (due to the fact they are all in else blocks following an if (confirm == 'y') block). So, in each of those cases, the while (confirm == 'y') will yield "false" and the loop will not run again.

To fix the problem, you can add an explicit confirm = 'y'; statement immediately before each of the continue; statements. However, as hinted in the comments, it may be better to re-think your loop logic and reduce much of the code-duplication you currently have.

Upvotes: 1

Related Questions