Loop issues

Mar 30, 2012 at 8:41pm
This is homework, so please, NO CODE. If you can just tell me what areas to concentrate on fixing.

I am to write a program that will ignore any negative integer values read as data and finds the sum of only the positive numbers. Loop exit should occur after a sequence of three negative values is read. I have to write this program as a while loop and as a do, while loop.

This is my while loop below.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int c; 
int N1, N2, N3, N4, N5, N6;
int sum;

cout << "Enter six numbers." << endl;
cin >> N1, N2, N3, N4, N5, N6;

//start loop

while ( c < 3) {
	if ( N1 < 0) {
	    N1 == 0; 
	    c = c++;
	}
	else 
	    N1 = N1;
	if ( N2 < 0) {
	    N2 == 0;
	    c = c++;
	}
	else
	   N2 = N2;
	if (N3 < 0) {
	    N3 == 0;
	    c = c++;
	}
	else
	    N3 = N3;
	if (N4 < 0) {
	    N4 == 0;
	    c = c++;
	}
	else 
	    N4 = N4;
	if (N5 < 0) {
	    N5 == 0;
	    c = c++;
	}
	else
	    N5 = N5;
	if (N6 < 0) {
	    N6 == 0;
	    c = c++;
	}
	else
	    N6 = N6;
	
sum = N1 + N2 + N3 + N4 + N5 + N6;
cout << sum << endl;
}

cout << "You entered more than three negative numbers. Error.";



return 0;
}


The code will run with no compilation errors, but it does not give the right sum and it will not stop the loop. I am using if statements inside the loop to determine if a number is negative.
Mar 30, 2012 at 8:48pm
1) First work on your loop condition. You are limiting inputs to 6 numbers, what happens when all 6 are positive.

2) Try making a while loop that exits on the first occurrence of a negative number, then work from there. Get your summation working correctly for this, then add in the negative counter for 3 total negatives.
Last edited on Mar 30, 2012 at 9:09pm
Mar 30, 2012 at 8:49pm
Here

while ( c < 3)

you compare 'c' with the value but 'c' was not initialized.

I think that your code incorrect in whole. You should read values in a loop and after entering each number check whether it is negative or not.
Last edited on Mar 30, 2012 at 8:49pm
Mar 30, 2012 at 9:05pm
@clanmjc OK, will give both of your suggestions a try.
@vlad Thank you for catching that first part. I am not sure what you are trying to say after that.
Mar 30, 2012 at 9:47pm
OK I have shortened the above code just for the basis of determining whether the number is negative or positive. Even though I have initialized the counter and have used it as the terminating statement, it still goes through the loop inifinitely.

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 c = 1; 
int N1;
int sum;

cout << "Enter a number." << endl;

//start loop
while ( c < 3) {
	cin >> N1;

	if ( N1 < 0) {
	   cout << "negative" << endl;  
	   	}
	else 
	    cout << "positive" << endl;	 
c = c++;	    
}
 cout << "This went through three times. It will now stop." << endl;

	

return 0;
}


By the way, I changed this c=c++; to this c = c +1; and I also changed int c = 1; to int c = 0; and it will now determine if the number is negative or positive and will go through the iteration three times.

I will now try to see if I can allow for the user to enter a number and exit on the first occurrence of a negative number and post back. Just showing my thoughts step by step.
Last edited on Mar 30, 2012 at 9:58pm
Mar 30, 2012 at 11:02pm
OK, I think I came up with something here and the math checks out, BUT if I enter just one negative number (as opposed to three) it still displays the error message. If I in fact enter three negative numbers, it displays the error message, does the correct math, but it doesn't ignore the negative numbers. Per the original post:
will ignore any negative integer values read as data and finds the sum of only the positive numbers. Loop exit should occur after a sequence of three negative values is read.


Here is the new 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int n = 0;
int N1, N2, N3, N4, N5, N6;
int sum;

cout << "Enter six numbers." << endl;

//start loop
while ( n < 2) {
	cin >> N1 >> N2 >> N3 >> N4 >> N5 >> N6;

	if ( N1 < 0) {
	    N1 == 0; 
	    n = n + 1;
	}
	else 
	    N1 == N1;
	if ( N2 < 0) {
	    N2 == 0;
	    n = n + 1;
	}
	else
	   N2 == N2;
	if (N3 < 0) {
	    N3 == 0;
	    n = n + 1;
	}
	else
	    N3 == N3;
	if (N4 < 0) {
	    N4 == 0;
	    n = n + 1;
	}
	else 
	    N4 == N4;
	if (N5 < 0) {
	    N5 == 0;
	    n = n + 1;
	}
	else
	    N5 == N5;
	if (N6 < 0) {
	    N6 == 0;
	    n = n + 1;
	}
	else
	    N6 == N6;	

sum = N1 + N2 + N3 + N4 + N5 + N6;
cout << sum << endl; 	    
}
 cout << "You entered three negative numbers - error." << endl;

	

return 0;
}


Further suggestions? I tried using this bit of code:
1
2
3
4
if ( N1 < 0) {
	    N1 == 0; 
	    n = n + 1;
	}
to have the program take the negative number and equal it to zero - thinking this would allow the program to "ignore" in a sense the negative number but still add and get the sum. Did I go about this wrong?

In addition, if all the numbers input are positive, it will do the math correctly but won't exit the loop.

Man, I know I am missing something simple about this loop and I just can't grab it! Grrrrr.
Last edited on Mar 30, 2012 at 11:35pm
Mar 31, 2012 at 1:16pm
Ha! I think I figured out something else (believe it or not this came to me in the middle of the night, and I am sick taking Benadryl - lol). The reason that the program keeps exiting the loop after one negative number is encountered is because it is doing what I TOLD it to do: if n < 3. So it should read: n == 3. Off I go to give this a shot! May still need help with having it ignore the negative numbers in the sum output though. :\
Mar 31, 2012 at 1:44pm
OK officially stumped going to go to my prof on this. Thanks for all the help.
Mar 31, 2012 at 3:29pm
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
#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int n = 0;
int numbers[6]={0};
int sum=0;

cout << "Enter six numbers." << endl;

//start loop
for(int i = 0; i < 6; i++)
{
	cout<<"["<<(i+1)<<"]"<<" Enter number :";
	cin >> numbers[i];
	if ( numbers[i] < 0) {
	    numbers[i]=0; 
		n++;   
	}
	if(n == 3)
	{
	 cout << "You entered three negative numbers - error." << endl;
	 break;
	}
}
if(n<3)
{
	for(int i= 0; i < 6 ;i++)
	{
		sum += numbers[i];
	} 	  
	cout <<"The Sum is :" <<sum << endl;
}


	

return 0;
}
Last edited on Mar 31, 2012 at 3:31pm
Mar 31, 2012 at 7:06pm
@subzero030201 - thanks for the help but I can't use that code. We haven't gone over using the whole numbers[6]={0} thing. Prof would know someone else wrote it, but thanks none the less. I already wrote him and he is having me actually re-think my logic/flowchart of how to write it. Mental note to self: when taking cold medication, don't write code. ;)
Mar 31, 2012 at 9:18pm
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <math.h>

using namespace std;

int main()    

{

int c=0; 
int N1=0, N2=0, N3=0, N4=0, N5=0, N6=0;
int sum;
char choice = '-';



//start loop

while ( true) {
	c=0;
	sum =0;
	cout << "Enter six numbers." << endl;
	cin >> N1>> N2>> N3>> N4>> N5>> N6;

	if ( N1 < 0) {
	    N1 = 0; 
	     c++;
	}
	else 
	    N1 = N1;
	if ( N2 < 0) {
	    N2 = 0;
	     c++;
	}
	else
	   N2 = N2;
	if (N3 < 0) {
	    N3 = 0;
	   c++;
	}
	else
	    N3 = N3;
	if (N4 < 0) {
	    N4 = 0;
	   c++;
	}
	else 
	    N4 = N4;
	if (N5 < 0) {
	    N5 = 0;
	    c++;
	}
	else
	    N5 = N5;
	if (N6 < 0) {
	    N6 = 0;
	    c++;
	}
	else
	    N6 = N6;
	if (c > 3)
	break;
	sum = N1 + N2 + N3 + N4 + N5 + N6;
	cout << sum << endl;
	cout<<"Do you want to run this again y for yes n for no : ";
	cin>>choice;
	if (choice == 'n'|| choice=='N')
	{
		break;
	}

}
if(c>3)
{
	cout << "You entered more than three negative numbers. Error."<<endl;
}
cout<<"Good Bye"<<endl;


return 0;
}
Apr 2, 2012 at 4:52pm
Just wanted to say that I got this to work after the first negative number was entered, and I was TOTALLY going about this the hard way/wrong way before. I blame Benadryll. Anyway, here is my improved code, now I am working on the part about having it exit after three negative numbers. Well, that and having it also at some point ask the user if they want to continue entering numbers vs. typing in positive numbers for all eternity.

Also, thanks to those who tried to help me. Sometimes I have just have to be shown in person where I am going wrong. Sigh.

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
#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 of only positive numbers

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

//start loop
while (num > 0) {
	cin >> num;
	sum = num + sum;
	cout << sum << endl; 
}

cout << "You entered a negative number - error." << endl;
	
return 0;
}


Topic archived. No new replies allowed.