Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ switch while infinite loop issue

I’m a total beginner at C++ and I’m trying to make a program which displays train schedule by selecting routes from the menu. I also want to make it possible to choose multiple options and quit manually. The issue is: every input creates an infinite loop.

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

struct {
    string Route;
    string depTime;
    string travelTime;
    string endDestTime;
} Schedule1, Schedule2, Schedule3;

int main()
{
    setlocale(LC_ALL, "RUSSIAN");
    int routeNum = 3;
    bool quit = false;
    
    Schedule1.Route = "Ярославль--Москва"; //route 
    Schedule1.depTime = "12.06.2023, 13:00"; //departure time
    Schedule1.travelTime = "4 часа"; //travel time
    Schedule1.endDestTime = "12.06.2023, 17:00"; //destination time
    
    Schedule2.Route = "Ярославль--Санкт-Петербург";
    Schedule2.depTime = "12.06.2023, 16:00";
    Schedule2.travelTime = "10 часов";
    Schedule2.endDestTime = "13.06.2023, 02:00";
    
    Schedule3.Route = "Ярославль--Екатеринбург";
    Schedule3.depTime = "12.06.2023, 12:00";
    Schedule3.travelTime = "20 часов";
    Schedule3.endDestTime = "13.06.2023, 08:00";
  
    
    //train routes to choose from
    cout<<"|----------------Расписание поездов-----------------| \n\n";
    cout<<"Город отправления:     Город прибытия: \n";
    cout<<"(1)  Ярославль         Москва \n";
    cout<<"(2)  Ярославль         Санкт-Петербург \n";
    cout<<"(3)  Ярославль         Екатеринбург \n";
    cout<<"(4)  Завершить" << endl;
    cout<<"Выберите номер маршрута: ";
    cin >> routeNum;

    do
    {
    switch (routeNum)
    {
        case 1:
        cout << "Маршрут: " << Schedule1.Route << " " << endl;
        cout << "Время отправления: " << Schedule1.depTime << " " << endl;
        cout << "Время в прибытия: " <<  Schedule1.endDestTime << " " <<endl;
        cout << "Время в пути: " << Schedule1.travelTime << " " <<endl;
        cout<<endl;
        break;
        
        case 2:
        cout << "Маршрут: " << Schedule2.Route << " " << endl;
        cout << "Время отправления: " << Schedule2.depTime << " " << endl;
        cout << "Время в прибытия: " <<  Schedule2.endDestTime << " " <<endl;
        cout << "Время в пути: " << Schedule2.travelTime << " " <<endl;
        cout<<endl;
        break;
        
        case 3:
        cout << "Маршрут: " << Schedule3.Route << " " << endl;
        cout << "Время отправления: " << Schedule3.depTime << " " << endl;
        cout << "Время в прибытия: " <<  Schedule3.endDestTime << " " <<endl;
        cout << "Время в пути: " << Schedule3.travelTime << " " <<endl;
        cout<<endl;
        break;
        
        case 4:
        quit = true; //quit program
        break;
        
        default:
        cout << "Выберите существующий номер маршрута"; //if input is invalid
    } 
    }
    while(!quit);

    
    return 0;
}

I know same issue has been brought before but I still can’t figure out what I’m missing here. Any help would be greatly appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

At least this statement

cin >> routeNum;

should be within the do-while loop as for example

do
{
cin >> routeNum;

switch (routeNum)
{
//...

Otherwise the variable routeNum is not being changed within the loop. As a result you have an infinite loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading