[英]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.