Hi guys,

Ok so I need to use pointer syntax for the functions init-costs(), show-costs() and toUpper() in the following code. I'm still working on the first two, but I'm curious as to what needs to be done to the toUpper() function. I'm pretty sure I've already done the pointer syntax correctly, right? Here is the code:

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


/* ================================================================== */

// ** Prototypes **
void heading(void);
int menu();
int add();
void getstring(char entry[], char prompt[]);
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, char d[], 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[]);
void firstUpper(char *buf);

// ** 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;
	char prodscrip[40];
	double types[6];
	char choice;


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

		prodnum = getint(MINPROD, MAXPROD, "product number");
		prodtype = getint(MINTYPE, MAXTYPE, "product type");
		
		printf("Enter the product description: ");
		gets(prodscrip);
		firstUpper(prodscrip);
		
		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, prodscrip, 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;
}
/*  ================================================================  */
void firstUpper(char *buf)
{
  *buf=toupper(*buf);    // first char CAP    
  while (*buf++ != '\0')        
	  if (*buf == ' ')   // look for space            
  *buf=toupper(*++buf);  // CAP next char
  }


/*  ================================================================  */

int getint(int min, int max, char item[])
{
	int x;
	printf("Enter the %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 the %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 the %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 the %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, char prodscrip[], 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 product description is ----->  %s\n", prodscrip);
	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;
}
/*  ================================================================  */

I don't see any function definition for toUpper(). But I see a function named firstUpper(). Is that the one?

void firstUpper(char *buf)
{
    *buf=toupper(*buf); // first char CAP
    while (*buf++ != '\0')
        if (*buf == ' ') // look for space
    *buf=toupper(*++buf); // CAP next char
}

I hope you have seen that *buf = toupper( *++buf ); // CAP next char belongs to the if statement block. Furthermore, how do you know that the next character will be a char that can be converted to uppercase?. Maybe is another space, or ", etc..

The itineration you are using with pointer arithmetic is buggy.
Run this test to understand:

char *text = "Hello";
int i = 0;

while( *text++ != '\0' )
    printf( "%c at loop %d\n", *text, i++ );

>>toUpper()
You don't have a function by that name in the code you posted.

I meant firstUpper().

make your "firstUpper( )" function look like this. this is basically what Ancient Dragon suggested from the previous thread.

void firstUpper(char *buf)
{
    while (*buf)  // continue as long as characters avail
    {
        while(*buf == ' ')
            *buf++;         // skip all spaces until next char
        *buf = toupper(*buf);   // cap first char after space
        while(*buf && *buf != ' ')
            *buf++;         // skip all chars until next space
    }
}

you're using my original suggestion from that thread which, unfortunately, was flawed.

sorry for the confusion.

this is basically what Ancient Dragon suggested from the previous thread.

you're using my original suggestion from that thread which, unfortunately, was flawed.

Yes, I think I see the bug in it. As I recall Narue pointed it out in another thread. Even this might not work 100% of the time.

void firstUpper(char *buf)
{
    while (*buf)  // continue as long as characters avail
    {
        while( isspace(*buf) || !isalpha(*buf) )
            *buf++;         // skip all spaces until next char
        *buf = toupper(*buf);   // cap first char after space
        while(*buf && !isspace(*buf) )
            *buf++;         // skip all chars until next space
    }
}

Hey guys, thanks for your help. Sorry for taking so long to respond... Been very sick this week! So here is what I have for the pointer syntax on those two functions. Let me know if you see anything I'm doing wrong!

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();


}

>>Let me know if you see anything I'm doing wrong!
If you test it then you will see for yourself if you did anything wrong. You don't need to wait for us to be your eyes.

Well I just meant as far as syntax goes. It all worked for me but I'm having a hard time understanding the concept of pointer syntax because I feel like it was too easy!

both of the functions you've posted are completely wrong and irretrievably broken.

throw them completely away, you can't even fix it.

re-read the chapter called "intro to pointers" or whatever.

okay ... on second thought, my reply was unnecessarily terse.

you've got the basic idea about how pointers work, but you're missing some fundamental bits.

your functions arent that bad, really. just you cant go playing with pointer arithmetic like you're trying to do. you cant add integers to pointers to type doubles. if it works, its just because youre lucky.

okay on third thought.... im an arsehole, and there's nothing wrong with what you did.

and i'm gonna shut up now.

yeah... it did work for me... is it actually wrong or no? And thanks for at least correcting your unnecessarily terse response ;)

I thought it was wrong, but I was wrong. it was late and i wasnt reading very carefully. sorry :)

those two functions that you're asking about, they seem to be okay. im sure someone will say it can be done better (someone always does) but I won't be that guy this time. :-D

as for the rest of your program, i think its a good first attempt at capturing the idea of the project. i like how you've tried to modularize all your functions.

thought there is still some room for improvements.

for one thing, your data gets erased every time you try a new operation. likely because your data is being declared and stored local to the add function rather than to main.

now you need to work out the bugginess in the interfacing between the functions. I'd start by declaring all of your inventory data in main() -- preferrably as a struct -- and then pass a pointer to that data into the functions that will modify it.

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.