It does, in fact, produce output. But you do not see it because the OS realizes your terminal program has terminated and gets rid of the terminal window.
You can most easily fix this by simply running the program
from the terminal.
On Windows, navigate to the
.exe in Explorer, click on the file path line (the “Location Bar”, analogous to the URI line in your web browser), and type
cmd
and press
Enter. This will open a terminal window at that location.
Type
dir
and press
Enter to see the directory (
i.e. folder) contents. You will see your
.exe file in the listing.
Type the name of your program to make it run. For example, if your executable is named “
a.exe”, then you can type
a.exe
(or just
a
) and press
Enter to make it run.
When you are done with the terminal window, either click the big red X as usual or type
exit
and press
Enter.
On Linux it is much the same. Run the “
terminal” program from the drop down menu to get the terminal window.
If your program were in your
Documents ► programming ► program1 directory/folder, for example, you can change to that directory/folder by typing
cd Documents/programming/program1
and pressing
Enter.
To see the listing of the files in that folder, type
ls
and press
Enter. (That’s a lower-case L and S, short for “list”.)
To run the program you must type its name. For example, if your executable is named “
a.out” you must type
./a.out
and press
Enter to run it.
When you are done with the terminal window, either click the big X as usual or type
exit
and press
Enter.
Another option is to add a call to an input function right at the end of
main(). This is not an optimal solution. The stickied thread at the top of the
Beginner’s Forum is all about ways to do this (and how inadequate they are).
Additional Notes
I do not know what book gave you that code, but it is horrible, horrible code and you should not be using it to learn. I currently recommend spending time at
https://www.learncpp.com/.
Learning to program has a very steep learning curve, but many people successfully do it. It just takes a willingness to put up with a lot of frustration as you go. It eventually makes sense.
Hope this helps. :O)