Menu

strange bug in dev-c++

Merak
2008-02-27
2012-09-26
  • Merak

    Merak - 2008-02-27

    I don't know where to post this, so I post it here and hope it eventually goes where it belongs.

    I did a little test to see the behaviour of C++ (I'm used to delphi) but...

    include <iostream>

    //#include <stdio>

    using namespace std;

    int main (int argc, char *argv[])
    {
    int i;
    char A[100];

    cout << "Hello World!" << endl;
    cin >> A;
    cout << A;
    for ( i = strlen(A), i = i - 1 ; ( i > -1 ) ){
    cout << A[i] << endl;
    }
    cout << endl << "Press ENTER to continue..." << endl;
    cin >>A;
    return 0;
    }

    One would expect this to output the string input backwards, but it doesn't output the last (first) character... "tester" give "retse" for example. Changing "i = i - 1" to "i--" makes the code work, so it's no real problem, but still a strange bug.

     
    • BiT

      BiT - 2008-02-27

      not so much a bug in Dev as it is a pebkac

      include <iostream>

      using namespace std;

      int main (int argc, char *argv[])
      {
      int i;
      char A[100];

      cout << "Hello World!" << endl;
      cin >> A;
      cout << A;
      for ( i = strlen(A); ( i > -1 );i-- ){
      cout << A[i] << endl;
      }
      cout << endl << "Press ENTER to continue..." << endl;
      cin >>A;
      return 0;
      }

      /**Produces*/

      Hello World! // <- Your Hello World
      tester // <- input text
      tester //<- output of var A
      r // <- Your Loop
      e
      t
      s
      e
      t

      Press ENTER to continue... // <- The End

       
    • cpns

      cpns - 2008-02-28

      Firstly Dev-C++ is not the compiler, that is GCC. Apply Occam's razor: is it likely that a compiler used by tens of thousands of developers for millions of lines of code (including Linux) has such a fundamental error, or is it in fact more likely that your code is just wrong!?

      You have a mal-formed for loop, the syntax is:

      for( <initial expression>; <loop condition>; <iteration expression> ) <loop body>

      The <initial expression> is executed once before the loop. The <loop body> executes while <loop condition> is true, and the <iteration expression> is evaluated at the end of each loop iteration. It is equivalent to:

      <initial expression>;
      while( <loop condition> )
      {
      <loop body>
      <iteration expression>
      }

      In your loop you had:

      <initial expression> = i = strlen(A), i = i - 1
      <loop condition> = i-- ;
      <iteration expresion> = (null statement)

      So teh loop body is executed starting from strlen(A) - 1, which happens to be correct since arrays are indexed starting from zero and continues while i-- evaluates to non-zero.

      BiT's loop is wrong too, it will output the NUL terminator as well as the string data.

      I suggest:

      for( i = strlen(A) - 1; i >= 0; i-- )

      Clifford

       
    • Nobody/Anonymous

      For future reference, Dev-C++ is just an IDE, a fancy editor. The compiler
      is, as Clifford indicated, the GNU compiler called GCC. It has a Unix
      ancestry, but it runs on Windows through the magic of a tool called MinGW.
      The MinGW/GCC toolchain is a completely seperate development from Dev.

      This may sound like pointless trivia, but you will find that when you want to
      google about to find out if there are libraries to do things you want - knowing
      the architecture really helps.

      Wayne

      p.s. I have been programming since 1975, and I would never be so sure of the
      correctness of any code I write that I would assume that it is a bug in the
      compiler rather than the unique unlimited stupidity of - well - me. ;)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.