![]() |
| | #1 |
| Registered User Join Date: Jun 2009
Posts: 4
| Help Help Help!!! Combining While Loops and If Statements Write a program that reads a series of numbers (doubles) from the user, then prints the mean and the range. Notes: • You do not know ahead of time how many numbers will be in the list. • When you want to stop entering numbers, enter control‐Z. • The range is the difference between the lowest and the highest number. • The numbers will be in the range of 0.0 to 100.0. Ignore any numbers outside of this range. this is my prompt so far. The average is only the last number not all the cins combine between 0 and 100 Code: #include <iostream>
#include <string>
using namespace std;
int main ()
{
double n, sum = 0, count = 0;
while(cin >> n)
{cin >> n;
if ((n >= 0.0) && (n <= 100.0))
{
++count;
sum += n;
}
else if (n == '^Z') break;
else
{
cout << "out of range: ignored.\n";
}
}
cout << "The average is " << sum / count << endl;
}
|
| Woot is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| You've got too many read statements. (Edit: That is to say, you've got a read statement in your while condition, plus one at the top of the loop.) Last edited by tabstop; 06-30-2009 at 08:23 PM. |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Jun 2009
Posts: 4
| how do i fix this then? |
| Woot is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Remove the extra read statements? |
| tabstop is offline | |
| | #5 |
| Registered User Join Date: Jun 2009
Posts: 4
| which ones are the extra read statment |
| Woot is offline | |
| | #6 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| |
| tabstop is offline | |
| | #8 |
| Registered User Join Date: Jul 2009
Posts: 40
| The question is do you know which one is in the expression and which one is not? Which one are you using to terminate the loop and which one is not used to terminate the loop? |
| WatchTower is offline | |
| | #9 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,359
| This is wrong: Code: else if (n == '^Z') break;
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #10 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,787
| FYI, from the forum guidelines: Quote:
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #11 |
| Registered User Join Date: Jun 2009
Posts: 4
| can you delete a board post? |
| Woot is offline | |
| | #12 |
| Super Moderator Join Date: Sep 2001
Posts: 4,680
| We generally don't delete posts because they can be useful for future readers. We'll usually delete inappropriate comments, though. |
| sean is offline | |
![]() |
| Tags |
| c++, problem |
| Thread Tools | |
| Display Modes | |
|