-1

I am trying to print the final results of the calculations using my program and put it into a clean organized table for the output, however, when the table is printed it doesn't print properly and I am very lost and my professor isn't really much help. Any help is greatly appreciated thank you!

The table is meant to look like this:

Example Table

#include <stdio.h>

void main(void) {
  float sale[10], pct[10], commission[10];
  char emp_name[10][20], company[15];
  float sum = 0;

  char c;
  int i, x, sales_persons, num_of_sales;

  printf("Enter your company name: ");
  scanf("%s", company);

  printf("\nWelcome to the %s Sales Analysis\n", company);

  do {
    printf("\nEnter how many sales persons from 1 to 10: ");
    scanf("%i", &sales_persons);
    while ((c = getchar() != '\n') && c != EOF)
      ; /* clear input buffer */

    if (sales_persons < 0 || sales_persons > 10)
      printf("Number of sales persons must be 1-10...\n\n");

  } while (sales_persons < 0 || sales_persons > 10);

  for (x = 0; x < sales_persons; x++) {
    printf("\nEnter the name of sales person #%i: ", x + 1);
    scanf("%19[^\n]", emp_name[x]);
    while ((c = getchar() != '\n') && c != EOF)
      ;

    do {
      printf("Enter the number of sales for %s (1 - 20): ", emp_name[x]);
      scanf("%i", &num_of_sales);

      while ((c = getchar() != '\n') && c != EOF)
        ; /* clear input buffer */

      if (num_of_sales < 0 || num_of_sales > 20)
        printf("Number of sales must be 1-20...\n\n");

    } while (num_of_sales < 0 || num_of_sales > 20);

    do {
      printf("Enter the commission percentage for %s (1-50): ", emp_name[x]);
      scanf("%f", &pct[x]);
      while ((c = getchar() != '\n') && c != EOF)
        ; /* clear input buffer */

      if (pct[x] < 0 || pct[x] > 50) printf("Percent must be 0-50...\n\n");

    } while (pct[x] < 0 || pct[x] > 50);

    printf("\n");

    for (x = 0; x < num_of_sales; x++) {
      do {
        printf("  Sale #%i for %s: ", x + 1, emp_name[x]);
        scanf("%f", &sale[x]);
        while ((c = getchar() != '\n') && c != EOF)
          ; /* clear input buffer */

        if (sale[x] < 0) printf("All sales must be greater than 0...\n\n");

      } while (sale[x] < 0);
    }

    sum = sum + sale[x];
    commission[x] = pct[x] * sum / 100;
  }

  printf("\n\n\n    *** %s Sales Commission Report ***", company);
  printf("\n\n\nName \t\t Sales \t\t Commission");
  printf("\n---- \t\t ----- \t\t ----------");

  for (x = 0; x < sales_persons; x++) {
    printf("\n%s \t\t %.2f\n \t\t%.2f", emp_name[x], sum, commission[x]);
  }
  getchar();
}
  • 2
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Michael M. Oct 21 '22 at 23:01
  • 3
    **_Strongly_** recommend you use an array of structs to aggregate your data rather than parallel arrays. That way lies madness. – Chris Oct 21 '22 at 23:05
  • @Chris Unfortunately im not allowed to as we havent learned that yet and would get points off If I used that – Mark Parisse Oct 21 '22 at 23:53
  • You collect up to **20** "sales" amounts into an array dimensioned for **10** items... Time to carefully examine what it is you are accumulating to print in the final loop... – Fe2O3 Oct 22 '22 at 02:21
  • @Fe2O3 I know thats outta place, theres a lot out of place because my professor is not the brightest but if i tell him hes wrong he marks me down lol. He told us to make the error condition 10 but the print 20 lol – Mark Parisse Oct 22 '22 at 02:47

1 Answers1

1

You are using the same x variable to control nested loops

for (x = 0; x < sales_persons; x++) {
    /* ... */
    for (x = 0; x < num_of_sales; x++) {

meaning the line

printf("  Sale #%i for %s: ", x + 1, emp_name[x]);

will access incorrect elements of emp_name, and the value of x will be num_of_sales + 1 after every outer loop.

Similarly,

sum = sum + sale[x];
commission[x] = pct[x] * sum / 100;

will be working with the wrong value of x after the inner loop completes.

Perhaps the unused variable i should be substituted for x in the inner loop?

Additionally, this format string has a stray newline in it, making the output of the program confusing:

printf("\n%s \t\t %.2f\n \t\t%.2f", emp_name[x], sum, commission[x]);
                   /* ^^^ excess newline character */

Other problems:

  • void main(void) should be int main(void).
  • char c; should be int c;. See here.
  • scanf("%s", company); is inviting undefined behaviour by not limiting the size of the input string ("%14s").
  • Newline characters should ideally be written at the end of lines.
  • A lot of repeated code should be placed in separate functions (clearing the input buffer, for example).
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Okay so I changed every x in the inner loop to i and im still getting the same error as well as changing to int main (void) also changed sum = sum + sale[x]; commission[x] = pct[x] * sum / 100; to all i's as well im kinda lost lol – Mark Parisse Oct 22 '22 at 01:47
  • That's a lot of debugging right there. If you're trying to just get the numbers to line up, [man printf](https://pubs.opengroup.org/onlinepubs/7908799/xsh/fprintf.html) will probably help. – Neil Oct 22 '22 at 04:17
  • `sum = ...` and `commission[x] = ...` are in the block of the outer loop, and should not use the value of `i`. – Oka Oct 22 '22 at 04:27