Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;:输出前无法删除已打印到控制台的字符 我正在学校为我的C++课做一个实验。该程序从文件中读取二进制数,然后计算其十进制等效值并将其打印到控制台,但它必须逐个字符读取整个文件。程序应该忽略前导空格和0。当程序到达一个无效数字时,我希望它返回到行的开头,然后缩进并打印出消息“输入的数字不正确”,如下所示:_C++_Eclipse_Console - Fatal编程技术网

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

C++;:输出前无法删除已打印到控制台的字符 我正在学校为我的C++课做一个实验。该程序从文件中读取二进制数,然后计算其十进制等效值并将其打印到控制台,但它必须逐个字符读取整个文件。程序应该忽略前导空格和0。当程序到达一个无效数字时,我希望它返回到行的开头,然后缩进并打印出消息“输入的数字不正确”,如下所示:,c++,eclipse,console,C++,Eclipse,Console,所有“坏数字输入”应与二进制数列中的其余数字保持左对齐,但正如您所看到的 以下是整个计划: 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) << "B

所有“坏数字输入”应与二进制数列中的其余数字保持左对齐,但正如您所看到的

以下是整个计划:

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;
}
int main(无效)
{
//声明
河流充填;
charx=0;
int y=0,i=0,count=0,q=0,l=0;
//将标题打印到控制台

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

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

// Delete the last two characters
std::cout << "\b\b  \b\b" << std::flush;
//删除最后一个字符

std::cout这个问题与MacOS有关。我尝试了固定输入的退格,但即使在我刚刚输出
'\b'
时,也没有删除任何内容。我在Visual Studio中运行了我的程序,它按预期输出,因此我得出结论,MacOS上的Eclipse或MacOS本身都不支持
'\b'
的鱼主题:
(!infle.eof())
对您来说效果不太好。这里有更多信息:首先不写东西通常比之后清理控制台更容易。保留输出直到您知道要写它,然后再写它。
而(infle.get(x))
没有在课堂上讨论过?奇怪。我在你的代码中看到
while
infle.get(x)
。对于这个作业,我们不能保留输出。我们必须按读入的方式输出每一个数字。我本来就有这个,但是它不会读取文件的最后一行。我在(!infle.eof()时更改了
while(infle.get(x))
并且它去掉了多余的0。在我说我必须使用
while(!infle.eof())
之前,我没有通读整篇文章。它所做的只是将“输入时的坏数字”移到下一行。已经打印的字符仍然没有被删除。'\b'在macos上失败(10.14.6)