# Help using loops...

• 10-31-2007
wco5002
Help using loops...
Hi,

I'm pretty much new at C++ and we've begun discussing for-loops in class today. We were given a problem for homework:

Write a program that reads a collection of positive and negative numbers and multiplies only the positive integers. Loop exit should occur when three consecutive negative values are read.

I'm not sure how to use the sentinel function to end when three consecutive negative values are entered. As far as I can suspect, the code should go something like this?:

Code:

``` . . . for (cin >> number; number != SENTINEL; cin >> number) { if (number > 0) number *= number; . . .```
That's all I can really figure out right now, any help would be greatly appreciated.
• 10-31-2007
for loops have the syntax:
Code:

```for(initialization; loop condition; counter control) {   // your code here }```
for example, to print out the first 5 positive integers, one may do:
Code:

```int i; for (i = 0; i < 5; i++) {   cout << i << endl; }```
here the initialization part of the for loop declaration is 'i = 0', the loop condition is 'i < 5' and the counter control is 'i++'.

the first part, initialization, is usually used to set the variable ('counter') to some initial ('starting') value. the loop condition (middle part) means that the loop will only execute if the statement there is true (ie loop while i < 5, here). the counter control simply increments the counter ('i') by one, after each run through the loop, or 'iteration'. note that we left out 'i++', the loop would never end, as i is always zero.

for your situation, i would declare an int starting at 0, before the for loop. this will be used to count the number of negative numbers. that is, each time you read in a negative number, increment this negative number counter, and if you read in a non-negative number set the counter back to 0. it will be used to control your for loop. ie, you want to loop 'while the last 3 numbers entered are negative.'

this isnt all of the solution, but i hope it helps.
• 10-31-2007
cyberfish
Quote:

[note: with some compilers you may have to declare ('int i') your variable before the for loop]
I thought that was the case only in C?
• 10-31-2007
Quote:

Originally Posted by cyberfish
I thought that was the case only in C?

i think your right cyberfish, which is why i removed it to prevent confusion to the op.
lately, ive just been using C on solaris, and i know thats the case there.
• 10-31-2007
dwks
Declaring variables in for loop initialization sections is standard C++. Only really old, pre-standard C++ compilers might not support it.

On the other hand, some compilers mess it up. For example, MSVC 6 treats this
Code:

`for(int x; ; ) {}`
exactly as if you'd written
Code:

```int x; for( ; ; ) {}```
This can cause some problems; for example, this is valid
Code:

```for(int x; ; ) {} std::cout << x;```
but this is not:
Code:

```for(int x; ; ) {} for(int x; ; ) {}```
As for C, in C89 declaring variables in this way is invalid -- but in C99, it's allowed.

Anyway, with standard C++, it's perfectly acceptable to declare variables in for loop initialization sections. Ignore my ramblings, unless you happen to be using MSVC 6, in which case get a newer compiler. :)

Also see this brief lesson: http://www.cprogramming.com/tutorial/lesson3.html