简体   繁体   English

为什么即使我放了一个 break 语句,我的程序也会一直循环

[英]Why my program keeps looping even when I put a break statement

I am trying to make a log book program but I encountered this problem.我正在尝试制作一个日志程序,但遇到了这个问题。 It always loops even when there is a break statement.即使有 break 语句,它也总是循环。

Here is the part where I use a break statement on while loop but it keeps looping endlessly.这是我在 while 循环上使用 break 语句的部分,但它不断循环。

void userPrompt(){
            while(true){
                cout << "[1] Log in\n";
                cout << "[2] Log out\n";
                cout << "[3] View Log Book\n";
                cout << "[0] Exit\n";
                cout << "Choice: ";
                cin >> choice;
                cout << endl;
                switch(choice){
                    case 1:
                        logIn();
                        break;
                    case 2:
                        logOut();
                        break;
                    case 3:
                    viewRecords();
                        break;
                    case 4:
                        break;
                    default:
                        cout<<"Invalid Choice. PLease try again\n";
                        break;
                }
            }
        }

This is the whole code这是整个代码

#include<string>
using namespace std;
struct node{
    string name;
    int timeIn, timeOut;
    node* nextNode;
};
class LogBook{
    private:
        node* head = NULL;
        node* tail = NULL;
        int choice;
    public:
        void userPrompt(){
            while(true){
                cout << "[1] Log in\n";
                cout << "[2] Log out\n";
                cout << "[3] View Log Book\n";
                cout << "[0] Exit\n";
                cout << "Choice: ";
                cin >> choice;
                cout << endl;
                switch(choice){
                    case 1:
                        logIn();
                        break;
                    case 2:
                        logOut();
                        break;
                    case 3:
                    viewRecords();
                        break;
                    case 4:
                        break;
                    default:
                        cout<<"Invalid Choice. PLease try again\n";
                        break;
                }
            }
        }
        void logIn(){
            string name;
            int time;
            node* temp = new node();
            cout << "Enter your name: ";
            cin >> name;
            cout << "Time in: ";
            cin >> time;
            cout << endl;
            temp->name = name;
            temp->timeIn = time;
            temp->nextNode = NULL;
            
            if(head == NULL){
                head = temp;
                tail = temp;
            }
            else{
                tail->nextNode = temp;
                tail = tail->nextNode;
            }
        }
        void logOut(){
            string name;
            int time;
            node* temp;
            temp = head;
            cout << "Enter Name: ";
            cin >> name;
            while(temp != NULL){
                if(temp->name == name){
                    cout << "Time out: ";
                    cin >> time;
                    temp->timeOut = time;
                }
                else
                cout << "Name not found\n";
                    break;
            }
        } 
        void viewRecords(){
            node* temp;
            temp = head;
            while(temp != NULL){
                cout << "\nLog records:\n";
                cout << "Name: " << temp->name << endl;
                cout << "Time in: " << temp->timeIn << endl;
                cout << "Time out: " << temp->timeOut << endl;
                temp = temp->nextNode;
            }
        }
};
int main(){
    LogBook user1;
    user1.userPrompt();
    return 0;
}`

All of your break statements are inside of a switch .您所有的 break 语句都在switch内。 So they are breaking out of the switch and back into the while loop (which then loops endlessly).所以他们正在脱离switch并回到while循环(然后无限循环)。

The switch statement will fall-through to the other case statements unless you place the break , so those break calls are just terminating that particular case.除非您放置了break ,否则switch语句将贯穿其他case语句,因此这些break调用只是终止该特定情况。

You need a break statement somewhere in the top-level of your while to signify that you want to stop, since your condition is while(true) .您需要在while的顶层某处使用break语句来表示您想要停止,因为您的条件是while(true) Otherwise it will just loop forever.否则它将永远循环。

You use break statement that is only break switch statement, You need to add break statement for while loop also您使用的 break 语句只是 break switch 语句,您还需要为 while 循环添加 break 语句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使不满足条件,我的程序也会在第一个 do...while 循环中继续循环 - My program keeps looping in the first do...while loop even when condition isn't met 为什么当我放置偶数个元素时我的程序不起作用 - Why my program doesn't work when I put an Even Number of Elements 即使设置了限制,我的for语句仍在重复? - My for statement keeps repeating even though I set a limit? 我的for循环不断循环并导致程序崩溃 - My for loop keeps looping and causes the program to crash 当我将它放在我的 while 语句之外时,为什么我的 ofstream 不起作用? - Why won't my ofstream work when I put it outside my while statement? 为什么我的程序在完全循环8192个元素时会变慢? - Why is my program slow when looping over exactly 8192 elements? 为什么break语句会终止程序? - Why does break statement terminate program? 为什么即使我使用 break 语句也会得到 3 7 作为输出 /* 4 4 2 2 4 3 */ /* o/p 1 2 3 7 */ - why i am getting 3 7 as output even if i am using break statement /* 4 4 2 2 4 3 */ /* o/p 1 2 3 7 */ 当我删除cout语句时,为什么我的C ++程序会因退出代码11而崩溃? - Why does my C++ program crash with exit code 11 when I remove a cout statement? 当cin不是int时C++程序保持循环 - C++ program keeps looping when cin is not an int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM