Loops - Add only positive numbers

I am writing a program that will allow the user to enter numbers to add, BUT if the user enters three negative numbers, it has to exit the loop and display an error. This is homework, so please don't give me the answer, just point me in the right direction/pseudo code.

I got it to exit the loop on the entry of the first negative number, but am having trouble getting it to exit the loop on the third.

Here is my code:
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
#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int n = 0;	//negative number counter
int num;	//input: user enters number
int sum = 0;	//output:  display sum


cout << "Please enter your numbers." << endl;

//start loop
while (num < 0) {
      cin >> num;
      sum = num + sum;
      cout << sum;
      n = n + 1;
	
	if (n = 3) {
	cout << "Error - you entered three negative numbers." << endl;
	}
	else
}
	sum = num + sum;
	cout << sum << endl;	 /*displaying sum after each addition to make sure math correct - will remove after working */ 
	
return 0;
}


Here is the output:

Please enter your numbers.
4


Where did I go wrong for it to automatically output 4?

Thanks.
closed account (S6k9GNh0)
if (n = 3)
I think you want while( n < 3 ) { ... }.
You are comparing num before it has an assigned value, so it contains garbage. (If you really wanted to, you could do do { ... } while( num < 0 );.)
I really don't know why it's outputting 4 right offhand though.

Also, I think you wanted lines 29 and 30 inside the loop, but the looks of your comment.

Check line 24: should have been n == 3, but you won't need that after you change the while loop's condition.

On line 27, else has no body. You most likely wanted the below lines to be in it's body.
Last edited on
the
if(n = 3)
will ALWAYS evaluate to true... you need the == bool operator.

there are two simple ways to evaulate the three count. one is
while loop with a ++.

or
an if break.

both will work
OK - I think I made the corrections suggested. However, now it allows me to enter one number. If it is a positive number it simply goes back to the command prompt. If I enter a negative number it gets caught in an infinite loop seems to add the negative number entered by itself, over, and over.

Positive Number entered Output:

Please enter your numbers.
3


Negative Number entered Output:

Please enter your numbers.
-2
-2-4
-6-8
-10-12
-14Error - you entered three negative numbers.
-14
-16-18
-20-22
-24-26
-28-30
-32-34
-36-38
-40-42
-44-46
etc.



@Matthead200,
I tried using (n < 3) and it will exit the loop after one negative number is entered, instead of after the third, which is why I decided to change it to (n == 3). I could be wrong, but I just need it to exit after the third neg. number is entered not after the first.

I added lines 20 through 30 inside the loop. Regarding "you won't need that after changing the while condition". Can you clarify?

@ ui uiho -
I was actually trying to write a while loop to handle the three count. Are you saying that I need an additional while loop, in addition to the one I have already written?

Thanks,
A
try to use the break comand, this comand make u leave the loop when he comes

could you post the code? and the break statement ends the inner most loop.
I re-wrote this code using a different loop. I am still not getting this to work, and just to be honest, am getting really frustrated that this program is baffling me.

I need this program to (just want to be clear on what I am trying to do):
1. ANY negative number is NOT to be added
2. When program is to be terminated, it displays the sum not an error message.
3. Program terminates when three CONSECUTIVE numbers are given. It means if a non-negative number is given even after two negative numbers, you need to forget the previous count of negative numbers.

Here is my code:
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
#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int n;	//negative number counter
int num;	//input: user enters number
int sum = 0;	//output:  display sum of only positive numbers

cout << "Please enter your numbers." << endl;

//start loop
do {
cin >> num;
sum = sum + num;
cout << sum << endl;


}
while (num > 0);
/* I know I need to tell this what to do for negative numbers here, just not sure what and how - use if stmnts or another loop?*/
num = 0;  //if number is negative set it to zero, but only if it is the 1st or 2nd neg num
n = 0;     //start neg num counter at zero
n = n + 1;// update the counter after each neg number encountered

return 0;
}


Output:

v245-2% ./a.out
Please enter your numbers.
2
2
3
5
6
11
-3 shouldn't exit the loop here - only on the third
8 shouldn't display sum unless it is positive or 3rd neg
v245-2%
this works
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
int main(){
int i, int counter = 0, sum = 0;
while(counter != 3){
std::cout << "Enter a number:";
std::cin >> i;
if( i > 0) sum += i;
else counter++;
std::cout << "current sum is: " << sum; 
}
return 0; 
}

you are not using math.h so do not include it.
if statement inside while is better for this than the do while loop. do while loop is really only used just to make sure your loop executes once before the while statement is evaluated, otherwise just use a while loop.
OK I can't use your code. So - I will re-write using a while loop and post back - can take it from there.
@ui uiho -
Thank you for your help. I re-wrote it using my "vernacular". It does work and I have some questions.

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
#include <iostream>

using namespace std;

int main()    

{

int n = 0;		//counter
int num;	//input: user enters number
int sum = 0;	//output:  display sum of only positive numbers

//start loop

while (n != 3) {
	cout << "Enter numbers please." << endl;
	cin >> num;

	if (num > 0) 
    	    sum += num;
	else 
    	    n++;

cout << "Sum is: "<< sum << endl;
}

return 0;
}


How did you know to tuck the if statement inside the loop?

Why did you decide to start off dealing with the counter part of the problem first? In other words, I had trouble deciding what to deal with first, the number being less than zero or the counter.

Thank you for your help again.
How did you know to tuck the if statement inside the loop?

Why did you decide to start off dealing with the counter part of the problem first? In other words, I had trouble deciding what to deal with first, the number being less than zero or the counter.


Answer to Q1 and Q2:
Terminating condition is at the top in a while loop.
What controls the loop? The counter in this case because it is needed to determine WHEN to exit to the loop.
IF statement goes inside the loop because need to determine each number as it is entered as being positive or negative, and in addition need to have it count how many negative numbers are entered.
Wonder if I could have used a nested loop?
Whenever you're questioning your design, work the problem on paper/by hand. Then try to follow your code and see if they're doing the same thing.

Yea, nested loops could would work too, but if-else is much easier to read.
Topic archived. No new replies allowed.