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

#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

Recommended Answers

All 10 Replies

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 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.

commented: nice reply.. +1

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 :)

#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;
}

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:

while ( loop still needs to run )
   {
        get input
        if( input meets test )
            set flag
    }
    if( flag set )
       return true;
    else
      return false;
}

I see what youre saying but, im really new at programming and dont know how to do it

My main function looks okay?

I just need to fix my loop?, if so I still need help with that, my boyfriend tried to help me over the phone but he couldnt help me much :(

Ha, you are so in my class man.
Dr. Keen? Isn't this assignment brutal?

no doubt, im probably gonna fail it :(

I can't get my damn loop to even work, let alone setup bool tests.

This is what ive gotten so far but I doubt im getting close :(

#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;
}

Ha, wow, that's 10x what I have.
Well, good luck. :)

Your while loop is fine. I would ask yourself if 6 people came up to you and handed you a card with a number on it, how would you tell if the numbers fell into those categories. Hint, can a number be both less than 10 AND greater than 500? No. Those are the two things you are measuring. There are a number of different ways to test for it but just break down the logic, is this less than 10? No. Is it greater than 500? Yes. Ok, we don't care how many times this condition comes up, just switch the flag to true. Are there any values that are less than 10 OR greater than 500.... Just write it out the logic on a piece of paper and translate your words into the appropriate code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.