Help Would Be Appreciated

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2005
Posts: 1,704
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help Would Be Appreciated

 
0
  #31
Oct 10th, 2008
If you insist on a stop gap measure that might not work everytime you can leave sum -= n right where it is, but, at least IMO, it's not the best way to do things.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 47
Reputation: joed13k1941 is an unknown quantity at this point 
Solved Threads: 0
joed13k1941 joed13k1941 is offline Offline
Light Poster

Re: Help Would Be Appreciated

 
0
  #32
Oct 10th, 2008
How do I ignore values outside of 0.0 and 100.0? Thanks for the help Lerner.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,704
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help Would Be Appreciated

 
0
  #33
Oct 10th, 2008
One way is to use a flag, again. They can be very helpful in controlling loops!
  1. bool stillLooking = true;
  2. do
  3. {
  4. cin >> n;
  5. if(n <0 || n > 100)
  6. cout << "try again" << endl;
  7. else
  8. stillLooking = false;
  9. }while(stillLooking);
Alternatively:
  1. cout << "enter a number between 0-100 inclusive: ";
  2. n = -9999;
  3. while(n < 0 || n > 100)
  4. {
  5. cin >> n;
  6. if(n < 0 || n > 100)
  7. cout << "enter a number between 0-100 inclusive: ";
  8. }
should work, too.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 47
Reputation: joed13k1941 is an unknown quantity at this point 
Solved Threads: 0
joed13k1941 joed13k1941 is offline Offline
Light Poster

Re: Help Would Be Appreciated

 
0
  #34
Oct 10th, 2008
I redefined sum in the last while loop as sum = sum - n (where n is the last value entered by the user). If i enter a number outside of 0-100 at the beginning of the inputs, it works fine, but if the last number entered is outside of 0-100, it does not work.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,831
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Help Would Be Appreciated

 
0
  #35
Oct 10th, 2008
Originally Posted by mjf5012 View Post
I redefined sum in the last while loop as sum = sum - n (where n is the last value entered by the user). If i enter a number outside of 0-100 at the beginning of the inputs, it works fine, but if the last number entered is outside of 0-100, it does not work.
Define "it works". One, you have brackets that don't need to be there. The compiler doesn't care, but it makes it hard for the human eye to follow. Delete the ones you don't need. Two, brackets should line up with the loop or if statement they are associated with. For example:

  1. if (count == 0)
  2. {
  3. maxval = n;
  4. minval = n;
  5. }
not

  1. if (count == 0)
  2. {
  3. maxval = n;
  4. minval = n;
  5. }

It makes it much easier to see what goes with what. So define your outer while loop:

  1. do
  2. {
  3.  
  4. }
  5. while (!cin.fail ());

Everything inside this loop should be indented so it's clear what's inside the loop and what is not. Within this loop, once you read in the user data, you need to have one or more if statements that tests the data that is entered. You'll have at least three criteria.

One, user enters a number from 0 to 100. Two, user enters a number, but one that is not from 0 to 100. Three, user enters something that isn't a number. You should, at the least, decide which category the input falls into. You should actually probably split that last category into two categories:

Category 3a: User enters Ctrl-Z.
Category 3b: User enters something that isn't a number and isn't Ctrl-Z

but that will require you to redesign your program considerably. Each category involves executing certain code and not executing other code. So decide which of the three (or four) categories the input belongs to, then have it execute the appropriate code based on that. You'll need one or more if statements to decide which category it belongs to.

Subtracting n from sum after the loop is over is a band-aid that may work, but a better solution is to control what code is executed rather than clean up after code that shouldn't have executed in the first place. Proper indentation of brackets/code within brackets and deleting unnecessary/misleading brackets is key to understanding the program's flow and debugging it.

Using cin.fail () to test for Ctrl-Z is OK if you don't care that other things which aren't Ctrl-Z will be caught by it too, but if you do, you'll need a different solution.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 47
Reputation: joed13k1941 is an unknown quantity at this point 
Solved Threads: 0
joed13k1941 joed13k1941 is offline Offline
Light Poster

Re: Help Would Be Appreciated

 
0
  #36
Oct 10th, 2008
I just decided to start over with all of your advice. The ctrl-z seems to have been "built-in." I am very happy to get it done. Thanks for all of your help.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double n;
  7. double minval = 0;
  8. double maxval = 0;
  9. double count = 0;
  10. double sum = 0;
  11. double range;
  12.  
  13. while(cin >> n)
  14. {
  15.  
  16. if(n < 0 || n > 100)
  17. {
  18. cout << "out of range; value ignored" << endl;
  19. continue;
  20. }
  21.  
  22.  
  23. ++count;
  24. sum += n;
  25.  
  26. if (count == 1)
  27. {
  28. maxval = n;
  29. minval = n;
  30. }
  31. if (n > maxval)
  32. {
  33. maxval = n;
  34. }
  35. if (n < minval)
  36. {
  37. minval = n;
  38. }
  39.  
  40. range = maxval - minval;
  41.  
  42. }
  43.  
  44. cout << "The average is " << sum / count << endl;
  45. cout << "The range is " << range << endl;
  46.  
  47.  
  48. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,704
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 274
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help Would Be Appreciated

 
0
  #37
Oct 10th, 2008
Except n isn't ignored if it's below zero or above 100. If everything outside the if statement inside the while loop is enclosed in an else statement, then n would be ignored if it is below zero or above 100.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 47
Reputation: joed13k1941 is an unknown quantity at this point 
Solved Threads: 0
joed13k1941 joed13k1941 is offline Offline
Light Poster

Re: Help Would Be Appreciated

 
0
  #38
Oct 11th, 2008
It does ignore values entered outside of 0 to 100. Try executing it. Thanks Lerner.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 49
Reputation: dexter1984 is an unknown quantity at this point 
Solved Threads: 2
dexter1984 dexter1984 is offline Offline
Light Poster

Re: Help Would Be Appreciated

 
0
  #39
Oct 11th, 2008
continue;
Do you really need this?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 3
Reputation: collegetextbook is an unknown quantity at this point 
Solved Threads: 0
collegetextbook collegetextbook is offline Offline
Newbie Poster

Re: Help Would Be Appreciated

 
1
  #40
Jul 2nd, 2009
have u read some reference textbooks? I believe there are many good reference textbooks for C++ study. For me, I use Weiss

Data Structures and Algorithm Analysis in C++, 3rd Edition

well organize and easy to follow.

If you search and buy from internet, you will find very good price for this textbooks. If you need where to buy it, email me at

<snipped>
Last edited by Ancient Dragon; Jul 3rd, 2009 at 12:52 am. Reason: snipped email
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC