Learning C++ while in lockdown. Problem with pre & postfix operators

50 views
Skip to first unread message

Kev P

unread,
Mar 28, 2020, 9:53:29 AM3/28/20
to
I'm using teach yourself C++ in 24hours.
In chapter 4 I try pre & post fix operators, but get different results to book, but my own little test prog seems to work?
I'm using: Microsoft Visual Studio Community 2019 (2)

#include <iostream>
int main()
{
int myAge = 39; // initialize two integers
int yourAge = 39;
std::cout << myAge << "\n";
std::cout << yourAge << "\n\n";
myAge++; // postfix increment
++yourAge; // prefix increment
std::cout << myAge << "\n";
std::cout << yourAge << "\n\n";
std::cout << myAge++ << "\n";
std::cout << ++yourAge << "\n\n";
std::cout << myAge << "\n";
std::cout << yourAge << "\n\n";
return 0;
}
Book says this should print as follows (text removed, just values shown):
39 39
40 40
40 41
41 41

But I get:
39 39
40 39
40 40
41 40

However, my own cut down version below gives correct(?) answer to book, weird!

#include <iostream>
int main()
{
int a = 39;
int b = 39;
std::cout << a << " " << b << "\n";
a++;
++b;
std::cout << a << " ";
std::cout << b << "\n\n";
std::cout << a++ << " ";
std::cout << ++b << "\n\n";
std::cout << a << " ";
std::cout << b << "\n\n";
return 0;
}
39 39
40 40
40 41
41 41

I can't see any difference other than the cout formatting, what am I missing?

Barry Schwarz

unread,
Mar 28, 2020, 10:32:37 AM3/28/20
to
On Sat, 28 Mar 2020 06:53:26 -0700 (PDT), Kev P <nivpa...@gmail.com>
wrote:

>I'm using teach yourself C++ in 24hours.
>In chapter 4 I try pre & post fix operators, but get different results to book, but my own little test prog seems to work?
>I'm using: Microsoft Visual Studio Community 2019 (2)
>
> #include <iostream>
> int main()
> {
> int myAge = 39; // initialize two integers
> int yourAge = 39;
> std::cout << myAge << "\n";
> std::cout << yourAge << "\n\n";
> myAge++; // postfix increment
> ++yourAge; // prefix increment
> std::cout << myAge << "\n";
> std::cout << yourAge << "\n\n";
> std::cout << myAge++ << "\n";
> std::cout << ++yourAge << "\n\n";
> std::cout << myAge << "\n";
> std::cout << yourAge << "\n\n";
> return 0;
> }
>Book says this should print as follows (text removed, just values shown):

Since there is no text in any of your output statements, this is not
the code the book uses.

>39 39
>40 40
>40 41
>41 41
>
>But I get:
>39 39
>40 39
>40 40
>41 40

Show us the actual code you used from the book

>
>However, my own cut down version below gives correct(?) answer to book, weird!
>
>#include <iostream>
>int main()
>{
> int a = 39;
> int b = 39;
> std::cout << a << " " << b << "\n";
> a++;
> ++b;
> std::cout << a << " ";
> std::cout << b << "\n\n";
> std::cout << a++ << " ";
> std::cout << ++b << "\n\n";
> std::cout << a << " ";
> std::cout << b << "\n\n";
> return 0;
>}
>39 39
>40 40
>40 41
>41 41
>
>I can't see any difference other than the cout formatting, what am I missing?

When you show us the exact ode you used (don't retype it, use cut and
paste), we will be able to tell you.

--
Remove del for email

Kev P

unread,
Mar 28, 2020, 1:44:46 PM3/28/20
to
Well, it seems to be behaving itself now, can't replicate the fault!
And, yes, I did use the exact same source file.
I guess the compiler had a bad moment??

Andrew Falanga

unread,
Mar 8, 2021, 12:02:04 PM3/8/21
to
That's a tempting thing to think, but it's usually never the case. In 20 years of programming, I have found only 1 legitimate compiler bug. The problem is usually between the keyboard and the chair. :-)

Real Troll

unread,
Apr 26, 2021, 11:54:51 AM4/26/21
to
On 26/04/2021 16:02, Stefan Ram wrote:
> Kev P <nivpa...@gmail.com> writes:
>> std::cout << myAge << "\n";
>> std::cout << yourAge << "\n\n";
> ...
>> But I get:
>> 39 39
>> 40 39
>> 40 40
>> 41 40
> `<< "\n\n"` should insert an empty line.

or this can insert a new line:

<< std::endl;


> It is unlikely that a popular compiler has issues with both
> increment operators /and/ newlines. It is more likely that
> the text shown as output is not the output of the program.
>
where is actual program that we can see to see what is happening.

The difference people should be aware of are:

i++
--i

Apart from that I can't see anything else likely to affect the output.






Keith Thompson

unread,
Apr 26, 2021, 2:50:40 PM4/26/21
to
r...@zedat.fu-berlin.de (Stefan Ram) writes:
> Kev P <nivpa...@gmail.com> writes:
>>std::cout << myAge << "\n";
>>std::cout << yourAge << "\n\n";
> ...
>>But I get:
>>39 39
>>40 39
>>40 40
>>41 40
>
> `<< "\n\n"` should insert an empty line.
>
> It is unlikely that a popular compiler has issues with both
> increment operators /and/ newlines. It is more likely that
> the text shown as output is not the output of the program.

The original article included a complete self-contained program
and output that was inconsistent with the source code. The OP said
nearly a month ago that he was unable to reproduce the problem.

My guess is that he modified the program and the source file and
executable got out of synch, but we'll never know.

--
Keith Thompson (The_Other_Keith) Keith.S.T...@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

Geoff

unread,
Apr 26, 2021, 7:09:52 PM4/26/21
to
On Sat, 28 Mar 2020 06:53:26 -0700 (PDT), Kev P <nivpa...@gmail.com>
wrote:

Neither version produces output as described above.

The first version outputs:
39
39

40
40

40
41

41
41


The second version outputs:

39 39
40 40

40 41

41 41

The misconception appears to be about the formatting of the output,
not the values of the operations involved.
Reply all
Reply to author
Forward
0 new messages