Same program: two different results

Hello,

I need your opinion of what is going wrong.
From home I wrote a program using Bloodsheed and got the wanted result. The purpose of the programm is to display lines from a source file to output a text with a certain width.
Then I went to uni to submit my program using TextPad and Borland: the output is different: spaces between words and some end of line characters are ignored. I do not understand what is going on. I have spent the all day on the case unsuccessfully. Do compiler use differently the operator >> to read string? Have you got a suggestion about the problem?

At home the successful output is:

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.

At uni:

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.

function which is going wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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();
    }
  }
}
Last edited on
IIRC you want to show a file but changing the spaces with underscores.
getline( file, line ); That will read till a '\n'
Thanks for your answer.
Unfortunately the instructions are that I can not analyse the content of the file line by line. Instead the file must be read using char or word string, adjusts big lines into smaller ones with a certain length. Tricky.
;-)
¿Can you load the entire file to a string before processing?
instead of the putback (that i think is causing undefined behaviour), you could peek at the next character to be read.

Your code is quite obfuscated
1
2
//if(static_cast<int>(w.length())>0)
if( not w.empty() )
No I am not allowed to load the entire file to a string to write this program.
Well spotted for the long winded

if(static_cast<int>(w.length())>0)

It has been changed as suggested.
Last edited on
Hi I am back. I shared my problem with my teacher via an email before Christmas. The main problem is due to the compilers: The compiler used with Bloodshed has an extraction operator which stops before the space or end of line character when reading data as a string whereas the compiler Borland discards them in the same situation. Because I had to use Borland I read all the input one character after another and managed to submit a successful program on time before eating some Christmas turkey.
Topic archived. No new replies allowed.