【发布时间】:2015-04-09 13:18:08
【问题描述】:
如何在每次循环中清除屏幕?我的意思是我不想再次打印菜单。当我选择一个选项时,这将告诉我您想要重复或返回主菜单或退出。是否可以使用if/else 或者我必须为此使用switch?
do
{
cout << "\t **Manue**" << endl;
cout << "1) Print a Table " << endl;
cout << "2) Find the Prime Number " << endl;
cout << "3) Find the Factorial" << endl;
cout << "4) Exit Program " << endl;
cout << "\nPlease select an option : ";
cin >> option;
if (option == 1)
{
int a, b;
cout << "Enter the number that you want to have its table" << endl;
cin >> a;
for (int c = 1; c <= 10; c++)
{
b = c*a;
cout << "The table is " << b << endl;
}
}
else if (option == 2)
{
int num, flag = 0, i = 2;
cout << "Enter the number that you want to check is a Prime or Not :" << endl;
cin >> num;
while (i < num)
{
if (num%i == 0)
{
flag = 1;
break;
}
i = i + 1;
}
if (flag == 0)
cout << "**Number is prime**\n" << endl;
else
cout << "\n**Number is not prime**\n" << endl;
}
else if (option == 3)
{
int i, fact = 1;
cout << "Enter the number to find its Factorial" << endl;
cin >> i;
for (int n = i; n > 1; n--)
{
fact = fact *n;
cout << fact << endl;
}
}
else
{
cout << "Invalid Option entered" << endl;
}
}
while (option != 4);
return 0;
}
【问题讨论】:
-
您可以在 Windows 上使用
system("cls")或在 Unix 上使用system("clear")作为快速破解。虽然不推荐。对于严肃的事情,你应该使用像 ncurses 这样的库。 -
你的 do while 应该被终止为 (option) ,并在菜单中使其 0) 退出
-
ncurses可能是矫枉过正。它占据了整个屏幕。 OP只想清除屏幕。 (就我个人而言,我认为这几乎总是一个坏主意;如果 your 程序清除了 my 屏幕,您可能正在擦除我不想擦除的信息。) -
@KeithThompson:ncurses 的好处之一是,当程序退出时,您的屏幕将恢复到之前的内容(如 反对到此处发布的所有其他解决方案)。
-
查看rlutil.h 在 Windows 和 Linux 中的可移植性。