943,635 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 13133
  • C++ RSS
Mar 4th, 2004
0

while loop in c++ help needed

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ziddi5 is offline Offline
1 posts
since Mar 2004
Mar 6th, 2004
0

Re: while loop in c++ help needed

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

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Cannot get switch to execute
Next Thread in C++ Forum Timeline: newbies guide to c++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC