Reputation: 19
I'm very new to C++ and have an assignment to edit this code to make it work. For some reason, when I use a combination of multiple positive or negative numbers, it gives the answer that I've entered multiple numbers an extra number.
Example:
I enter 3 postive numbers and 1 negative number. The counter displays 4 positive and 1 negative. Same thing happens if I start with negative numbers instead of positive.
Any help would be greatly appreciated. Here's the code:
#include <iostream>
using namespace std;
int main()
{
int number = 0;
int positive = 0; //counter
int negative = 0; //counter
int totalPositive = 0;
int totalNegative = 0;
//get a number
cout << "Enter a positive or negative integer (enter 0 to end): ";
cin >> number;
while (number != 0)
{
//update counters
if (number > 0)
{
positive =+ 1;
}
else
negative =+ 1;
//end if
//get another number
cout << "Enter another positive or negative integer (enter 0 to end): ";
cin >> number;
totalPositive += positive;
totalNegative += negative;
}//end while
//display counters
cout << endl;
cout << "Total positive numbers: " << totalPositive << endl;
cout << "Total negative numbers: " << totalNegative << endl;
system("pause");
return 0;
} //end of main function
Upvotes: 1
Views: 296
Reputation: 591
Firstly note the =+
is not an operator (Unlike +=
which increments the value)
The line
positive =+ 1
is just interpreted as assigned +1
to positive (which is what you want) but can be just written as
positive = 1
This though brings us to the actual point which is that you don't reset positive or negative back to zero each time around the loop.
Once both positive and negative are assigned to 1
(i.e. when you have entered at least one of each), every time both totalPositive
and totalNegative
will be incremented.
You may want to consider whether you need the variables positive or negative at all.
Upvotes: 1
Reputation: 1085
Well your counters are not reset to 0 after your loop finishes.
When you enter your loop the first time, the counters are 0 (positive, negative), but the second time around, the counters are still 1, resulting in a bad counter.
When entering the loop:
positive = 0;
negative = 0;
//update counters
This code, could be highly refactored to be more efficent, but that's for another question.
Upvotes: 0
Reputation: 201
Try this code:
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int number;
int positive = 0;
int negative = 0;
do{
cout << "Enter a positive or negative integer (enter 0 to end): ";
cin >> number;
if (number > 0)
positive += 1;
else if(number<0)
negative += 1;
}while(number!=0);
//end while
//display counters
cout << endl;
cout << "Total positive numbers: " << positive << endl;
cout << "Total negative numbers: " << negative << endl;
system("pause");
return 0;
} //end of main function
You don't have to use total positive
and total negative
, unless you want to have the summation of the positive numbers and the negative ones.
Upvotes: 1
Reputation: 206567
The logic error in your program is that positive
and negative
never get reset to 0
once they are set to 1
.
If your first two numbers are such that one is a negative number and the other is a positive number, totalPositive
and totalNegative
will keep increasing after that regardless of the value of number
.
You can simplify your logic to:
//get a number
cout << "Enter a positive or negative integer (enter 0 to end): ";
while (cin >> number && number != 0)
{
//update counters
if (number > 0)
{
++totalPositive;
}
else
{
++totalNegative;
}
//get another number
cout << "Enter another positive or negative integer (enter 0 to end): ";
}//end while
Upvotes: 1
Reputation: 736
SOmething that might help is to do this for the while loop instead
while (number != 0)
{
positive = 0;
negative = 0;
//update counters
if (number > 0)
{
positive = 1;
}
else
negative = 1;
//end if
//get another number
cout << "Enter another positive or negative integer (enter 0 to end): ";
cin >> number;
totalPositive +=positive;
totalNegative +=negative;
}
Otherwise you're doing this thing that's basically 1 + 2 + 3 + 4... for every time the loop runs. By setting each value to 0, it ensures that totalPositive only increases once every loop. Otherwise it grows exponentially
Upvotes: 0
Reputation: 2505
positive
is accumulating, but totalPositive
is accumulating your accumulation. Try totalPositive++
(and the negative version) in your if
statement, and eliminate variables positive
and negative
.
Upvotes: 1