I am a novice to C programming, and a program I am writing for school is crashing when attempting to total all values in table, and I cannot figure out what I am doing wrong in the void Totals(void) function. Any suggestions would be great.
Code:
/* The name of this program is Week 4 Classroom */
/* This program prints out sales information, number of sales persons, products sold, and amount of money */
#include <stdio.h>
/* Preload array with values */
int sales[7][6] = {{0, 1, 2, 3, 4}, {1}, {2}, {3}, {4}, {5}};
/* Define Functions */
/* print out blank table */
void print_table(void)
{
int r, c; /* r = row and c = column */
for(r = 0; r < 7; ++r)
{
for(c = 0; c < 6; ++c)
printf("%5i", sales[r][c]); /* %5i makes column 5 characters wide */
printf("\n");
}
}
/* zeros out the table */
void zero_table(void)
{
int r, c; /* r = row and c = column */
for(r = 1; r < 7; ++r)
{
for(c = 1; c < 6; ++c)
sales[r][c] = 0; /* all values set to zero */
}
}
int get_information(int number, int low, int high) /* number is salesperson or product number */
{
int a;
do
{
printf("Enter");
switch(number) /* no semi colon after switch statement */
{
case 1: printf("Salesperson");
break;
case 2: printf("Product");
break;
case 3: printf("Amount");
break;
}
scanf("%i", &a);
if(a < low || a > high)
printf("Invalid entry... please try again \n");
}
while(a < low || a > high);
return(a);
}
void enter_all_daily_sales(void)
{
int done;
int salesperson;
int product;
int amount;
do
{
salesperson = get_information (1, 1, 4);
product = get_information (2, 1, 5);
amount = get_information (3, 0, 9999999);
sales[product][salesperson] = sales[product][salesperson] + amount;
printf("Are you done? 1 = Yes 0 = No \n");
scanf("%i", &done);
}
while(! done);
}
void totals(void)
{
int c; /* c = columns */
int rows;
for(rows = 1; c < 6; ++rows)
for(c = 1; c < 5; ++c)
sales[rows][5] = sales[rows][5] + sales[rows][c];
for(c = 1; c < 5; ++c)
for(rows = 1; rows < 6; ++rows);
sales[6][c] = sales[6][c] + sales[6][rows];
}
/* Declare Global Variables */
int main(void)
{
print_table();
printf("\n\n\n\n");
zero_table();
print_table();
printf("\n\n\n\n");
enter_all_daily_sales();
print_table();
printf("\n\n\n\n");
totals();
print_table();
printf("\n\n\n\n");
}