Syed Hadi:
If Rabbit had not replied, I would have deleted this as a homework question.
As your postings have progressed, the entire thread continues to give me the impression that this is a homework question and as such we should stop here until you can provide an example that actually works without basic logical errors or jumbled up half code.
Your initial code had a fundamental logic error.
The second code doesn't handle that logic error.
The third code does little if anything as Rabbit has pointed out.
Therefore, I respectfully ask that you take a few deep breaths, regroup, and then take a very hard close look at your project.
1) Write out in a single paragraph the major goals that you want the program to accomplish. This should be in "plain English." Not that you have to use English, just that you should not use any pseudo code, jargon, or other short cuts.
2) In a single paragraph, for each of the major goals, detail the steps. These also should be in "plain English."
3) You need to do a logic chart for your program. I prefer the NS style (
nassi shneiderman diagram Google Search ) however any programing flow-charting method will work. This should flow naturally from your work in (1) and (2)
4) Using the flowchart write the major blocks of code and then fill in with your support code as needed.
5) Compile and fix compiler-time errors.
6) Execute the program
7) Then come back here with any further issues.