我正在学校为我的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;
}
以下是我在输入文件中的内容(我将#替换为&#34;&#34;因此它们将可见):
据我所知,循环cout << '\b';
一定次数应退回该字符数,但出于某种原因我的不是。
修改:已添加cout << '\r';
。这是我的新输出。现在我有随机的空行,带前导零的输入仍无法正确打印出来。
编辑:修正了前导零问题。添加cout << '\r';
刚刚移动&#34;输入错误数字&#34;到下一行,将已打印的字符留在上面一行。
答案 0 :(得分: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
。您必须覆盖已写入当前行的所有内容。
答案 1 :(得分:0)
问题与MacOS有关。我尝试退回固定输入,即使我输出'\b'
也没有删除任何内容。我在Visual Studio中运行我的程序并按预期输出,因此我得出结论,MacOS或MacOS上的Eclipse都不支持'\b'
。