Need help with a simple while loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster

Need help with a simple while loop

 
0
  #1
25 Days Ago
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

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 300
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 32
jonsca jonsca is online now Online
Posting Whiz
 
1
  #2
25 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 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; 25 Days Ago at 3:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #3
24 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

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,674
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso
 
0
  #4
24 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:
  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. }
"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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #5
24 Days Ago
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: A Tripolation is an unknown quantity at this point 
Solved Threads: 0
A Tripolation A Tripolation is offline Offline
Newbie Poster
 
0
  #6
24 Days Ago
Ha, you are so in my class man.
Dr. Keen? Isn't this assignment brutal?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #7
24 Days Ago
no doubt, im probably gonna fail it
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: A Tripolation is an unknown quantity at this point 
Solved Threads: 0
A Tripolation A Tripolation is offline Offline
Newbie Poster
 
0
  #8
24 Days Ago
I can't get my damn loop to even work, let alone setup bool tests.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 43
Reputation: UKmason is an unknown quantity at this point 
Solved Threads: 1
UKmason UKmason is offline Offline
Light Poster
 
0
  #9
24 Days Ago
This is what ive gotten so far but I doubt im getting close

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 12
Reputation: A Tripolation is an unknown quantity at this point 
Solved Threads: 0
A Tripolation A Tripolation is offline Offline
Newbie Poster
 
0
  #10
24 Days Ago
Ha, wow, that's 10x what I have.
Well, good luck.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC