如何在每次循环迭代之前清除屏幕?

时间:2015-02-09 17:40:43

标签: c++ c

如何通过循环每次清除屏幕?我的意思是我不想再次打印菜单。当我选择一个选项时,这将显示您要重复或返回主菜单或退出。是否可以使用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;
}

8 个答案:

答案 0 :(得分:3)

如果不使用隐藏终端可能特性的库,您将找不到便携式方法。这就是为什么像curses这样的图书馆及其所有衍生品如ncurses都被编写的原因。

而且奥尔德仍然可以记住我们使用打印终端的时间,是的,就像带有串行接口的打字机:键盘上输入的所有东西都送到了计算机上,所写的一切都转到了......纸上!没理由清楚那个菜单......

好的那些日子已经过去了,但是不要指望在Windows下的cmd.exe,Unix类似系统的控制台以及任何终端仿真程序上清除屏幕的简单方法。

所以你只有三个选择:

  • 严格携带,只输出几条新线 - 没有清除,只有垂直间距
  • 便携性较差,请尝试\f适用于不同系统(但不保证无处不在)
  • 使用ncurses衍生库。

答案 1 :(得分:1)

另一个简单的想法是,您可以使用system命令并发出clear(或cls)以清除输出中的屏幕并显示/刷新新状态你的菜单。如果你想创建一些简单的东西,这不是一个坏主意,因为你不必阅读有关ncurses等的任何内容。另外,定义一个形式的常量

#define CLEARSCREEN system ( "cls" )

这样你就可以很快地在cls / clear之间切换。请注意,在Windows上我认为这也需要库process.h;即你还需要一份表格

的陈述
#include <process.h>

在某处。

答案 2 :(得分:1)

<强>的Ncurses。

扩展Serge的答案,<iostream>std::cout主要用于基于行的输出,没有&#34;屏幕&#34;的概念。如果你想提供一个菜单驱动的用户界面,提供的选项,用户输入和响应用户输入循环生成的输出,你应该真正看看ncurses,它提供了那种功能。

Ncurses在许多平台上都受支持,并提供许多不同语言的绑定。但是,对于一个简单的例子,我选择&#34; raw&#34; C,这很容易在C ++代码中采用:

#include <ncurses.h>
#include <string.h>

#define LINELEN 50

int main()
{
    int c;
    char input[LINELEN];

    initscr();

    do {
        erase();
        mvprintw( 1, 1, "**Manue**\n" );
        mvprintw( 2, 1, "1) Print a Table\n" );
        mvprintw( 3, 1, "2) Find the Prime Number\n" );
        mvprintw( 4, 1, "3) Find the Factorial\n" );
        mvprintw( 5, 1, "4) Exit Program\n" );
        mvprintw( 7, 1, "Please select an option.\n" );
        refresh();

        getnstr( input, LINELEN );

        erase();
        mvprintw( 1, 1, "You entered: '%s'. (Press a key.)\n", input );
        refresh();

        int c = getch();
    } while ( strcmp( input, "4" ) );

    endwin();

    return 0;
}

应该很容易从那里扩展。关于Keith Thompson的评论,这具有实际上擦除用户终端的额外好处 - 一旦调用endwin(),就会恢复先前的内容。

答案 3 :(得分:0)

添加标题文件

#include < stdlib.h >

并使用函数

system("cls");

您想要清除屏幕的任何地方。

答案 4 :(得分:0)

在我看来,你并不想要完全清除屏幕 - 就像你写的那样:

  

第一次选择选项时出现问题1.它会清除屏幕。这没关系。但当它显示一些结果时,它会再次自动显示菜单。我不想要。我希望在执行后,程序会询问用户您要转到菜单。按这个你要重复按这个。退出这个......

我想你可能在谈论子菜单:当用户在菜单中选择选项时,你想给用户一个选择:是否再次执行相同的操作或返回菜单。这是菜单子菜单的一个简单示例:

#include <iostream>

void print_main() {
    std::cout << "Main menu: [a, b, q (quit)]" << std::endl;
}

void print_submain() {
    std::cout << "Go back to main? [y/n]" << std::endl;
}

void submain() {
    char suboption;
    do {
        std::cout << "Do domething" << std::endl;
        print_submain();
        std::cin >> suboption;
    } while ( suboption != 'y');
}

int main() {
    char option;
    do {
        print_main();
        std::cin >> option;
        // it doesn't matter if switch or if-else is used
        switch(option) {
        case 'a':
            submain();
            break;
        case 'b':
            submain();
            break;
        default:
            break;
        }
    } while (option != 'q');
    return 0;
}

您可以使用不同的submain功能,执行不同的操作,上面的示例用户可以根据需要执行它们,然后返回主菜单。

答案 5 :(得分:0)

system("pause");之前执行system("cls");。用户必须按ENTER或任意键才能返回菜单。

答案 6 :(得分:0)

在每次迭代中使用此功能: -

python -m scripts.retrain \
--bottleneck_dir=tf_files/bottlenecks \
--how_many_training_steps=500 \
--model_dir=tf_files/models/ \
--summaries_dir=tf_files/training_summaries/%ARCHITECTURE% \
--output_graph=tf_files/retrained_graph.pb \
--output_labels=tf_files/retrained_labels.txt \
--architecture="%ARCHITECTURE%" \
--image_dir=tf_files/flower_photos

答案 7 :(得分:0)

cout << "\033[2J\033[1;1H";
相关问题