Help: Value in between

I want to make a value in between. I have this problem during my program.

E.G: If value is between 200-299, increment group1
If value is between 300-399, increment group2
If value is between 400-499, increment group3
...
If value is more than 1000, increment group9

But for some reason, whatever value I inputted, for group1-group8 it prints 2 and for group9 it prints 0.

// grossweek.cpp

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
	double salesperson[10];
	int counter = 0;
	int group1 = 0, group2 = 0, group3 = 0, group4 = 0, group5 = 0, group6 = 0, group7 = 0, group8 = 0, group9 = 0;

	cout << "Please input 10 salespeoples' gross sales for that week: \n";

	for (int a = 0; a < 10; ++a)
		cin >> salesperson[a];

	for (int b = 0; b < 10; ++b)
		salesperson[b] = (salesperson[b] * 9/100) + 200;

	cout << "Salespeople No." << setw(18) << "Receives" << "\n";
	for ( int d = 0; d < 10; ++d)
		cout << setw(7) << d << setw(19) << "$" << salesperson[d] << endl;

	for ( int e = 0; e < 10; ++e)
	while ( counter < 10) {
		if (e >= 200 || e < 300)
			++group1; 
			++counter;
		if (e >= 300 || e < 400)
			++group2;
			++counter;
		if (e >= 400 || e < 500)
			++group3;
			++counter;
		if (e >= 500 || e < 600)
			++group4;
			++counter;
		if (e >= 600 || e < 700)
			++group5;
			++counter;
		if (e >= 700 || e < 800)
			++group6;
			++counter;
		if (e >= 800 || e < 900)
			++group7;
			++counter;
		if (e >= 900 || e < 1000)
			++group8;
			++counter;
		if (e >= 1000)
			++group9;
			++counter;
		}

	cout << "Group: \n";
	cout << "\n$200-$299      : " << group1;
	cout << "\n$300-$399      : " << group2;
	cout << "\n$400-$499      : " << group3;
	cout << "\n$500-$599      : " << group4;
	cout << "\n$600-$699      : " << group5;
	cout << "\n$700-$799      : " << group6;
	cout << "\n$800-$899      : " << group7;
	cout << "\n$900-$999      : " << group8;
	cout << "\n$1000 and over : " << group9;
}
I think that this expression in the if statement and others similar should be changed from

if (e >= 200 || e < 300)

to

if (e >= 200 && e < 300)

I changed all to && and now whatever value I input, all group will output zero.
Why does this happens?
Variable e has the value that is less than or equal to 10. So there is no any sense in your code.

1
2
3
	for ( int e = 0; e < 10; ++e)
	while ( counter < 10) {
		if (e >= 200 && e < 300)
Last edited on
In addition to what Vlad's saying:
1
2
3
4
5
6
		if (e >= 200 || e < 300)
			++group1; 
			++counter;
		if (e >= 300 || e < 400)
			++group2;
			++counter;


is equivalent to:
1
2
3
4
5
6
7
8
9
		if (e >= 200 || e < 300)
			++group1; 

		++counter;

		if (e >= 300 || e < 400)
			++group2;

		++counter;


If no braces are supplied:
1
2
3
4
5
        if ( e >= 200 && e < 300 )
        {
                ++group1 ;
                ++counter ;
        }

only one statement after the if is actually dependent on the if condition. This is not determined by indentation.
Topic archived. No new replies allowed.