Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

PartyOmNi

for loop

This topic is 5319 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I wanted to make a program that would calculate avg. temperature for 1 month. The user inputs and then it just calculates. Assume that 1 month is 30 days. so, I use VC++ 6, and this is what I did. #include <iostream> using namespace std; int input; int avg; int main() { for (int i = 30;i>0;i--) { cout<<"enter temperature (high): "<>input; avg = avg+input; i--; } avg = avg/30; cout<<"the average is "<

Share this post


Link to post
Share on other sites
Advertisement
This wouldn''t cause your loop to be cut down to one iteration, but the last line of your loop, i--, isnt necessary. the for loop automatically executes the instruction you give it, so your loop will decrement i twice each time through.

Share this post


Link to post
Share on other sites
a) Don''t decrement i twice.
b) Once fixed, you still have an off-by-one error in the number of iterations.
c) initialize avg to 0.
d) if your input operation fails, and you don''t clear the offending characters from the buffer, all subsequent attempts will fail too, silently. I suspect that may be what happens here.

Share this post


Link to post
Share on other sites
The for loop takes care of the i--
Having the i-- at the end of the for block will decrement i again. Also, assuming the code does compile and the errors in your post are just typos, you want the ''cin'' statement to be cin >> avg; This will store whatever the user types in into avg.

Share this post


Link to post
Share on other sites
so this is what I did...
#include <iostream>
using namespace std;

int avg;
int input;

int main ()
{
for (int i = 30;i<0;i--)
{
cout<<"please enter temperature: "< cin>>avg;
avg = avg+input;
}

avg = avg/30;
cout<<"the average is: "<
return 0;
}



and this is what I get as my output...

the average is: 0
Press any key to continue


Share this post


Link to post
Share on other sites

//I''ve made more modifications//
#include <iostream>
using namespace std;

int avg=0;
int input;

int main ()
{
for (int i = 30;i<0;i--)
{
cout<<"please enter temperature: "< cin>>input;
avg = avg+input;
}

avg = avg/30;
cout<<"the average is: "<
return 0;
}

Share this post


Link to post
Share on other sites
Since the loop starts at 30, the condition i<0 is immediately false and the loop is never executed.

Try (int i=0; i<3130; ++i) instead.

[edited by - Fruny on February 29, 2004 11:48:05 PM]

Share this post


Link to post
Share on other sites
you might be getting integer truncation when you divide try something like this


avg=(int)( (float)avg/30.0f) );


"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
Since the loop starts at 30, the condition i<0 is immediately false and the loop is never executed.

Try (int i=0; i<31; ++i) instead.


Actually, that will loop 31 times.

this...

for (int i=0; i<30; i++)...

will loop 30 times.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!