【问题标题】:C++ Same program: two different results. Maybe due to operator >>?C++ 相同的程序:两个不同的结果。也许是由于运营商>>?
【发布时间】:2012-01-22 09:46:54
【问题描述】:

我需要您对问题出在哪里的意见。

我在家中使用 Bloodsheed 编写了一个程序并得到了想要的结果。该程序的目的是显示源文件中的行以输出具有一定宽度的文本。无法逐行分析源文件。相反,它应该使用 char 和 string word 读取。

然后我去 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 open\n";
    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 == windows == true &amp;&amp; uni == linux == true

标签: c++ operator-keyword turbo-c++ textpad


【解决方案1】:

这是不同newline representations的症状。

在“家”中,您的换行符是 LF('\n'0x0A)。

在“uni”中,您的换行符是 CR+LF('\r\n'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++ 允许标识符长于单个字符。以下内容更具表现力,消除了对 cme​​ts 的需求,并且将使您的代码更容易维护。

std::string space, word;
char letter;
int count = 0;

【讨论】:

  • 所以我修改了标识符,使其更加明确。
  • 那只是旁白,它不会解决你的问题。您需要为 CR+LF 编码。
  • 看来这就像将换行检查从if (l=='\n') 更改为if (letter == '\n' || letter == '\r') 一样简单,但我还没有测试过你的代码。
  • 我已按照建议更改了 'if (l=='\n')'。警告消失了。我已经筋疲力尽了:我已经在这个问题上工作了半天。所以我需要好好睡一觉。明天我将尝试为 CR+LF 编码:这对我来说是完全未知的。我注意到在家里或在 uni 我的源文件使用 LF,而我的输出文件使用 CR+LF。谢谢你们所有的cmets。
  • 哪个警告消失了?
猜你喜欢
  • 1970-01-01
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多