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.
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.
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.
@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.
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.
#include <iostream>
#include <math.h>
usingnamespace 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.
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.
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.
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. :\
@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. ;)
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.
#include <iostream>
#include <math.h>
usingnamespace 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;
}