| | |
Help Would Be Appreciated
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2005
Posts: 1,704
Reputation:
Solved Threads: 274
One way is to use a flag, again. They can be very helpful in controlling loops! Alternatively: should work, too.
C++ Syntax (Toggle Plain Text)
bool stillLooking = true; do { cin >> n; if(n <0 || n > 100) cout << "try again" << endl; else stillLooking = false; }while(stillLooking);
C++ Syntax (Toggle Plain Text)
cout << "enter a number between 0-100 inclusive: "; n = -9999; while(n < 0 || n > 100) { cin >> n; if(n < 0 || n > 100) cout << "enter a number between 0-100 inclusive: "; }
•
•
Join Date: Jan 2008
Posts: 3,831
Reputation:
Solved Threads: 501
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
if (count == 0) { maxval = n; minval = n; }
C++ Syntax (Toggle Plain Text)
if (count == 0) { maxval = n; minval = n; }
It makes it much easier to see what goes with what. So define your outer while loop:
C++ Syntax (Toggle Plain Text)
do { } 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.
•
•
Join Date: Oct 2008
Posts: 47
Reputation:
Solved Threads: 0
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)
#include <iostream> using namespace std; int main() { double n; double minval = 0; double maxval = 0; double count = 0; double sum = 0; double range; while(cin >> n) { if(n < 0 || n > 100) { cout << "out of range; value ignored" << endl; continue; } ++count; sum += n; if (count == 1) { maxval = n; minval = n; } if (n > maxval) { maxval = n; } if (n < minval) { minval = n; } range = maxval - minval; } cout << "The average is " << sum / count << endl; cout << "The range is " << range << endl; }
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 0
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>
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
![]() |
Similar Threads
- Where to begin? Any help is much appreciated! (IT Professionals' Lounge)
- Any help would be greatly appreciated :) (Java)
- Help appreciated on a display problem (Windows 95 / 98 / Me)
- Any help on my dead Satellite A20 would be greatly appreciated! (Troubleshooting Dead Machines)
- Suggestion appreciated. (Promotion and Marketing Plans)
- Help appreciated/required for project (Java)
- Help would be appreciated concerning task manager (Windows NT / 2000 / XP)
- Any help will be greatly appreciated... (Viruses, Spyware and other Nasties)
- NEW Site: Feedback Greatly Appreciated (Website Reviews)
- An Interesting Evolution of Problems, Suggestions Appreciated (Troubleshooting Dead Machines)
Other Threads in the C++ Forum
- Previous Thread: How do i read a huge file of data from a text file in c++?
- Next Thread: Saving STL Maps to file as Binary(Possible?)
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library linker list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






