【问题标题】:C++: Cannot delete characters already printed to console before outputtingC ++:在输出之前无法删除已打印到控制台的字符
【发布时间】:2016-11-25 03:06:44
【问题描述】:

我正在为学校的 C++ 课程做一个实验室。该程序从文件中读取二进制数,然后计算它们的十进制等值并将它们打印到控制台,但它必须逐个字符地读取整个文件。该程序应该忽略前导空格和 0。当程序到达无效数字时,我希望它退格到行首,然后缩进并打印出消息“输入错误”,如下所示:

所有“错误的数字输入”都应与二进制数列中的其余数字对齐,但正如您所见...

这是整个程序:

int main(void)
{
    //Declarations
    ifstream infile;
    char x = 0;
    int y = 0, i = 0, count = 0, q = 0, l = 0;
    //Prints out header to console
cout << left << setw(20) << "Binary Number" << right << setw(15) << "Decimal Equivalent" << endl;

//Opens data file
infile.open("INLABVII.dat");

//Will loop through file until it reaches <eof>
while(!infile.eof())
{
    //Gets character from file
    infile.get(x);

    //Digit is a valid binary digit
    if(x == '1' || x == '0')
    {
        //First binary digit read in
        if(y == 0)
        {
            //Backspace any leading spaces or zeros
            for(i = 0; i < count; i++)
            {
                cout << '\b';
            }

            //Add 6 spaces to indent line
            for(q = 0; q < 6; q++)
            {
                cout << " ";
                count++;
            }
            if (x == '1')       //First digit was a 1
            {
                y = y*2+1;
                cout << "1";
                count++;
                l++;
            }else               //First digit was a 0
            {
                y = y*2;
                cout << "0";
                count++;
                l++;
            }
        }else if(y != 0)
        {
            if (x == '1')       //Digit was a 1
            {
                y = y*2+1;
                cout << "1";
                count++;
                l++;
            }else               //Digit was a 0
            {
                y = y*2;
                cout << "0";
                count++;
                l++;
            }
        }
    }else if(x == ' ')          //Read in a space
    {
        if(y == 0)              //Was a leading space
        {
            count++;
        }else if(y != 0)        //Now testing for if it is a space at the end of the line
        {
            //Gets character from file
            infile.get(x);

            if(x == '\n')       //Space was at the end of the line
            {
                cout << right << setw(15) << y << endl;
                y = 0;
                count = 0;
            }else               //Space was either in the middle of the number, or there was more than one at the end of the line
            {
                //Backspace to beginning of current line on console
                for(i = 0; i < count; i++)
                {
                    cout << '\b';
                }

                //Add 6 spaces to indent line
                for(q = 0; q < 6; q++)
                {
                    cout << " ";
                    count++;
                }
                cout << left << setw(25) << "Bad digit on input" << endl;
                y = 0;
                count = 0;

                //Skip to next line if infile has invalid digit on current line
                infile.ignore(100, '\n');   //Skip bad input
            }
        }
    }else if (x == '\n')        //End of line
    {
        cout << right << setw(15) << y << endl;
        y = 0;
        count = 0;

    }else if(x != '1' && x != '0' && x != ' ' && x != '\n')     //Invalid digit was read in
    {

        //Backspace to beginning of current line on console
        for(i = 0; i < count; i++)
        {
            cout << '\b';
        }

        //Print out 6 spaces to indent line
        for(q = 0; q < 6; q++)
        {
            cout << " ";
            count++;
        }
        cout << left << setw(25)<< "Bad digit on input" << endl;
        y = 0;
        count = 0;

        //Should skip to next line if infile has invalid digit on current line
        infile.ignore(100, '\n');   //Skip bad input


    }

}

return 0;
}

这是我的输入文件中的内容(我用 _ 代替了“”,因此它们可见):

据我所知,循环 cout &lt;&lt; '\b'; 一定次数应该退格该数量的字符,但由于某种原因我的不是。

编辑:添加了cout &lt;&lt; '\r';。这是我的新输出。现在我有随机的空白行,并且带有前导零的输入仍然无法正确打印出来。

编辑:修复了前导零问题。添加cout &lt;&lt; '\r'; 只是将“Bad digit on input”移动到下一行,将已经打印的字符留在上面的行。

【问题讨论】:

  • 离题主题:while(!infile.eof()) 不适合你。更多信息:stackoverflow.com/questions/5605125/…
  • 一开始不写东西通常比事后清理控制台更容易。坚持你的输出,直到你知道你想写它,然后写它。
  • while(infile.get(x)) 还没有上课?奇怪的。我在您的代码中同时看到了 while 和 infile.get(x)。
  • 对于这个作业,我们无法保留输出。我们必须在读入时输出每个数字。我最初有,但它不会读取文件的最后一行
  • 我将while(!infile.eof()) 更改为while(infile.get(x)),它去掉了多余的0。在我说我必须使用while(!infile.eof()) 之前,我没有通读整个帖子。跨度>

标签: c++ eclipse console


【解决方案1】:

您可以尝试使用退格 \b 和空格的组合,如下所示:

// Delete the last character
std::cout << "\b \b" << std::flush;

// Delete the last two characters
std::cout << "\b\b  \b\b" << std::flush;

表达式返回多个字符,用空格覆盖已写入的内容,然后再次返回新的输出位置。

要返回到行首,请使用\r。您将不得不覆盖已写入当前行的所有内容。

【讨论】:

  • 它所做的只是将“输入错误的数字”移动到下一行。已经打印的字符仍然没有被删除
  • '\b' 在 macos(10.14.6) 上失败
【解决方案2】:

这个问题与 MacOS 有关。我尝试退格固定输入,即使我只输出'\b',也没有删除任何内容。我在 Visual Studio 中运行我的程序并按预期输出,因此我得出结论,MacOS 上的 Eclipse 或 MacOS 本身不支持'\b'。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多