You are using a version without Ads of this website. Please, consider donating:

### LOOP problem

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 do not want the code written for me i just want some help.
I'm not sure how to use the sentinel function to end when the three consecutive negative values are entered.

I did the code below as a start but i dont know if its relevant. Is it?

 ``123456789101112131415`` ``````#include using namespace std; int main() { int i; for(i=0;i<5;i++) { cout<

Also should the numbers be randomly generated so there is variation in numbers?
Any input is appreciated. Thanks
As it is said in your assignment the program have to read a collection. That is I think you should use standard input stream std::cin to read integers until three consecutive negative integers will be encountered. So you need a count of negative numbers in the loop.
Ok. Would it be something like this ?:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` ``````#include using namespace std; int main() { int number; int positive=0; int negative=0; //Read the Data double average; cout <<" Enter first Number : "; cin >> number; while (number != 0) { if (number > 0) positive++; else negative++; cout <<" Enter second Number : " ; cin >> number; cout << endl; cout <<" there are "<
This condition in the loop

while (number != 0)

is wrong. There is nothing said in the assignment that entered values can not be equal to zero.
Why not use getchar()?

/* getchar example : typewriter */
#include <stdio.h>

int main ()
{
char c;
puts ("Enter text. Include a dot ('.') in a sentence to exit:");
do {
c=getchar();
putchar (c);
} while (c != '.');
return 0;
}

Inside the loopp just put an if char is desired condition, then multiply that char. Process 1 number at a time.
ok I changed that, but the part where i have
 ``123`` ``````cout <<" there are "<

It does not work properly, like if 1 and -1 is put for the two numbers it says 1 positive and 0 negative why is that?
The loop can look for example the following way

 ``1234567891011121314151617`` ``````const int MAX_CONSECUTIVE_NEGATIVE = 3; int number; int negative_count = 0; int positive_count = 0; while ( negative_count != MAX_CONSECUTIVE_NEGATIVE && std::cin >> number ) { if ( number < 0 ) { negative_count++; } else { negative_count = 0; if ( 0 < number ) positive_count++; } }``````
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: