944,174 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 696
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2009
0

Need help with a simple while loop

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7. int num;
  8. int ctr = 0;
  9.  
  10. cout << " **** A Flag question ****" << endl;
  11. cout << endl;
  12. cout << "Please Enter 6 numbers";
  13.  
  14.  
  15. while ( ctr < 7 )
  16. cin >> num;
  17. if (num > 500 || num < 10)
  18. return true;
  19. else
  20. return false;
  21.  
  22. ctr ++;
  23.  
  24. return 0;



Thanks guys
-UKmason
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Oct 31st, 2009
1
Re: Need help with a simple while loop
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.
Last edited by jonsca; Oct 31st, 2009 at 3:37 pm.
Sponsor
Featured Poster
Reputation Points: 1165
Solved Threads: 578
Quantitative Phrenologist
jonsca is offline Offline
4,271 posts
since Sep 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool loop ( int num, int ctr);
  6.  
  7. int main ()
  8. {
  9. int num;
  10. int ctr = 0;
  11.  
  12.  
  13. cout << " **** A Flag question ****" << endl;
  14. cout << endl;
  15. cout << "Please Enter 6 numbers";
  16. cin >> num;
  17.  
  18. loop (num, ctr);
  19. cout << "Flag has been raised";
  20.  
  21.  
  22. return 0;
  23. }
  24. bool loop ( int num, int ctr)
  25. {
  26. while ( ctr < 7 )
  27. if (num > 500 || num < 10)
  28. return true;
  29. else
  30. return false;
  31.  
  32. ctr ++;
  33.  
  34. return 0;
  35. }
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
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:
C++ Syntax (Toggle Plain Text)
  1. while ( loop still needs to run )
  2. {
  3. get input
  4. if( input meets test )
  5. set flag
  6. }
  7. if( flag set )
  8. return true;
  9. else
  10. return false;
  11. }
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 1st, 2009
0
Re: Need help with a simple while loop
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
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
Ha, you are so in my class man.
Dr. Keen? Isn't this assignment brutal?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
no doubt, im probably gonna fail it
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
I can't get my damn loop to even work, let alone setup bool tests.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
This is what ive gotten so far but I doubt im getting close

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int main ()
  6. {
  7. int number;
  8. int counter = 1;
  9. int flags = 1;
  10. bool no_flag = true;
  11. bool flagged = false;
  12.  
  13. cout << " **** A Flag question ****" << endl << endl;
  14. cout << "Please enter a number and press enter, then do this again to check the flags " << endl;
  15.  
  16. while(counter < 7)
  17. {
  18. cin >> number;
  19. cout << "You entered " << number << endl;
  20.  
  21. if (number >= 10 && number <= 500)
  22. {
  23. return true;
  24. }
  25. else
  26. return false;
  27.  
  28.  
  29. counter ++;
  30.  
  31. }
  32. if (counter < 7 == true)
  33. {
  34. cout << "Number " << counter << " is between 10 and 500" << endl << "Number " << counter << " has not been flagged" << endl;
  35. }
  36. else (counter < 7 == false);
  37. cout << "Number " << counter << " is either less than 10 or more than 500" << endl << "Number " << counter << " has been flagged " << endl << " There have been " << (flags ++) << " flags";
  38.  
  39. return 0;
  40. }
Reputation Points: 10
Solved Threads: 1
Light Poster
UKmason is offline Offline
43 posts
since Oct 2009
Nov 1st, 2009
0
Re: Need help with a simple while loop
Ha, wow, that's 10x what I have.
Well, good luck.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009

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: Multiple Inputs
Next Thread in C++ Forum Timeline: How do I link my stuff?





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


Follow us on Twitter


© 2011 DaniWeb® LLC