944,091 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 742
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 1st, 2009
0

Loop help and such.

Expand Post »
Hey all.
I'm really new to programming and would like some help, if you all don't mind

I'm not actually having trouble with the programming, but rather, what I'm being asked to do.

Here is what I'm supposed to do (and yes, it is an assignment, but I'm only looking for help and not the answers):
# This program should read in 6 numbers from the keyboard and then indicate whether any of them were larger than 500 or smaller than 10. It is possible that both events (being larger than 500, being less than 10) occurred, that neither did or that only one of the two did. And it is possible that the same event happened more than once.

# First, write the program so that it reads in 6 numbers and echoes them to the screen. You must use a loop. Think about how it will be controlled.

# Then add in a flag that will detect numbers larger than 500. Use a bool variable for this.

# Then add in another flag that will detect numbers smaller than 10. Again use a bool variable.

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

could anyone tell me why I'm supposed to use a loop? And what flags are? We haven't gone over any of this at all.

Thanks for the help guys =),
Trip

Edit***
Sorry if i posted this in the wrong section.
Last edited by A Tripolation; Nov 1st, 2009 at 6:06 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
1
Re: Loop help and such.
I would have solved the problem as follows.
Create an array of objects of std:: pair <int, int>. In the loop, when you enter values would determine how to categorize this number. And on the test results would set the flag.
ex.:
C++ Syntax (Toggle Plain Text)
  1. typedef std::pair<int, int> item;
  2. std::vector<item> array;
  3. ...
  4. for ( int i = 0; i < N; i++ ) {
  5. std::cin >> ...
  6. item it;
  7. it.first = (value) /** fro std::cin >> */
  8. it.second= (expression?) /** set a flag */
  9.  
  10. }
Last edited by niXman; Nov 1st, 2009 at 6:36 pm.
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Nov 1st, 2009
0
Re: Loop help and such.
Well we aren't allowed to use anything we haven't learned, and I most certainly haven't learned arrays.

But thank you for the help. I appreciate it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
0
Re: Loop help and such.
Well we aren't allowed to use anything we haven't learned, and I most certainly haven't learned arrays.

But thank you for the help. I appreciate it.
I will just write pseudo code so you can do the assignment by yourself .

C++ Syntax (Toggle Plain Text)
  1. bool bigger;
  2. bool smaller;
  3. while (ii < 6)
  4. {
  5. Read value from std. input;
  6. biger = value > 500; // flag for bigger
  7. smaller = value < 10; // flag for smaller
  8. ++ii;
  9. }
  10.  
  11. Write message to std. output.

It is not complete but you see the idea. Hope this helps.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SriHanuman is offline Offline
4 posts
since Oct 2009
Nov 1st, 2009
0
Re: Loop help and such.
Ha, no, that's not clear to me at all.
But thanks for trying.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
0
Re: Loop help and such.
Ha, no, that's not clear to me at all.
But thanks for trying.

Hm...well just try writing it. It may take a while but you will learn something. Example is nearly done, you just need to add reading from standard input and writing to standard output. Just think about it, it will come to you
Last edited by SriHanuman; Nov 1st, 2009 at 7:16 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SriHanuman is offline Offline
4 posts
since Oct 2009
Nov 1st, 2009
0
Re: Loop help and such.
2 SriHanuman
Assume that the values falling in any subcategory few, what then?
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Nov 1st, 2009
0
Re: Loop help and such.
http://pastebin.com/d4f85ce82

Could anyone tell me what I'm doing wrong here?
And quite a lot of this is derived from UKmason's work. Just giving credit where it's due.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
A Tripolation is offline Offline
12 posts
since Nov 2009
Nov 1st, 2009
0
Re: Loop help and such.
[edit]Er, wait...
Last edited by Dave Sinkula; Nov 1st, 2009 at 7:50 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Nov 1st, 2009
0
Re: Loop help and such.
The following code is unreachable:
if (counter < 7 == true)
{
	cout << "Number " << counter << " is between 10 and 500" << endl; 
	cout << "Number " << counter << " is not flagged. " << endl;
}

else (counter < 7 == false);
{	
	cout << "Number " << counter << " is either less than 10 or more than 500" << endl; 
	cout << "Number " << counter << " is flagged." << endl; 
	cout << "There have been " << (flags ++) << " flags";
}
Because you return from main within the while loop.

An else doesn't have conditions like you used there.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: i want to know......
Next Thread in C++ Forum Timeline: sort code & write exchange increment line





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


Follow us on Twitter


© 2011 DaniWeb® LLC