简体   繁体   中英

Trying to find out why my code keeps infinite looping

I am very new to programming so pardon my lack of knowledge. I am trying to create a simple menu in which I am going to execute a few problems by pressing 1,2,3 etc. but my code keeps looping over and over again and I can't understand why.

int main()
{
    int choice;
    do
    {
        cout << "\t|--------------------------- Menu ---------------------------|" << endl;
        cout << "1.|- Добавяне на телефонни номера -|" << endl;
        cout << "2.|- Извеждане на всички телефонни абонати на екрана -|" << endl;
        cout << "3.|- Месечно потребление -|" << endl;
        cout << "4.|- Изчисление на месечна такса -|" << endl;
        cout << "5.|- Справки за абонатите с под меню -|" << endl;
        cout << "6.|- Край на програмата -|" << endl;

    switch(choice)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                break;
            case 6:
                    cout << "|- Благодаря ви -|" << endl;
                return 0;
        }  

   }       
while(choice != 6);
}

You need to use cin in your code in order to change what choice is every time that you go through the loop. Also it is a good idea to handle invalid user inputs. This is done by (!(cin >> choice)) until input will return true . While this is the case you want to cin.clear() and cin.ignore() to reset the state of the stream so that you can keep asking the user for a correct input.

#include<iostream>
using namespace std;

int main()
{
    // it is a good idea to initialize the variable when you create it
    int choice = 0;
    do
    {
        cout << "\t|--------------------------- Menu ---------------------------|" << endl;
        cout << "1.|- Добавяне на телефонни номера -|" << endl;
        cout << "2.|- Извеждане на всички телефонни абонати на екрана -|" << endl;
        cout << "3.|- Месечно потребление -|" << endl;
        cout << "4.|- Изчисление на месечна такса -|" << endl;
        cout << "5.|- Справки за абонатите с под меню -|" << endl;
        cout << "6.|- Край на програмата -|" << endl;


        // this loop will handle incorrect inputs from the user
        // for example entering a char, when the stream is expecting an int
        while (!(cin >> choice))
        {
            cin.clear();
            cin.ignore();
            cout << "Please enter a valid choice: ";
            cin >> choice;
        }

        switch (choice)
        {
        case 1:
            break;
        case 2:
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            cout << "|- Благодаря ви -|" << endl;
            return 0;
        }

    } while (choice != 6);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM