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
|