Hi guys,

Okay so this time I need help with string input. I have the whole program (yet again) but I just need to figure out how to change letters of user input to redisplay them. The code is below and it asks the user to input the product description. I would like to take the product description and capitalize the first letter of each word they type in, so I can redisplay it that way in the summary. The only thing is that I'm not allowed to use the strupr() function... I have to write my own... Otherwise I would have this done! Please let me know if you can help!

// ** 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[]);
/*================================================================== */

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

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

// ** 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");
		
		getstring(prodscrip, "product description");
		
		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 getstring(char entry[], char prompt[])
{
	printf("Enter the %s:", prompt);
	gets(entry);
}
/*  ================================================================  */

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, 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;
}
/*  ================================================================  */

Recommended Answers

All 13 Replies

>>capitalize the first letter of each word they type in,
One way to do it would be to use strtok() to find each work then toupper() to change the first character to upper case. strtok() modifies the original string so you will want to make a copy of the string before doing anything with it.

commented: Very helpful! +1

strtok will work to be sure, but you have to watch out that it will stomp all over your original string.

another way is to use pointers. make sure you can understand, and explain, how this works

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

Ok that does make sense, but now the only thing that doesn't make sense to me is where I'm supposed to call that function to change the case of each word... Here's what I have:

// ** 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);

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

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

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

// ** 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");
		
		getstring(prodscrip, "product description");
		
		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 getstring(char entry[], char prompt[])
{
	int count;
	char prodscrip[40];

	printf("Enter the %s:", prompt);
	count = scanf("%40s", prodscrip);
	firstUpper(entry);
	return;
}
/*  ================================================================  */

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 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, 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;
}
/*  ================================================================  */

call it from anywhere you want to.

if you have any string, lets call it "myString", and it has a bunch of text in it, when you make this call

captializeFirsts(myString);

after the function comes back, all the first characters in each word within "myString" will be capitalized.

if there's only one word in "myString", only the first letter of that word will be capitalized. if theres a hundred word, each of the hundred words will be capitalized.

Hummmm. Seems your function needs a tad bit more work. But its close.

int main()
{
    char str[] = "now \tis     the   time  for   all    good    men\n";
    capitalizeFirsts(str);
    puts(str);
    return 0;
}

Results

Now     is     The   Time  for   All    good    men

Press any key to continue . . .

Alright, I'm still totally confused. I don't know where that is supposed to go since it is getting the string from user input during multiple functions. Here is what I have and every time I run it as soon as I enter the description it asks for the quantity then automatically says I entered an incorrect quantity, and it just keeps repeating that over and over.

// ** 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);

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

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

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

// ** 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");
		
		getstring(prodscrip, "product description");
		firstUpper(prodscrip);
		puts(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 getstring(char entry[], char prompt[])
{
	int count;
	char prodscrip[40];

	printf("Enter the %s:", prompt);
	count = scanf("%40s", prodscrip);
	return;
}
/*  ================================================================  */

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 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, 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;
}
/*  ================================================================  */

your "getstring" function is buggering things up. your have a redeclaration for prodscrip that is local to that function, so when it returns back to "main( )" there's nothing for it to use, and you overrun the buffer.

"fprint" and "gets" work nice enough, i dont see what the benefit is for a separate function anyhow. you should always avoid scanf() whenever possible.

from your main( ) routine, remove the line

getstring(prodscrip,"product description");

and replace it with

printf("Enter the product description : ");
gets(prodscrip);

that will work.

I add that but then it still doesn't capitalize the first letter of each word inputted. What am I missing? I think I have to call the firstUpper() function after the gets(prodscrip) part, right?

// ** 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);

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

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

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

// ** 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("product description : ");
		gets(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 getstring(char entry[], char prompt[])
{
	int description;
	char prodscrip[40];

	printf("Enter the %s:", prompt);
	description = scanf("%40s", prodscrip);
	return;
}
/*  ================================================================  */

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 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, 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;
}
/*  ================================================================  */

your program, as posted in #7, with the changes i call in #8, is working just fine for me.

other than the fact i had to comment out #include lab7.h ... im using exactly what you posted.

EDIT:

i see what you did. in your latest program you removed the lines:

firstUpper(prodscrip);
puts(prodscrip);

just curious, how much help did you have writing the rest of the program? did you write it yourself?

Hummmm. Seems your function needs a tad bit more work. But its close.

int main()
{
    char str[] = "now \tis     the   time  for   all    good    men\n";
    capitalizeFirsts(str);
    puts(str);
    return 0;
}

Results

Now     is     The   Time  for   All    good    men

Press any key to continue . . .

good catch :P

i only planned for single spaces between words. as it happens, the function handles odd number of spaces, but not even number.

change the function to this, and it should be nearly bulletproof:

void capitalizeAll(char *buf)
{
	int capNext = 0;

	*buf=toupper(*buf);
	while (*buf++ != '\0')
	{
		if (capNext)
		{
			*buf=toupper(*buf);
			capNext = 0;
		}
		if (*buf <= ' ')
			capNext=1;
	}
}

Sorry, I should have let you know I actually got it riiiight after I posted last time. Thanks a lot for your help!

As far as writing the program goes, it's for my C Programming Class and it has been written and modified over the course of 6 lab assignments. I wrote it all myself, but each week I usually have some small problem and I put it on here to get some help. Other than that, I've gotten no other help.

Another version

void capitalizeFirsts(char *buf)
{
    while( *buf )
    {
        while( isspace(*buf) )
            buf++;
        *buf = toupper(*buf);
        while(*buf && !isspace(*buf))
            buf++;
    }
}

^ that works better than my first attempt, and much more elegant than my second.

nice job

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.