Reputation: 57
I'm have some trouble understanding this while loop, the results of it is "TT", however for me what make sense if it printed out "TP". Can anyone help me out? I'll be showing why I think it should print "TP", and need an explaination why it prints "TT" (which is correct).
#include <iostream>
using namespace std;
char txt[] = "ETERNITTPLATENE-OG-TAKPAPPEN-BLE-KASTET";
void funk(char* t1, char* t2, int n)
{
while (t1 < t2) { ++t1; t2 -= n; } cout << *t1 << *t2;
}
int main() {
funk(txt, txt + 27, 3); cout << '\n';
return 0;
}
So first time it it runs func(E, N, 3) so we get
E < N
which is true, therefore it makes E to T and N to P (as 27-3 is 24, and in that array P
is in the 24th).
So then we get T < P
, which is false therefore it should stop the while loop and display TP
, which it doesn't. What am I thinking wrong?
Upvotes: 0
Views: 133
Reputation: 941
Run this to understand what is happening:
#include <iostream>
using namespace std;
char txt[] = "ETERNITTPLATENE-OG-TAKPAPPEN-BLE-KASTET";
int length = strlen(txt);//
void show_position(char* t1, char* t2)//
{
for (int i = 0; i < length; i++)
{
if (t1 - txt == i)
{
if (t2 - txt == i)
{cout << 'X';}
cout << '1';
}
else if (t2 - txt == i)
{cout << '2';}
else
{cout << '-';}
}
cout << endl;
}
void funk(char* t1, char* t2, int n)
{
show_position(t1, t2);//
while (t1 < t2)
{
++t1;
t2 -= n;
show_position(t1, t2);//
}
cout << *t1 << *t2 << endl;
}
int main() {
cout << txt << endl;//
funk(txt, txt + 27, 3);
return 0;
}
The lines followed by a // and the function declaration are there for drawing only, the rest is your almost untouched code.
A few nickpicks/suggestions (which i changed in your code):
1) use cout << endl;
instead of cout << "\n";
. endl is standard, and thus it will work properly in any platform (windows, linux, mac - which is linux but uses a different newline). Many apps support \n as default even on platform which don't officially use them (for example notepad++ supports the linux newline standard on windows), but that's not a good reason to not use endl.
2) If you have a function which purpose is printing something which clearly needs a newline, why you put the newline after the call instead of putting it directly inside your printing function?
Upvotes: 0
Reputation: 2855
When you are doing t1 < t2
you are not comparing (*t1) < (*t2)
which will mean comparing 'T' < 'P'
.
Instead, it is comparing the values of memory address in which character 'T'
and 'P'
are stored. And in this case, 'T'
is stored before 'P'
.
I have included how the pointers would move throughout the loop. This should clarify the whole iteration of while
loop.
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t1 t2
E T E R N I T T P L A T E N E - O G - T A K P A P P E N
0 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
^ ^
t2 t1
Upvotes: 8