Hello kenmariot,
When you use the ".cpp" extension the compiler will compile the program differently than if you used the ".c" extension.
The header files "stdio.h" and "cstdio" are in a sense the same as "cstdio" does include "stdio.h" at the beginning, but goes further to make the C header file work better with a C++ program.
I may not have this 100% correct, but the basic idea is sound.
So if you use the file extension is ".cpp" it is better to use the "c?????" header files even if you see no difference, there is a difference. Be aware that not every C header file has a C++ counterpart and it is not a good idea to mix C++ and C header files in a C++ program, but sometimes you have to and that is OK.
getchar
is a function that should be written as
getchar()
. As the name implies it will get a single character from the input buffer. Not enough to deal with any possible buffer overflow. What you might consider using is
scanf_s
. I took a quick look at
https://stackoverflow.com/questions/21434735/difference-between-scanf-and-scanf-s
I am not sure if
scanf_s
is specific to Microsoft compilers as the post suggests. Or if it became part of a C++ standard after C++98 or 99. I did not go that far to read up on the whole post, but the idea I get is that
scanf_s
is a more secure form that should avoid any buffer overflow.
Your use of
getchar()
in your program is just creating a pause waiting for the user to press enter to continue. But without a prompt before its use the user does not know what to do.
In your for loop you are using
<=
. This may work, but as your output shows you are printing six numbers when I believe you only want five numbers. The
<=
is causing one extra loop that prints the extra number. AS I showed you in my code the "<" is what you need to use here. Even if you start "i" at one it prints five numbers not six.
On line 5 you define three variables. First I state again that you should avoid using single letter variable names. It does not matter to me and the program is small an easy to see all of it on one screen, but in the future, using a variable that is a noun and describes what it is or does will benefit you in the long run and help others to understand your program better. So, the sooner you get use to creating better variable names the better off you will be.
Also the variable "c" is defined, but never used. This is not a problem with the compile, it should produce a warning, and it will not stop the program from running, but it is wasted storage space since it is never used. In a larger program it could make a difference holding space for a variable that is never used and allowing something to use that space.
You have a program and know what to enter and how to enter the information, but someone else has no idea what to do when presented with a blank screen and no idea what to do.
A lot of the time it is best to prompt the user with what to enter and an example of how to do it. Some times it is better to prompt for each input so that there is no question about what to enter.
I have seen times where I have had to create a whole new program because I could not figure out what the problem was. And even copying and pasting the same code worked. I have also had programs that I have had to comment out a line of code and rewrite the same code below the comment for it to work and other times where I had to copy a variable and paste it into a line of code for it to work. I have never figured out what caused the problem. Sometimes you just have to try several things or just give up and create a new program.
Hope that helps,
Andy