简体   繁体   English

C ++:在输出之前无法删除已经打印到控制台的字符

[英]C++: Cannot delete characters already printed to console before outputting

I'm doing a lab for my C++ class in school. 我正在为学校的C ++课做一个实验室。 The program reads binary numbers in from a file, then calculates their decimal equivalent and prints them to the console, but it has to read the entire file in character by character. 该程序从文件中读取二进制数字,然后计算它们的十进制等效值并将其打印到控制台,但是它必须逐字符读取整个文件。 The program is supposed to ignore leading spaces and 0s. 该程序应该忽略前导空格和0。 When the program reaches an invalid digit, I want it to backspace to the beginning of the line, then indent and print out the message "Bad digit on input" like so: 当程序到达无效数字时,我希望它退格到行首,然后缩进并打印出消息“输入数字错误”,如下所示:

在此处输入图片说明

All of the "Bad digit input" should be left aligned with the rest of the numbers in the Binary Number column, but as you can see... 所有“错误数字输入”应与“ 二进制数”列中的其余数字保持对齐,但是如您所见...

控制台输出

Here is the entire program: 这是整个程序:

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;
}

Here is what I have in my input file (I substituted _ for " " so they would be visible): 这是我输入文件中的内容(我用_代替了“”,这样它们就可见了):

输入文件

From what I know, looping cout << '\\b'; 据我所知,循环cout << '\\b'; a certain number of times should backspace that number of characters, but for some reason mine is not. 一定次数应该退格该数目的字符,但是由于某种原因我的不是。

Edit: Added cout << '\\r'; 编辑:添加cout << '\\r'; . Here is my new output. 这是我的新输出。 Now I have random blank lines and the input with leading zeros still does not print out correctly. 现在我有随机的空白行,并且前导零的输入仍不能正确打印。

在此处输入图片说明

Edit: Fixed the leading zero problem. 编辑:修复了前置零问题。 Adding cout << '\\r'; 添加cout << '\\r'; just moved "Bad digit on input" to the next line, leaving the already printed characters on the line above. 刚刚将“输入的错误数字”移到了下一行,将已经打印的字符留在了上一行。

在此处输入图片说明

You can try to use a combination of backspace \\b and space like so: 您可以尝试使用退格\\b和空格的组合,如下所示:

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

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

The expression goes back a number of characters, overwrites with space what is already written, and again goes back to the new output position. 该表达式返回多个字符,用空格覆盖已写入的内容,然后再次返回新的输出位置。

To go back to the beginning of the line, use \\r . 要返回到行的开头,请使用\\r You will have to overwrite everything which is already written to the current line. 您将不得不覆盖已经写入当前行的所有内容。

The problem has something to do with MacOS. 问题与MacOS有关。 I tried backspacing fixed input and nothing was deleted even when I just output '\\b' . 我尝试退回固定输入的空间,即使我只是输出'\\b'也没有删除任何内容。 I ran my program in Visual Studio and it outputs as expected, so I have concluded that either Eclipse on MacOS or MacOS itself does not support '\\b' . 我在Visual Studio中运行了程序,它的输出符合预期,因此得出的结论是,MacOS上的Eclipse或MacOS本身都不支持'\\b'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM