Hi guys,

Ok so once again, I have pretty much the whole code written, but something is not working right! When I run it and select "add record", it works up until the user inputs price, then it just prints a ton of numbers and basically crashes. Please help me figure out what is wrong!

Here is the code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

// ** Prototypes **
void heading(void);
int menu();
int add();
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(double quantity, double amount);
double profit(double profit_cost, double profit_price);
double init_costs(double t[], int x);
double show_costs(double t[], int x);
void show(int prodnum, int prodtype, int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);
void message(char msg[]);


// ** Functions **
int main()
{
	int TRUE = 1;
	int FALSE = 0;
	int select, end = TRUE;

	do
	{

		select = menu();
		switch (select)
		{
		case 1: add(); break;
		case 2:
		case 3:
		case 4:
		case 5: end = quit(); break;
		default: message("\n\nPlease make a selection between 1 and 5.\a");
		}
	}

	while (end);
	return 0;
}
/*  ================================================================  */

int menu()
{
	int choice;
	system("cls");

	printf("Sierra Sporting Goods\n\n");
	printf("1  =  Add a record\n");
	printf("2  =  Report\n");
	printf("3  =  Delete a record\n");
	printf("4  =  Change a record\n");
	printf("5  =  Quit\n");
	choice = menuerror(1, 5, "Enter your selection ");

	return(choice);
}
/*  ================================================================  */

void heading()
{
	system("cls");
	printf("Sierra Sporting Goods\n\n\n");
	return;
}
/*  ================================================================  */

int add()
{
	double cost, price, prod_cost, prod_price, prod_profit;
	double MINCOST = 5.00;
	double MAXCOST = 900.00;
	double MINPRICE = 6.00;
	double types[6];
	double MAXPRICE = 1000.00;
	int prodnum, prodtype, prodquan;
	int MINPROD = 1;
	int MAXPROD = 9999;
	int MINTYPE = 1;
	int MAXTYPE = 5;
	int MINQUAN = 1;
	int MAXQUAN = 50;
	char choice;

	system("cls");

	init_costs(types, 6);
	do
	{
		heading();
		
		prodnum = getint(MINPROD, MAXPROD, "product number");
		prodtype = getint(MINTYPE, MAXTYPE, "product type");
		prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
		cost = getreal(MINCOST, MAXCOST, "product cost");

		types[prodtype] += cost;

		price = getreal(MINPRICE, MAXPRICE, "product price");
		prod_cost = total(prodquan, cost);
		prod_price = total(prodquan, price);
		prod_profit = profit(prod_price, prod_cost);
		show(prodnum, prodtype, prodquan, cost, price, prod_price, prod_cost, prod_profit);
		show_costs(types, 6);
		choice = prompt("Would you like to continue");
	}
	while (choice != 'N');
	return (choice);
}
/*  ================================================================  */

int getint(int min, int max, char item[])
{
	int x;
	printf("Enter a %s between %d and %d: ", item, min, max);
	scanf("%d%*c", &x);
	while (x < min || x > max)
	{
		message("\nError in range\a");
		printf("Enter a %s between %d and %d: ", item, min, max);
		scanf("%d%*c", &x);
	}
	return(x);
}
/*  ================================================================  */

double getreal(double min, double max, char item[])
{
	double y;

	printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
	scanf("%lf%*c", &y);
	while (y < min || y > max)
	{
		message("\nError in range.  Press Enter to continue.S\a");
		printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
		scanf("%lf%*c", &y);
	}
	return(y);
}
/*  ================================================================  */

double total(double quantity, double amount)
{
	return (quantity * amount);
}
/*  ================================================================  */

double profit(double profit_price, double profit_cost)
{
	return (profit_price - profit_cost);
}
/*  ================================================================  */

double init_costs(double t[], int x)
{
	int z;

	for (z = 1; z < x; z++)
		t[z] = 0;

return 0;
}
/*  ================================================================  */

double show_costs(double t[], int x)
{
	int z;
	double total = 0;

	printf("Product Cost by Type\n\n");
	printf("Type Cost\n\n");
		for (z = 1; z < x; t++)
		{
			printf("%d%lf\n", z, t[z]);
			total += t[z];
		}
		printf("%22.2lf\n", total);
		
		return 0;
}
/*  ================================================================  */
void message(char msg[])
{
	printf("%s\n\n", msg);
	getchar();
}
/*  ================================================================  */

int prompt(char msg[])
{
	int z;
	do
	{
		printf("%s (Y/N)? ", msg);
		z = toupper(getchar());
		if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
	}
	while (z != 'Y' && z != 'N');
	return(z);
}
/*  ================================================================  */

int quit()
{
	int TRUE = 1;
	int FALSE = 0;
	int z, end = TRUE;

	z = prompt("\nEnd program");
	if (z == 'Y')
	{
		system("cls");
		message("\nSierra Sporting Goods Database closed successfully.");
		end = FALSE;
	}
	return(end);
}
/*  ================================================================  */

int menuerror(int min, int max, char item[])
{
	int z;
	printf("%s from %d to %d: ", item, min, max);
	scanf("%d%*c", &z);
	while (z < min || z > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("%s from %d to %d: ", item, min, max);
		scanf("%d%*c", &z);
	}
	return(z);
}
/*  ================================================================  */
void show(int prodnum, int prodtype, int prodquan, double cost, double price, 
		  double prod_price, double prod_cost, double prod_profit)
{

	printf("\n\n\nThe product number is ---->  %04d\n", prodnum);
	printf("The product type is ------>  %d\n", prodtype);
	printf("The quantity is ---------->  %d\n", prodquan);
	printf("The cost is -------------->  $%.2lf\n", cost);
	printf("The price is ------------->  $%.2lf\n\n", price);

	printf("Total product price ------> $%.2lf\n", prod_price);
	printf("Total product cost -------> $%.2lf\n", prod_cost);
	printf("Total product profit -----> $%.2lf\n\n\n\n", prod_profit);

	return;
}
/*  ================================================================  */

Look at line 179. I am guessing it is crashing here.

for (z = 1; z < x; t++)

Do you want to increment t or z?

Yeah, I saw that and fixed it. Here is the code I have now... I run the program and it goes all the way through the first show() function, then at the continue prompt I press 'n' and it shows the show_costs table (which is completely not working btw), but it also shows the Menu in the same window again and I just want to have a "Press ENTER to continue" prompt after the show_costs table that will take the user back to the menu. Pleaaaase help me I'm running out of steam and I need to get it done or it's going to drive me crazy!

Here is the code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

// ** Prototypes **
void heading(void);
int menu();
int add();
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(double quantity, double amount);
double profit(double profit_cost, double profit_price);
void init_costs(double t[], int x);
void show_costs(double t[], int x);
void show(int prodnum, int prodtype, int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);
void message(char msg[]);


// ** Functions **
int main()
{
	int TRUE = 1;
	int FALSE = 0;
	int select, end = TRUE;

	do
	{

		select = menu();
		switch (select)
		{
		case 1: add(); break;
		case 2:
		case 3:
		case 4:
		case 5: end = quit(); break;
		default: message("\n\nPlease make a selection between 1 and 5.\a");
		}
	}

	while (end);
	return 0;
}
/*  ================================================================  */

int menu()
{
	int choice;

	printf("Sierra Sporting Goods\n\n");
	printf("1  =  Add a record\n");
	printf("2  =  Report\n");
	printf("3  =  Delete a record\n");
	printf("4  =  Change a record\n");
	printf("5  =  Quit\n\n");
	choice = menuerror(1, 5, "Enter your selection");

	return(choice);
}
/*  ================================================================  */

void heading()
{
	system("cls");
	printf("Sierra Sporting Goods\n\n\n");
	return;
}
/*  ================================================================  */

int add()
{
	double cost, price, prod_cost, prod_price, prod_profit;
	double MINCOST = 5.00;
	double MAXCOST = 900.00;
	double MINPRICE = 6.00;
	double MAXPRICE = 1000.00;
	int prodnum, prodtype, prodquan;
	int MINPROD = 1;
	int MAXPROD = 9999;
	int MINTYPE = 1;
	int MAXTYPE = 5;
	int MINQUAN = 1;
	int MAXQUAN = 50;
	double types[6];
	char choice;


	init_costs(types, 6);
	do
	{
		heading();

		prodnum = getint(MINPROD, MAXPROD, "product number");
		prodtype = getint(MINTYPE, MAXTYPE, "product type");
		prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
		cost = getreal(MINCOST, MAXCOST, "product cost");
		price = getreal(MINPRICE, MAXPRICE, "product price");

		types[prodtype] += cost;

		prod_cost = total(prodquan, cost);
		prod_price = total(prodquan, price);
		prod_profit = profit(prod_price, prod_cost);
		show(prodnum, prodtype, prodquan, cost, price, prod_price, prod_cost, prod_profit);
		choice = prompt("Would you like to continue");
	}
	while (choice != 'N');
	getchar();
		show_costs(types, 6);


	
	return 0;
}
/*  ================================================================  */

int getint(int min, int max, char item[])
{
	int x;
	printf("Enter a %s between %d and %d: ", item, min, max);
	scanf("%d%*c", &x);
	while (x < min || x > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("Enter a %s between %d and %d: ", item, min, max);
		scanf("%d%*c", &x);
	}
	return(x);
}
/*  ================================================================  */

double getreal(double min, double max, char item[])
{
	double y;

	printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
	scanf("%lf%*c", &y);
	while (y < min || y > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
		scanf("%lf%*c", &y);
	}
	return(y);
}
/*  ================================================================  */

double total(double quantity, double amount)
{
	return (quantity * amount);
}
/*  ================================================================  */

double profit(double profit_price, double profit_cost)
{
	return (profit_price - profit_cost);
}
/*  ================================================================  */

void init_costs(double t[], int x)
{
	int z;

	for (z = 1; z < x; z++)
		t[z] = 0;

}
/*  ================================================================  */

void show_costs(double t[], int x)
{
	int z;
	double total = 0;

	system("cls");

	printf("Product Cost by Type\n\n");
	printf("Type Cost\n\n");
	for (z = 1; z < x; z++)
	{
		printf("%2d%20.2lf\n", t[z], z);
		total += t[z];
	}
	printf("%22.2lf\n\n", total);
	printf("Press Enter to continue.\a");

}
/*  ================================================================  */
void message(char msg[])
{
	printf("%s\n\n", msg);
}
/*  ================================================================  */

int prompt(char msg[])
{
	int z;
	do
	{
		printf("%s (Y/N)? ", msg);
		z = toupper(getchar());
		if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
	}
	while (z != 'Y' && z != 'N');
	return(z);
}
/*  ================================================================  */

int quit()
{
	int TRUE = 1;
	int FALSE = 0;
	int z, end = TRUE;

	z = prompt("\nEnd program");
	if (z == 'Y')
	{
		system("cls");
		message("\nSierra Sporting Goods Database closed successfully.");
		end = FALSE;
	}
	return(end);
}
/*  ================================================================  */

int menuerror(int min, int max, char item[])
{
	int z;
	printf("%s from %d to %d: ", item, min, max);
	scanf("%d%*c", &z);
	while (z < min || z > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("%s from %d to %d: ", item, min, max);
		scanf("%d%*c", &z);
	}
	return(z);
}
/*  ================================================================  */
void show(int prodnum, int prodtype, int prodquan, double cost, double price, 
		  double prod_price, double prod_cost, double prod_profit)
{

	printf("\n\n\nThe product number is ---->  %04d\n", prodnum);
	printf("The product type is ------>  %d\n", prodtype);
	printf("The quantity is ---------->  %d\n", prodquan);
	printf("The cost is -------------->  $%.2lf\n", cost);
	printf("The price is ------------->  $%.2lf\n\n", price);

	printf("Total product price ------> $%.2lf\n", prod_price);
	printf("Total product cost -------> $%.2lf\n", prod_cost);
	printf("Total product profit -----> $%.2lf\n\n\n\n", prod_profit);

	return;
}
/*  ================================================================  */

line 189: add getchar() to make the program stop and wait for input.

Alright, I got it! Thanks a lot for your help, guys!

Here is the working code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

// ** Prototypes **
void heading(void);
int menu();
int add();
int getint(int min, int max, char prompt[]);
double getreal(double min, double max, char prompt[]);
double total(double quantity, double amount);
double profit(double profit_cost, double profit_price);
void init_costs(double t[], int x);
void show_costs(double t[], int x);
void show(int prodnum, int prodtype, int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
int menuerror(int min, int max, char prompt[]);
int prompt(char msg[]);
int quit(void);
void message(char msg[]);


// ** Functions **
int main()
{
	int TRUE = 1;
	int FALSE = 0;
	int select, end = TRUE;

	do
	{

		select = menu();
		switch (select)
		{
		case 1: add(); break;
		case 2:
		case 3:
		case 4:
		case 5: end = quit(); break;
		default: message("\n\nPlease make a selection between 1 and 5.\a");
		}
	}

	while (end);
	return 0;
}
/*  ================================================================  */

int menu()
{
	int choice;

	system("cls");

	printf("Sierra Sporting Goods\n\n");
	printf("1  =  Add a record\n");
	printf("2  =  Report\n");
	printf("3  =  Delete a record\n");
	printf("4  =  Change a record\n");
	printf("5  =  Quit\n\n");
	choice = menuerror(1, 5, "Enter your selection");

	return(choice);
}
/*  ================================================================  */

void heading()
{
	system("cls");
	printf("Sierra Sporting Goods\n\n\n");
	return;
}
/*  ================================================================  */

int add()
{
	double cost, price, prod_cost, prod_price, prod_profit;
	double MINCOST = 5.00;
	double MAXCOST = 900.00;
	double MINPRICE = 6.00;
	double MAXPRICE = 1000.00;
	int prodnum, prodtype, prodquan;
	int MINPROD = 1;
	int MAXPROD = 9999;
	int MINTYPE = 1;
	int MAXTYPE = 5;
	int MINQUAN = 1;
	int MAXQUAN = 50;
	double types[6];
	char choice;


	init_costs(types,6);
	do
	{
		heading();

		prodnum = getint(MINPROD, MAXPROD, "product number");
		prodtype = getint(MINTYPE, MAXTYPE, "product type");
		prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
		cost = getreal(MINCOST, MAXCOST, "product cost");
		price = getreal(MINPRICE, MAXPRICE, "product price");
		prod_cost = total(prodquan, cost);
		prod_price = total(prodquan, price);
		prod_profit = profit(prod_price, prod_cost);

		types[prodtype] += prod_cost;

		show(prodnum, prodtype, prodquan, cost, price, prod_price, prod_cost, prod_profit);
		choice = prompt("Would you like to enter another product");
	}
	while (choice != 'N');
	getchar();
		show_costs(types,6);


	
	return 0;
}
/*  ================================================================  */

int getint(int min, int max, char item[])
{
	int x;
	printf("Enter a %s between %d and %d: ", item, min, max);
	scanf("%d%*c", &x);
	while (x < min || x > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("Enter a %s between %d and %d: ", item, min, max);
		scanf("%d%*c", &x);
	}
	return(x);
}
/*  ================================================================  */

double getreal(double min, double max, char item[])
{
	double y;

	printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
	scanf("%lf%*c", &y);
	while (y < min || y > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
		scanf("%lf%*c", &y);
	}
	return(y);
}
/*  ================================================================  */

double total(double quantity, double amount)
{
	return (quantity * amount);
}
/*  ================================================================  */

double profit(double profit_price, double profit_cost)
{
	return (profit_price - profit_cost);
}
/*  ================================================================  */

void init_costs(double t[], int x)
{
	int z;

	for (z = 1; z < x; z++)
		t[z] = 0;

}
/*  ================================================================  */

void show_costs(double t[], int x)
{
	int z;
	double total = 0;

	system("cls");

	printf(" Product Cost by Type\n\n");
	printf("_Type_____________Cost_\n\n");
	for (z = 1; z < x; z++)
	{
		printf(" %2d%20.2lf\n", z, t[z]);
		total += t[z];
	}
	printf("________________________\n");
	printf("%23.2lf\n\n\n\n", total);
	printf("Press ENTER to return to the Main Menu...");
	getchar();


}
/*  ================================================================  */
void message(char msg[])
{
	printf("%s\n\n", msg);
}
/*  ================================================================  */

int prompt(char msg[])
{
	int z;
	do
	{
		printf("%s (Y/N)? ", msg);
		z = toupper(getchar());
		if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
	}
	while (z != 'Y' && z != 'N');
	return(z);
}
/*  ================================================================  */

int quit()
{
	int TRUE = 1;
	int FALSE = 0;
	int z, end = TRUE;

	z = prompt("\nEnd program\a");
	if (z == 'Y')
	{
		system("cls");
		message("\nSierra Sporting Goods Database closed successfully.");
		end = FALSE;
	}
	return(end);
}
/*  ================================================================  */

int menuerror(int min, int max, char item[])
{
	int z;
	printf("%s from %d to %d: ", item, min, max);
	scanf("%d%*c", &z);
	while (z < min || z > max)
	{
		message("\nError in range.  Press Enter to continue.\a");
		printf("%s from %d to %d: ", item, min, max);
		scanf("%d%*c", &z);
	}
	return(z);
}
/*  ================================================================  */
void show(int prodnum, int prodtype, int prodquan, double cost, double price, 
		  double prod_price, double prod_cost, double prod_profit)
{

	printf("\n\n\nThe product number is ---->  %04d\n", prodnum);
	printf("The product type is ------>  %d\n", prodtype);
	printf("The quantity is ---------->  %d\n", prodquan);
	printf("The cost is -------------->  $%.2lf\n", cost);
	printf("The price is ------------->  $%.2lf\n\n", price);

	printf("Total product price ------> $%.2lf\n", prod_price);
	printf("Total product cost -------> $%.2lf\n", prod_cost);
	printf("Total product profit -----> $%.2lf\n\n\n\n", prod_profit);

	return;
}
/*  ================================================================  */

>>it isn't giving the total cost (cost * prodquan), just the cost that the user entered. I'm so close!

what function? [edit]nevermind you already told me that :) [/edit]

line 186: you didn't tell the program to print that information. You have to add it to your program.

Please explain this line: scanf("%lf%*c", &y); It makes no sense with the format specifier you have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.