为什么系统( "CLS" ) 在 while 中不起作用?
Why system("CLS") in while is not working?
我想在选择选项后清除屏幕,但我不知道它不起作用。它将显示Display()函数中的内容以及创建新购买和其他东西。这是因为在循环中?
while (selection != -1) // While for create new purchase
{
cout << "Create New Purhcase" << endl << endl;
cout << "1. Display Item" << endl;
cout << "2. Create a New Purchase" << endl << endl <<endl;
cout << "0. Back to Main Menu" << endl;
cout << "Enter Option:";
cin >> selection;
//Back to main menu
if (selection == 0)
{
system("CLS");
break;
}
if (selection == 1)
{
system("CLS");
cout << "Display Menu" << endl;
Display();
}
void Display()
{
system("CLS");
temp = itemHead; //start at the first node
cout << "Dispaly Menu" << endl << endl;
while (temp != NULL)
{
cout << "ID:" << temp->itemid << endl;
cout << "Item Name:" << temp->name << endl;
cout << "Item Type:" << temp->type << endl;
cout << "Item Price" << temp->cost << endl;
cout << endl << endl;
temp = temp->next; //forward to the next node
}
}
这里的问题是,您的代码在打印Display()
函数后不会停止,因为它是While循环的一部分。结果,它打印了菜单,然后继续打印选项。
要确保打印菜单后循环暂停,请将您的代码更改为:
if (selection == 1)
{
system("CLS");
cout << "Display Menu" << endl;
Display();
cout << endl << endl;
system("pause");
}
system("pause")
也是算法文件的一部分,因此您无需包含任何内容。这样,整个菜单将从您的Display()
函数,然后是一些新线来打印,最后是按Enter键的提示。直到您击中Enter键,while循环将不会继续。
注意:还有其他方法可以做到,但这是最简单和最短的方法。如果您对我的答案有任何疑问,或者我的答案不起作用,请在评论框中通知我。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 嵌套While循环不起作用(C++问题)
- 为什么在 while 循环中返回表达式不起作用
- For 和 while 循环在全局定义的函数中不起作用
- 为什么 For 循环中的 While 循环在我的程序中不起作用?
- 为什么系统( "CLS" ) 在 while 中不起作用?
- 无限 while 循环 - 字符关闭不起作用C++
- 从文件中读取行,while(getline()) 不起作用
- Do While和While循环在C++代码中不起作用
- 构造 do while 循环以处理C++不起作用的帐号
- 有人可以帮助我弄清楚为什么我的 Do/While 循环不起作用吗?(C++)
- std::cin在do-while循环中不起作用
- getline() 在 while 循环中第二次不起作用
- While循环在链表中不起作用
- 一个简单的c++ While循环不起作用
- While循环在使用布尔值(C++)的程序中不起作用
- while循环中的文件处理不起作用
- cin.getline() 在 while 循环中不起作用
- 这种情况似乎不起作用。(while (dAmount != (2*dbAmount)))此外,第一次计算不正确 o.O