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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
int i;

 for(i=0;i<5;i++)
  {
   cout<<i<<endl;
  }
  
  
 system("PAUSE");
 return EXIT_SUCCESS;
}


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 ?:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
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 "<<positive<<" Positives "<<endl; 



cout <<" there are "<<negative<<" Negatives " << endl; 

cout <<endl;

system("PAUSE");
return EXIT_SUCCESS;
}
}
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
1
2
3
cout <<" there are "<<positive<<" Positives "<<endl; 

cout <<" there are "<<negative<<" Negatives " << endl; 


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.