![]() |
|
#1
|
|||
|
|||
Need help creating a counting loopI need a loop that will increase a variable start_pop by 10% each time and stop when it is larger than end_pop. While I do this I need to increase another variable year by 1 each time the loop runs.
I got this but I doesn't do what I want it to do, hopefully someone can help me. Thanks. So if start_pop is 10 and end_pop is 11 I need to return 1 for year. CPP / C++ / C Code:
Last edited by admin : 04-Nov-2009 at 03:14.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|||
|
#2
|
|||
|
|||
Re: Need help creating a counting loopQuote:
I have a couple of observations and a couple of suggestions: Observation number 1: I made a program with that loop that does exactly what I want it to do. See Footnote. However, in general (and this is, in my opinion, more important)... Observation number 2: Because of the ever-present possibility of roundoff error in in the representation of non-integer values of floating point variables and because of the ever-present possibility of roundoff error in calculations involving floating point variables, making a loop that depends comparisons of the values floating point variables may not do what you want it to do. Suggestion number 1: Put a print statement inside the loop so that each time through the loop, it prints out values of start_pop, 0.1*start_pop, and year. Maybe use different values for start_pop and end_pop so that you might learn more about the behavior of your loop. If you see something that you don't understand then: Suggestion number 2: Show us the code. The entire code so that we can see things like your variable declarations, etc. Show us the output from your program (with the extra print stuff that I recommended). Tell us exactly what you don't understand about the difference between what you expected to see and what the program actually presented. Regards, Dave Footnote: In addition to posting your complete program, you might tell us what compiler and what version of compiler you are using. Sometimes it makes a difference to people who would like to help. Last edited by davekw7x : 03-Nov-2009 at 09:44.
|
|
#3
|
|||
|
|||
Re: Need help creating a counting loopI am using the gcc compiler, I don't know the version but I'm guessing that its the latest.
CPP / C++ / C Code:
When returning values I get some random number like 12737924593 for years and 0.00 for ending population. And the printfs in the loop don't show up, and I'm completely lost. ![]() Last edited by admin : 04-Nov-2009 at 03:15.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#4
|
|||
|
|||
Re: Need help creating a counting loopFirst, you should probably print the user input after you read it so that you know you read it correctly. Many times, scanf() can be a major pain.
Secondly, you are not calling your function correctly: CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Need help creating a counting loopQuote:
Code:
Quote:
Code:
Now as for debugging: Pay attention to all compiler messages. For simple programs, I don't think there is any excuse for not letting the compiler help you as much as it can. Sometimes warnings are benign, but for beginners, I think they should take all warnings to heart and I personally would not accept a program that was not clean. With the warning flags turned on, you will see something like the following: Code:
CPP / C++ / C Code:
Change it to something that calls the function and uses the return value: CPP / C++ / C Code:
and try again. This time pay attention to all warnings and try to fix them. If you don't understand what happens, then 1. Post the latest code that you are using. 2. If there were any compiler messages, paste them into the post. 3. If the program runs but gives unexpected results, then show us the output and tell us what you don't understand. Regards, Dave Footnote: Why not make the debugging messages a little more verbose so that you (and we) can know exactly where the output came from. For example CPP / C++ / C Code:
|
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Error compiling Apache 2.2.10 | tiagofgarcia | Apache Web Server Forum | 2 | 10-Nov-2009 14:31 |
| Classic recursion game of 8 Queens errors | BKizzle77 | Java Forum | 11 | 11-Aug-2008 01:35 |
| Text-Based Roulette Game | mfm1983 | C++ Forum | 5 | 29-Nov-2006 13:20 |
| How to prevent caption bar from blocking message loop? | ckorda | MS Visual C++ / MFC Forum | 0 | 12-Apr-2006 15:22 |
| Counting a certain interval through a loop | Darth Predator | C++ Forum | 7 | 28-Sep-2005 01:34 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The