| | |
Need help with a simple while loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
I have gotten started on my while loop but I'm kinda stuck I dont know where to go from here 
My assignment is to write a program to read in 6 numbers from the keyboard and then indicate whether any of them were larger than 500 or smaller than 10.
After that I need to add in a flag that will detect numbers larger than 500. Use a bool variable for this.
Then add in a flag that will detect numbers less than 10. Use a bool variable for this.
The feedback to the user should happen after (outside) the end of the loop, not during the input phase. The user is not interested in how many times an event occurred, only whether it happened or not. The feedback should have clear messages telling the user whether each event occurred or not; it should NOT just be the word 'true' or 'false' or 1 or 0.
And this is all I have gotten so far
.... Im just not cut out for computer science
Thanks guys
-UKmason

My assignment is to write a program to read in 6 numbers from the keyboard and then indicate whether any of them were larger than 500 or smaller than 10.
After that I need to add in a flag that will detect numbers larger than 500. Use a bool variable for this.
Then add in a flag that will detect numbers less than 10. Use a bool variable for this.
The feedback to the user should happen after (outside) the end of the loop, not during the input phase. The user is not interested in how many times an event occurred, only whether it happened or not. The feedback should have clear messages telling the user whether each event occurred or not; it should NOT just be the word 'true' or 'false' or 1 or 0.
And this is all I have gotten so far
.... Im just not cut out for computer science C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int num; int ctr = 0; cout << " **** A Flag question ****" << endl; cout << endl; cout << "Please Enter 6 numbers"; while ( ctr < 7 ) cin >> num; if (num > 500 || num < 10) return true; else return false; ctr ++; return 0;
Thanks guys
-UKmason
•
•
Join Date: Sep 2009
Posts: 306
Reputation:
Solved Threads: 33
1
#2 26 Days Ago
With no braces,{}, your while loop is only getting the statement directly underneath it. Start there. You don't want to return a true or false from main as your return type for main is an integer but also doing so will halt your program. Instead, after your if statement, set a variable (so initiate it as false at the top and then set it to be true once your condition occurs) that you have defined under
Then use your if statement to set this boolean variable if the value is out of range. Then make another boolean for values over 500 and yet another for those under 10.
Don't be so discouraged, programming like anything else takes time to work on and improve.
int ctr; (really you could declare it anywhere outside of the while loop and within main).Then use your if statement to set this boolean variable if the value is out of range. Then make another boolean for values over 500 and yet another for those under 10.
Don't be so discouraged, programming like anything else takes time to work on and improve.
Last edited by jonsca; 26 Days Ago at 3:37 pm.
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
0
#3 25 Days Ago
Ive tried to do what was recommended but havnt really gotten any where 
Whats it looking like now?
I still am confused on how to make the loop work while only using 1 variable 6 times... plz help

Whats it looking like now?
I still am confused on how to make the loop work while only using 1 variable 6 times... plz help

C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; bool loop ( int num, int ctr); int main () { int num; int ctr = 0; cout << " **** A Flag question ****" << endl; cout << endl; cout << "Please Enter 6 numbers"; cin >> num; loop (num, ctr); cout << "Flag has been raised"; return 0; } bool loop ( int num, int ctr) { while ( ctr < 7 ) if (num > 500 || num < 10) return true; else return false; ctr ++; return 0; }
0
#4 25 Days Ago
Your new function, which still has the { } problem jonsca pointed out, will return immediately after the first number is read in, depending on whether it meets the 10-500 test or not.
You should construct the loop to read in six times, and test if the new number is outside the 10-500 range. If so, set a variable to indicate so. After the loop, test that variable to see if there was an out of range number, returning true if so, false otherwise.
In the enhancements to the function, to indicate specifically if number less than 10 or greater than 500 was entered, you'll need to pass two boolean variables by reference to the function, and they will initially be set false, and be set true any time a qualifying value is entered.
I think the purpose of this exercise, besides a simple loop, is to see how you can pass one piece of information back by the return value, but to get multiple pieces of information you must use multiple parameters passed by reference.
The general model for you loop should be something like:
You should construct the loop to read in six times, and test if the new number is outside the 10-500 range. If so, set a variable to indicate so. After the loop, test that variable to see if there was an out of range number, returning true if so, false otherwise.
In the enhancements to the function, to indicate specifically if number less than 10 or greater than 500 was entered, you'll need to pass two boolean variables by reference to the function, and they will initially be set false, and be set true any time a qualifying value is entered.
I think the purpose of this exercise, besides a simple loop, is to see how you can pass one piece of information back by the return value, but to get multiple pieces of information you must use multiple parameters passed by reference.
The general model for you loop should be something like:
C++ Syntax (Toggle Plain Text)
while ( loop still needs to run ) { get input if( input meets test ) set flag } if( flag set ) return true; else return false; }
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 1
0
#9 25 Days Ago
This is what ive gotten so far but I doubt im getting close 

C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main () { int number; int counter = 1; int flags = 1; bool no_flag = true; bool flagged = false; cout << " **** A Flag question ****" << endl << endl; cout << "Please enter a number and press enter, then do this again to check the flags " << endl; while(counter < 7) { cin >> number; cout << "You entered " << number << endl; if (number >= 10 && number <= 500) { return true; } else return false; counter ++; } if (counter < 7 == true) { cout << "Number " << counter << " is between 10 and 500" << endl << "Number " << counter << " has not been flagged" << endl; } else (counter < 7 == false); cout << "Number " << counter << " is either less than 10 or more than 500" << endl << "Number " << counter << " has been flagged " << endl << " There have been " << (flags ++) << " flags"; return 0; }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Multiple Inputs
- Next Thread: How do I link my stuff?
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






