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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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!?
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> = 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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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. ;)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
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
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. ;)