while loop in c++ help needed

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 1
Reputation: ziddi5 is an unknown quantity at this point 
Solved Threads: 0
ziddi5 ziddi5 is offline Offline
Newbie Poster

while loop in c++ help needed

 
0
  #1
Mar 4th, 2004
Please help me get the minimum in the following problem. i get everything else but the min. take a look at it.

//**************************************************
// Description : This program asks a user for five real numbers, then outputs
// the number of data entries entered, the total of the numbers
// entered, the mean average of the numbers entered, the minimum
// and maximum value entered. it will also format the output to
// one decimal place.
//**************************************************

#include <iostream>
#include <stdlib.h>

using namespace std;
int main(void)
{
double num, sum = 0, avg, max, min;
int count = 0; // loop counter variable
while (count < 5)
{
cout <<"Enter number " << count ++ << ":";
cin >> num;
sum = sum + num;
if (count == 0)
{
max = num;
min = num;
}
if (num > max)
{
max = num;
}
if (num < min)
{
min = num;
}
}
cout <<"The number of values entered is " << count << endl;
cout <<"The sum of values entered is " << sum << endl;
avg = sum / count;
cout <<"The average of values entered is " << avg << endl;
cout <<"The maximum value entered is " << max << endl;
cout <<"The minimum of value entered is " << min << endl;

system("PAUSE");
return 0;
}
Last edited by cscgal; Mar 7th, 2004 at 12:58 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 1,135
Reputation: samaru is just really nice samaru is just really nice samaru is just really nice samaru is just really nice 
Solved Threads: 5
Team Colleague
samaru's Avatar
samaru samaru is offline Offline
a.k.a inscissor

Re: while loop in c++ help needed

 
0
  #2
Mar 6th, 2004
In your while loop, you're telling it to loop five times and keeping track with variable count. Once you first enter the while loop, you're immediately incrementing it with the stament count++. Even if you're using this with a cout as output, it still increments it. What does this mean? It means that once it enter, it equals 1, and your statement of

  1. if (count == 0) {
  2. max = num;
  3. min = num;
  4. }

will never run. All you have to do is take the "++" from the "count" variable. You then increment this variable before another loop begins. Like this:

  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. int main(void) {
  5. double num, sum = 0, avg=0, max=0, min=0;
  6. int count = 0; // loop counter variable
  7. while (count < 5) {
  8. cout <<"Enter number " << count << ":";
  9. cin >> num;
  10. sum = sum + num;
  11. if (count == 0) {
  12. max = num;
  13. min = num;
  14. }
  15. if (num > max) {
  16. max = num;
  17. }
  18. if (num < min) {
  19. min = num;
  20. }
  21. count++;
  22. }
  23.  
  24. cout <<"The number of values entered is " << count << endl;
  25. cout <<"The sum of values entered is " << sum << endl;
  26. avg = sum / count;
  27. cout <<"The average of values entered is " << avg << endl;
  28. cout <<"The maximum value entered is " << max << endl;
  29. cout <<"The minimum of value entered is " << min << endl;
  30. return 0;
  31. }

Also, don't forget to initialize your variables always. In this case, you'd want to set them to zero, as I did.
Check out my blog at http://www.shinylight.com for more stuff about web dev.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC