943,878 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3502
  • C++ RSS
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
Oct 10th, 2008
0

Re: Help Would Be Appreciated

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 10th, 2008
0

Re: Help Would Be Appreciated

How do I ignore values outside of 0.0 and 100.0? Thanks for the help Lerner.
Reputation Points: 10
Solved Threads: 0
Light Poster
joed13k1941 is offline Offline
47 posts
since Oct 2008
Oct 10th, 2008
0

Re: Help Would Be Appreciated

One way is to use a flag, again. They can be very helpful in controlling loops!
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 10th, 2008
0

Re: Help Would Be Appreciated

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
joed13k1941 is offline Offline
47 posts
since Oct 2008
Oct 10th, 2008
0

Re: Help Would Be Appreciated

Click to Expand / Collapse  Quote originally posted by mjf5012 ...
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:

C++ Syntax (Toggle Plain Text)
  1. if (count == 0)
  2. {
  3. maxval = n;
  4. minval = n;
  5. }
not

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

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 10th, 2008
0

Re: Help Would Be Appreciated

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
joed13k1941 is offline Offline
47 posts
since Oct 2008
Oct 10th, 2008
0

Re: Help Would Be Appreciated

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 11th, 2008
0

Re: Help Would Be Appreciated

It does ignore values entered outside of 0 to 100. Try executing it. Thanks Lerner.
Reputation Points: 10
Solved Threads: 0
Light Poster
joed13k1941 is offline Offline
47 posts
since Oct 2008
Oct 11th, 2008
0

Re: Help Would Be Appreciated

continue;
Do you really need this?
Reputation Points: 31
Solved Threads: 2
Light Poster
dexter1984 is offline Offline
49 posts
since Jan 2008
Jul 2nd, 2009
1

Re: Help Would Be Appreciated

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
Reputation Points: 22
Solved Threads: 0
Newbie Poster
collegetextbook is offline Offline
4 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: How do i read a huge file of data from a text file in c++?
Next Thread in C++ Forum Timeline: Saving STL Maps to file as Binary(Possible?)





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


Follow us on Twitter


© 2011 DaniWeb® LLC