C++ 相同的程序:两个不同的结果。也许是由于操作员>>?
C++ Same program: two different results. Maybe due to operator >>?
我需要你的意见,什么地方出了问题。
我在家里用Bloodsheed写了一个程序,得到了想要的结果。该程序的目的是显示源文件中的行,以输出具有一定宽度的文本。源文件不能逐行分析。相反,它应该使用字符和字符串来读取。
然后我去uni使用TextPad和Borland提交我的程序:输出是不同的:单词之间的空格和一些行尾字符被忽略。我不明白这是怎么回事。我花了一整天研究这个案子,但没有成功。编译器使用不同的操作符>>读取字符串?看起来在第一种情况下,它停在空格或行结束符之前,在第二种情况下,它放弃它们。你对这个问题有什么建议吗?
在国内成功输出为:
Max line length: 40
___Inglis_(1994)_describes_it_thus:
"For_output_of___floating-point_numbers,
the_format_strings_used_by_printf_may
include_%f_or_%e_(or_%g,_which_we_will
ignore).__These_have_much_in_common_with
%i:
____"A_minus_sign_indicates_left
justification,_a_plus_sign_indicates
that_the_converted_value_will_start_with
a_plus_sign_if_it_is_positive,_and_a
minimum_field_width_and/or_a_precision
may_be_specified.
在大学:Max line length: 40
___Inglis(1994)describesitthus:
"Foroutputof__floating-pointnumbers,the
formatstringsusedbyprintfmayinclude%for
%e(or%g,whichwewillignore)._Thesehave
muchincommonwith%i:
____"Aminussignindicatesleft
justification,aplussignindicatesthatthe
convertedvaluewillstartwithaplussignifit
ispositive,andaminimumfieldwidthand/ora
precisionmaybespecified.
出错的函数:
void Text::display(ofstream & out)
{ ifstream from(infileName.c_str());
if (from.fail())
{ cerr<<infileName<<" not openn";
exit(1);
}
out<<"Max line length: "<<lineLength<<endl<<endl;
string s, w; //s stands for space, w for word
char l; //l stands for letter
int c=0; //c syands for count
while(true)
{ if(static_cast<int>(w.length())>0)
{ if(lineLength<w.length())
{ cerr <<"The line length "<<lineLength
<<" is not long enough.n"<<"The longuest word is "
<<w<<" and has "<<w.length()
<<" letters.n";
exit(1);
}
c+=w.length();
out<<w;
w.erase();
}
from.get(l);
if (from.fail())
{ out<<endl;
break;
}
if (l=='n')
{ out<<l;
s.erase();
c=0;
continue;
}
while (l==' ')
{ s.push_back('_');
c++;
from.get(l);
}
if (l=='n')
{ out<<l;
s.erase();
c=0;
continue;
}
from.putback(l);
from>>w;
c+=w.length();
if (lineLength<c)
{ out<<endl;
s.erase();
c=0;
}
else if(w.length()>0)
{ out<<s<<w;
w.erase();
s.erase();
}
}
}
这是不同换行符表示的症状。
在"home",你的换行符是LF ('n'
或0x0A
)。
在"uni"处,你的换行符是CR+LF ('rn'
或0x0D0A
)。
你的代码只允许LF换行符
作为题外话…
string s, w; //s stands for space, w for word
char l; //l stands for letter
int c=0; //c syands for count
c++允许大于一个字符的标识符。下面的代码更具表现力,不需要注释,并且使维护代码更容易。
std::string space, word;
char letter;
int count = 0;
相关文章:
- EASTL矢量<向量<int>>连续的
- C++和带有国家符号的文件路径(也许用 UTF8 编码)
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- C 字符串比较“祝您好运”&gt;“再见”
- (也许是NP-Hard)求一个集合的子集总数,使得每个子集在与其所有元素相乘时的值都大于X
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 多线程 - 降低效率,也许是由“ false共享”所产生的
- 在密钥扩展和再生成方面,AES128 和 AES256 与 AES128 和 AES256 有什么区别?(也许还有 AE
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- C 操作员&gt;&gt;与突变器过载
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 是否需要使用 - &gt;运算符在C 中调用成员函数时
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- 向量问题,也许有点手
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- 下面的代码无法编译。也许是因为 std=c++2a 的 GCC 仍然没有完全跟上最新的草案
- 错误c++visual studio c2227左侧'->;Init';必须指向类/结构/联合/泛型类型