Need Some Help Finding the Problem!!!!!!!

Nov 9, 2013 at 9:57pm
So this is the question I was given and I have also put my program in ... I am a beginner and don't see where the mistake is in my program can some help me detect the problem? I'd appreciate not having people rewrite the program unless mine is completely wrong but if they can help in showing me where I am going wrong that's not allowing the program to function Thank you and every little bit of help is appreciated .........

/*Use a single-subscripted array to solve the following problem. A company
pays its salespeople on a commission basis. The salespeople receive
$200 per week plus 9 percent of their gross sales for that week. For
example, a salesperson who grosses $3000 in sales a week receives $200
plus 9 percent of $3000, or a total of $470. Write a C program (using
an array of counters) that determines how many of the sales people earned
salaries in each of the following ranges (assume that each salesperson’s
salary is truncated to an integer amount);
a) $200 - $299
b) $300 - $399
c) $400 - $499
d) $500 - $599
e) $600 - $699
f) $700 - $799
g) $800 - $899
h) $900 - $999
i) $1000 and over*/

#include <stdio.h>
#include <iostream>

using namespace std;

int GROSS();

int main(void)

{
int counting[9], i;
char sale;

for (i = 0; i < 11; i++)
{
counting[i] = 0;
}

counting[GROSS()]++;

printf("\nIs there another salesperson 'yes' or 'no': ");
cin >>sale;

if (sale == 'yes')
{
counting[GROSS()]++;
}

if (sale == 'no')
{
printf("Amount of salaries earned by salespersons in each of the following ranges:\n");
printf("--------------------------------------------------------------------------\n");
printf(" RANGE COUNT\n");

int j;

for (j = 2; j <= 9; j++)
{
int k = 0;

printf("$%i00 - $%i99 %i\n", j, j, counting[k]);

k++;

}
printf("over $900 %i\n", counting[8]);
}

}

int GROSS()
{
int gross, k;

printf("\nPlease enter the salespersons gross sales for that week: $");
cin >>gross;

double total = 200 + (gross * 0.09);

if (total >= 200 && total <= 299)
{
k = 0;
}

if (total >= 300 && total <= 399)
{
k = 1;
}

if (total >= 400 && total <= 499)
{
k = 2;
}

if (total >= 500 && total <= 599)
{
k = 3;
}

if (total >= 600 && total <= 699)
{
k = 4;
}

if (total >= 700 && total <= 799)
{
k = 5;
}

if (total >= 800 && total <= 899)
{
k = 6;
}

if (total >= 900 && total <= 999)
{
k = 7;
}

if (total > 999)
{
k = 8;
}

return k;
}
Nov 10, 2013 at 12:40am
I think if you solve issues listed below, you are good to go:

Why do you declare loop counters outside the for loops? Why you'd want to keep unused variable when the loop exits?

You write your loops in a way, that invites this kinds of errors:
1
2
3
4
5
6
int counting[9], i; // <= 9? 
char sale;
for (i = 0; i < 11; i++) // <= 11?
{
	counting[i] = 0; // <= Ouch! out of bounds!
}

1
2
3
if (sale == 'yes')  // single quotes are for chars
//..
if (sale == 'no')   // but these are strings 

This took me a while to grasp, it's difficult to follow:
1
2
3
4
5
6
7
int j;
for (j = 2; j <= 9; j++) // why  2 to 9? This should cover counting[] indices!
{
	int k = 0; // you declare k here, k is always 0 at this point
	printf("$%i00 - $%i99 %i\n", j, j, counting[k]); // its always k=0
	k++;   // this line is useless, k will be redeclared next iteration
}

To make it easier, and help you avoid such mistakes, you should always try to create for loops with counter starting at 0 and end condition i < maxIterations //(not i <= maxIterations!) . If you need to derive some variables from the counter, do it in loop's body:
1
2
3
4
5
6
7
8
9
10
11
12
for (int j = 0; j < 9; j++) // indices 0-8
{
	int k = j+2; // values 2-10
	if (k <10)
	{
		printf("$%i00 - $%i99 %i\n", k, k, counting[j]);
	}
	else // you don't really need this distinction, but I kept it for reference
	{
		printf("   over $900 %i\n", counting[j]);
	}
}

Logical comparisons are quick operations, but still you should aim to minimize number of comparisons in your ifs:
1
2
3
4
5
6
7
8
9
10
11
12
13
if (total >= 200 && total <= 299) 
{
	k = 0;	// if that's true, why bother checking all other ifs?
		// just return the k now. 
		// If it cant be returned yet due to some extra action at the end
		// use else if for all remaining checks
}
if (total >= 300	// <- Why checking this? 
                        //   k less than 300 should be returned before.
      && total <= 399)  // <- This one is enough
{
	k = 1;  // just return it
}

And now the big picture. How many times will you be able to add another score? 9? No. In fact it is max 2. Or 1 if user selects 'n' option the first time:
1
2
3
4
5
6
7
8
9
10
11
12
counting[GROSS()]++; 	// first chance
printf("\nIs there another salesperson 'yes' or 'no': ");

cin >>sale; // Only one chance to input y/n
if (sale == 'y')
{
	counting[GROSS()]++; //last chance, but if this is chosen...
}
if (sale == 'n') 		// ... this will never happen.
{
	// do the printing
}

This should be done in a loop, perhaps infinite while:
1
2
3
4
5
6
while(true)
    // ask for score
    // ask if continue
    // if yes - continue
    // if no - break;
    // if sth else: say "I assume you wish to continue" and continue 

Last edited on Nov 11, 2013 at 6:56pm
Nov 11, 2013 at 5:25pm
@JockX thank you so much ... you gave me a lot of great tips here ... I ended up fixing the program and getting it to work properly with all the great stuff you laid out here ... My biggest problem was definitely not looping the question being asked whether to continue or not ... Once again thank you
Topic archived. No new replies allowed.