【发布时间】: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 << '\b'; 一定次数应该退格该数量的字符,但由于某种原因我的不是。
编辑:添加了cout << '\r';。这是我的新输出。现在我有随机的空白行,并且带有前导零的输入仍然无法正确打印出来。
编辑:修复了前导零问题。添加cout << '\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())之前,我没有通读整个帖子。跨度>