November 4th, 2013, 01:10 AM
Answer required: 400..300
but i get some garbages.
November 4th, 2013, 01:34 AM
What do your warning messages say?
Because of the format string, printf is expecting six arguments, but you are only giving it five. It is bound and determined to print out six numbers, so it goes to the next location where a number should be and grabs whatever garbage is sitting there.
You lied to the compiler. It got its revenge.
Originally Posted by Henry Spencer
November 4th, 2013, 03:56 AM
I jus cpoied the problem.
let the %d should be five.
Revenge, that is a nice one.
November 4th, 2013, 10:18 AM
"what then?" what? The program is a bad one to start with, since it depends on undefined behavior that will be different depending on the compiler and platform you compile and run it on. You need to realize that that is not the way to write code and to avoid it.
When you use printf and scanf, you must always verify that the number of arguments matches the number of flags in the format string. And when you get something like what you got, that should be the first thing that you check.
I've gotten warnings from my compiler when that happened, so you should have gotten warnings too. Never ignore warnings. Always turn warnings on and up! And never ignore warnings.
November 4th, 2013, 11:03 PM
Now onwards I won't repeat this.
November 5th, 2013, 11:28 AM
That is not actually what I wanted you to learn.
In the signature of one of the forum members, it says something like this (quoting from memory):
You see, you will repeat that same mistake again. Several times throughout your career, in fact. We all make that same mistake on occasion, even the most experienced programmers. And lots of other mistakes as well.
But what distinguishes us experienced programmers from beginners is that we immediately work to identify and to correct those mistakes. Part of our ability to do that is the practical experience of having made those mistakes before. We learn to identify the symptoms. If we get a particular error or warning message, then we already have an idea of what we had done wrong this time. In fact, in the past I have informed a beginner that the reason why we could spot his error so quickly was because we had made the same mistake before so we knew what to look for. It may appear that we can read your mind, but in reality we're just drawing on our own personal experience.
What I want you to learn from that mistake is how to identify it, what's happening when you make that mistake, and how to correct it. So that the next time you encounter that same mistake, you will be able to correct it immediately instead of having to post it to a forum and wait for a few days before you can be given a solution. The time savings is very subtantial, especially considering how much time we need to devote to developing software.
Ultimately, even though the ideal goal is to learn to avoid mistakes, the most valuable lesson is to develop the ability to immediately correct and recover from mistakes.