Capitalize letter in string to output... Should be a pretty easy solution!

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 59
Reputation: nelledawg is an unknown quantity at this point 
Solved Threads: 0
nelledawg nelledawg is offline Offline
Junior Poster in Training

Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #1
Mar 14th, 2008
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!

  1. // ** Prototypes **
  2. void heading(void);
  3. int menu();
  4. int add();
  5. void getstring(char entry[], char prompt[]);
  6. int getint(int min, int max, char prompt[]);
  7. double getreal(double min, double max, char prompt[]);
  8. double total(double quantity, double amount);
  9. double profit(double profit_cost, double profit_price);
  10. void init_costs(double t[], int x);
  11. void show_costs(double t[], int x);
  12. void show(int prodnum, int prodtype, char d[], int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
  13. int menuerror(int min, int max, char prompt[]);
  14. int prompt(char msg[]);
  15. int quit(void);
  16. void message(char msg[]);
  17. /*================================================================== */
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. #include <string.h>
  23. #include "Lab7.h"
  24.  
  25. /* ================================================================== */
  26.  
  27. // ** Functions **
  28.  
  29. int main()
  30. {
  31. int TRUE = 1;
  32. int FALSE = 0;
  33. int select, end = TRUE;
  34.  
  35. do
  36. {
  37.  
  38. select = menu();
  39. switch (select)
  40. {
  41. case 1: add(); break;
  42. case 2:
  43. case 3:
  44. case 4:
  45. case 5: end = quit(); break;
  46. default: message("\n\nPlease make a selection between 1 and 5.\a");
  47. }
  48. }
  49.  
  50. while (end);
  51. return 0;
  52. }
  53. /* ================================================================ */
  54.  
  55. int menu()
  56. {
  57. int choice;
  58.  
  59. system("cls");
  60.  
  61. printf("Sierra Sporting Goods\n\n");
  62. printf("1 = Add a record\n");
  63. printf("2 = Report\n");
  64. printf("3 = Delete a record\n");
  65. printf("4 = Change a record\n");
  66. printf("5 = Quit\n\n");
  67. choice = menuerror(1, 5, "Enter your selection");
  68.  
  69. return(choice);
  70. }
  71. /* ================================================================ */
  72.  
  73. void heading()
  74. {
  75. system("cls");
  76. printf("Sierra Sporting Goods\n\n\n");
  77. return;
  78. }
  79. /* ================================================================ */
  80.  
  81. int add()
  82. {
  83. double cost, price, prod_cost, prod_price, prod_profit;
  84. double MINCOST = 5.00;
  85. double MAXCOST = 900.00;
  86. double MINPRICE = 6.00;
  87. double MAXPRICE = 1000.00;
  88. int prodnum, prodtype, prodquan;
  89. int MINPROD = 1;
  90. int MAXPROD = 9999;
  91. int MINTYPE = 1;
  92. int MAXTYPE = 5;
  93. int MINQUAN = 1;
  94. int MAXQUAN = 50;
  95. char prodscrip[40];
  96. double types[6];
  97. char choice;
  98.  
  99.  
  100. init_costs(types,6);
  101. do
  102. {
  103. heading();
  104.  
  105. prodnum = getint(MINPROD, MAXPROD, "product number");
  106. prodtype = getint(MINTYPE, MAXTYPE, "product type");
  107.  
  108. getstring(prodscrip, "product description");
  109.  
  110. prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
  111. cost = getreal(MINCOST, MAXCOST, "product cost");
  112. price = getreal(MINPRICE, MAXPRICE, "product price");
  113. prod_cost = total(prodquan, cost);
  114. prod_price = total(prodquan, price);
  115. prod_profit = profit(prod_price, prod_cost);
  116.  
  117. types[prodtype] += prod_cost;
  118.  
  119. show(prodnum, prodtype, prodscrip, prodquan, cost, price, prod_price, prod_cost, prod_profit);
  120. choice = prompt("Would you like to enter another product");
  121. }
  122. while (choice != 'N');
  123. getchar();
  124. show_costs(types,6);
  125.  
  126.  
  127.  
  128. return 0;
  129. }
  130. /* ================================================================ */
  131. void getstring(char entry[], char prompt[])
  132. {
  133. printf("Enter the %s:", prompt);
  134. gets(entry);
  135. }
  136. /* ================================================================ */
  137.  
  138. int getint(int min, int max, char item[])
  139. {
  140. int x;
  141. printf("Enter a %s between %d and %d: ", item, min, max);
  142. scanf("%d%*c", &x);
  143. while (x < min || x > max)
  144. {
  145. message("\nError in range. Press Enter to continue.\a");
  146. printf("Enter a %s between %d and %d: ", item, min, max);
  147. scanf("%d%*c", &x);
  148. }
  149. return(x);
  150. }
  151. /* ================================================================ */
  152.  
  153. double getreal(double min, double max, char item[])
  154. {
  155. double y;
  156.  
  157. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  158. scanf("%lf%*c", &y);
  159. while (y < min || y > max)
  160. {
  161. message("\nError in range. Press Enter to continue.\a");
  162. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  163. scanf("%lf%*c", &y);
  164. }
  165. return(y);
  166. }
  167. /* ================================================================ */
  168.  
  169. double total(double quantity, double amount)
  170. {
  171. return (quantity * amount);
  172. }
  173. /* ================================================================ */
  174.  
  175. double profit(double profit_price, double profit_cost)
  176. {
  177. return (profit_price - profit_cost);
  178. }
  179. /* ================================================================ */
  180.  
  181. void init_costs(double t[], int x)
  182. {
  183. int z;
  184.  
  185. for (z = 1; z < x; z++)
  186. t[z] = 0;
  187.  
  188. }
  189. /* ================================================================ */
  190.  
  191. void show_costs(double t[], int x)
  192. {
  193. int z;
  194. double total = 0;
  195.  
  196. system("cls");
  197.  
  198. printf(" Product Cost by Type\n\n");
  199. printf("_Type_____________Cost_\n\n");
  200. for (z = 1; z < x; z++)
  201. {
  202. printf(" %2d%20.2lf\n", z, t[z]);
  203. total += t[z];
  204. }
  205. printf("________________________\n");
  206. printf("%23.2lf\n\n\n\n", total);
  207. printf("Press ENTER to return to the Main Menu...");
  208. getchar();
  209.  
  210.  
  211. }
  212. /* ================================================================ */
  213. void message(char msg[])
  214. {
  215. printf("%s\n\n", msg);
  216. }
  217. /* ================================================================ */
  218.  
  219. int prompt(char msg[])
  220. {
  221. int z;
  222. do
  223. {
  224. printf("%s (Y/N)? ", msg);
  225. z = toupper(getchar());
  226. if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
  227. }
  228. while (z != 'Y' && z != 'N');
  229. return(z);
  230. }
  231. /* ================================================================ */
  232.  
  233. int quit()
  234. {
  235. int TRUE = 1;
  236. int FALSE = 0;
  237. int z, end = TRUE;
  238.  
  239. z = prompt("\nEnd program\a");
  240. if (z == 'Y')
  241. {
  242. system("cls");
  243. message("\nSierra Sporting Goods Database closed successfully.");
  244. end = FALSE;
  245. }
  246. return(end);
  247. }
  248. /* ================================================================ */
  249.  
  250. int menuerror(int min, int max, char item[])
  251. {
  252. int z;
  253. printf("%s from %d to %d: ", item, min, max);
  254. scanf("%d%*c", &z);
  255. while (z < min || z > max)
  256. {
  257. message("\nError in range. Press Enter to continue.\a");
  258. printf("%s from %d to %d: ", item, min, max);
  259. scanf("%d%*c", &z);
  260. }
  261. return(z);
  262. }
  263. /* ================================================================ */
  264. void show(int prodnum, int prodtype, char prodscrip[], int prodquan, double cost, double price,
  265. double prod_price, double prod_cost, double prod_profit)
  266. {
  267.  
  268. printf("\n\n\nThe product number is ----------> %04d\n", prodnum);
  269. printf("The product type is ------------> %d\n", prodtype);
  270. printf("The product description is -----> %s\n", prodscrip);
  271. printf("The quantity is ----------------> %d\n", prodquan);
  272. printf("The cost is --------------------> $%.2lf\n", cost);
  273. printf("The price is -------------------> $%.2lf\n\n", price);
  274.  
  275. printf("Total product price ----------> $%.2lf\n", prod_price);
  276. printf("Total product cost -----------> $%.2lf\n", prod_cost);
  277. printf("Total product profit ---------> $%.2lf\n\n\n\n", prod_profit);
  278.  
  279. return;
  280. }
  281. /* ================================================================ */
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #2
Mar 14th, 2008
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
1
  #3
Mar 14th, 2008
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

  1. void capitalizeFirsts(char *buf)
  2. {
  3. *buf=toupper(*buf); // first char CAP
  4. while (*buf++ != '\0')
  5. if (*buf == ' ') // look for space
  6. *buf=toupper(*++buf); // CAP next char
  7. }
Last edited by jephthah; Mar 14th, 2008 at 2:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 59
Reputation: nelledawg is an unknown quantity at this point 
Solved Threads: 0
nelledawg nelledawg is offline Offline
Junior Poster in Training

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #4
Mar 14th, 2008
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:

  1.  
  2. // ** Prototypes **
  3. void heading(void);
  4. int menu();
  5. int add();
  6. void getstring(char entry[], char prompt[]);
  7. int getint(int min, int max, char prompt[]);
  8. double getreal(double min, double max, char prompt[]);
  9. double total(double quantity, double amount);
  10. double profit(double profit_cost, double profit_price);
  11. void init_costs(double t[], int x);
  12. void show_costs(double t[], int x);
  13. void show(int prodnum, int prodtype, char d[], int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
  14. int menuerror(int min, int max, char prompt[]);
  15. int prompt(char msg[]);
  16. int quit(void);
  17. void message(char msg[]);
  18. void firstUpper(char *buf);
  19.  
  20. /*============================================================ */
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "Lab7.h"
  27.  
  28. /* ================================================================== */
  29.  
  30. // ** Functions **
  31.  
  32. int main()
  33. {
  34. int TRUE = 1;
  35. int FALSE = 0;
  36. int select, end = TRUE;
  37.  
  38. do
  39. {
  40.  
  41. select = menu();
  42. switch (select)
  43. {
  44. case 1: add(); break;
  45. case 2:
  46. case 3:
  47. case 4:
  48. case 5: end = quit(); break;
  49. default: message("\n\nPlease make a selection between 1 and 5.\a");
  50. }
  51. }
  52.  
  53. while (end);
  54. return 0;
  55. }
  56. /* ================================================================ */
  57.  
  58. int menu()
  59. {
  60. int choice;
  61.  
  62. system("cls");
  63.  
  64. printf("Sierra Sporting Goods\n\n");
  65. printf("1 = Add a record\n");
  66. printf("2 = Report\n");
  67. printf("3 = Delete a record\n");
  68. printf("4 = Change a record\n");
  69. printf("5 = Quit\n\n");
  70. choice = menuerror(1, 5, "Enter your selection");
  71.  
  72. return(choice);
  73. }
  74. /* ================================================================ */
  75.  
  76. void heading()
  77. {
  78. system("cls");
  79. printf("Sierra Sporting Goods\n\n\n");
  80. return;
  81. }
  82. /* ================================================================ */
  83.  
  84. int add()
  85. {
  86. double cost, price, prod_cost, prod_price, prod_profit;
  87. double MINCOST = 5.00;
  88. double MAXCOST = 900.00;
  89. double MINPRICE = 6.00;
  90. double MAXPRICE = 1000.00;
  91. int prodnum, prodtype, prodquan;
  92. int MINPROD = 1;
  93. int MAXPROD = 9999;
  94. int MINTYPE = 1;
  95. int MAXTYPE = 5;
  96. int MINQUAN = 1;
  97. int MAXQUAN = 50;
  98. char prodscrip[40];
  99. double types[6];
  100. char choice;
  101.  
  102.  
  103. init_costs(types,6);
  104. do
  105. {
  106. heading();
  107.  
  108. prodnum = getint(MINPROD, MAXPROD, "product number");
  109. prodtype = getint(MINTYPE, MAXTYPE, "product type");
  110.  
  111. getstring(prodscrip, "product description");
  112.  
  113. prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
  114. cost = getreal(MINCOST, MAXCOST, "product cost");
  115. price = getreal(MINPRICE, MAXPRICE, "product price");
  116. prod_cost = total(prodquan, cost);
  117. prod_price = total(prodquan, price);
  118. prod_profit = profit(prod_price, prod_cost);
  119.  
  120. types[prodtype] += prod_cost;
  121.  
  122. show(prodnum, prodtype, prodscrip, prodquan, cost, price, prod_price, prod_cost, prod_profit);
  123. choice = prompt("Would you like to enter another product");
  124. }
  125. while (choice != 'N');
  126. getchar();
  127. show_costs(types,6);
  128.  
  129.  
  130.  
  131. return 0;
  132. }
  133. /* ================================================================ */
  134. void getstring(char entry[], char prompt[])
  135. {
  136. int count;
  137. char prodscrip[40];
  138.  
  139. printf("Enter the %s:", prompt);
  140. count = scanf("%40s", prodscrip);
  141. firstUpper(entry);
  142. return;
  143. }
  144. /* ================================================================ */
  145.  
  146. void firstUpper(char *buf)
  147. {
  148. *buf=toupper(*buf); // first char CAP
  149. while (*buf++ != '\0')
  150. if (*buf == ' ') // look for space
  151. *buf=toupper(*++buf); // CAP next char
  152. }
  153.  
  154.  
  155. /* ================================================================ */
  156.  
  157. int getint(int min, int max, char item[])
  158. {
  159. int x;
  160. printf("Enter a %s between %d and %d: ", item, min, max);
  161. scanf("%d%*c", &x);
  162. while (x < min || x > max)
  163. {
  164. message("\nError in range. Press Enter to continue.\a");
  165. printf("Enter a %s between %d and %d: ", item, min, max);
  166. scanf("%d%*c", &x);
  167. }
  168. return(x);
  169. }
  170. /* ================================================================ */
  171.  
  172. double getreal(double min, double max, char item[])
  173. {
  174. double y;
  175.  
  176. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  177. scanf("%lf%*c", &y);
  178. while (y < min || y > max)
  179. {
  180. message("\nError in range. Press Enter to continue.\a");
  181. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  182. scanf("%lf%*c", &y);
  183. }
  184. return(y);
  185. }
  186. /* ================================================================ */
  187.  
  188. double total(double quantity, double amount)
  189. {
  190. return (quantity * amount);
  191. }
  192. /* ================================================================ */
  193.  
  194. double profit(double profit_price, double profit_cost)
  195. {
  196. return (profit_price - profit_cost);
  197. }
  198. /* ================================================================ */
  199.  
  200. void init_costs(double t[], int x)
  201. {
  202. int z;
  203.  
  204. for (z = 1; z < x; z++)
  205. t[z] = 0;
  206.  
  207. }
  208. /* ================================================================ */
  209.  
  210. void show_costs(double t[], int x)
  211. {
  212. int z;
  213. double total = 0;
  214.  
  215. system("cls");
  216.  
  217. printf(" Product Cost by Type\n\n");
  218. printf("_Type_____________Cost_\n\n");
  219. for (z = 1; z < x; z++)
  220. {
  221. printf(" %2d%20.2lf\n", z, t[z]);
  222. total += t[z];
  223. }
  224. printf("________________________\n");
  225. printf("%23.2lf\n\n\n\n", total);
  226. printf("Press ENTER to return to the Main Menu...");
  227. getchar();
  228.  
  229.  
  230. }
  231. /* ================================================================ */
  232. void message(char msg[])
  233. {
  234. printf("%s\n\n", msg);
  235. }
  236. /* ================================================================ */
  237.  
  238. int prompt(char msg[])
  239. {
  240. int z;
  241. do
  242. {
  243. printf("%s (Y/N)? ", msg);
  244. z = toupper(getchar());
  245. if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
  246. }
  247. while (z != 'Y' && z != 'N');
  248. return(z);
  249. }
  250. /* ================================================================ */
  251.  
  252. int quit()
  253. {
  254. int TRUE = 1;
  255. int FALSE = 0;
  256. int z, end = TRUE;
  257.  
  258. z = prompt("\nEnd program\a");
  259. if (z == 'Y')
  260. {
  261. system("cls");
  262. message("\nSierra Sporting Goods Database closed successfully.");
  263. end = FALSE;
  264. }
  265. return(end);
  266. }
  267. /* ================================================================ */
  268.  
  269. int menuerror(int min, int max, char item[])
  270. {
  271. int z;
  272. printf("%s from %d to %d: ", item, min, max);
  273. scanf("%d%*c", &z);
  274. while (z < min || z > max)
  275. {
  276. message("\nError in range. Press Enter to continue.\a");
  277. printf("%s from %d to %d: ", item, min, max);
  278. scanf("%d%*c", &z);
  279. }
  280. return(z);
  281. }
  282. /* ================================================================ */
  283. void show(int prodnum, int prodtype, char prodscrip[], int prodquan, double cost, double price,
  284. double prod_price, double prod_cost, double prod_profit)
  285. {
  286.  
  287. printf("\n\n\nThe product number is ----------> %04d\n", prodnum);
  288. printf("The product type is ------------> %d\n", prodtype);
  289. printf("The product description is -----> %s\n", prodscrip);
  290. printf("The quantity is ----------------> %d\n", prodquan);
  291. printf("The cost is --------------------> $%.2lf\n", cost);
  292. printf("The price is -------------------> $%.2lf\n\n", price);
  293.  
  294. printf("Total product price ----------> $%.2lf\n", prod_price);
  295. printf("Total product cost -----------> $%.2lf\n", prod_cost);
  296. printf("Total product profit ---------> $%.2lf\n\n\n\n", prod_profit);
  297.  
  298. return;
  299. }
  300. /* ================================================================ */
Last edited by nelledawg; Mar 14th, 2008 at 6:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #5
Mar 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,662
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #6
Mar 14th, 2008
Hummmm. Seems your function needs a tad bit more work. But its close.
  1. int main()
  2. {
  3. char str[] = "now \tis the time for all good men\n";
  4. capitalizeFirsts(str);
  5. puts(str);
  6. return 0;
  7. }

Results
  1. Now is The Time for All good men
  2.  
  3. Press any key to continue . . .
Last edited by Ancient Dragon; Mar 14th, 2008 at 6:17 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 59
Reputation: nelledawg is an unknown quantity at this point 
Solved Threads: 0
nelledawg nelledawg is offline Offline
Junior Poster in Training

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #7
Mar 14th, 2008
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.

  1. // ** Prototypes **
  2. void heading(void);
  3. int menu();
  4. int add();
  5. void getstring(char entry[], char prompt[]);
  6. int getint(int min, int max, char prompt[]);
  7. double getreal(double min, double max, char prompt[]);
  8. double total(double quantity, double amount);
  9. double profit(double profit_cost, double profit_price);
  10. void init_costs(double t[], int x);
  11. void show_costs(double t[], int x);
  12. void show(int prodnum, int prodtype, char d[], int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
  13. int menuerror(int min, int max, char prompt[]);
  14. int prompt(char msg[]);
  15. int quit(void);
  16. void message(char msg[]);
  17. void firstUpper(char *buf);
  18.  
  19. /* ================================================================== */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include "Lab7.h"
  26.  
  27. /* ================================================================== */
  28.  
  29. // ** Functions **
  30.  
  31. int main()
  32. {
  33. int TRUE = 1;
  34. int FALSE = 0;
  35. int select, end = TRUE;
  36.  
  37. do
  38. {
  39.  
  40. select = menu();
  41. switch (select)
  42. {
  43. case 1: add(); break;
  44. case 2:
  45. case 3:
  46. case 4:
  47. case 5: end = quit(); break;
  48. default: message("\n\nPlease make a selection between 1 and 5.\a");
  49. }
  50. }
  51.  
  52. while (end);
  53. return 0;
  54. }
  55. /* ================================================================ */
  56.  
  57. int menu()
  58. {
  59. int choice;
  60.  
  61. system("cls");
  62.  
  63. printf("Sierra Sporting Goods\n\n");
  64. printf("1 = Add a record\n");
  65. printf("2 = Report\n");
  66. printf("3 = Delete a record\n");
  67. printf("4 = Change a record\n");
  68. printf("5 = Quit\n\n");
  69. choice = menuerror(1, 5, "Enter your selection");
  70.  
  71. return(choice);
  72. }
  73. /* ================================================================ */
  74.  
  75. void heading()
  76. {
  77. system("cls");
  78. printf("Sierra Sporting Goods\n\n\n");
  79. return;
  80. }
  81. /* ================================================================ */
  82.  
  83. int add()
  84. {
  85. double cost, price, prod_cost, prod_price, prod_profit;
  86. double MINCOST = 5.00;
  87. double MAXCOST = 900.00;
  88. double MINPRICE = 6.00;
  89. double MAXPRICE = 1000.00;
  90. int prodnum, prodtype, prodquan;
  91. int MINPROD = 1;
  92. int MAXPROD = 9999;
  93. int MINTYPE = 1;
  94. int MAXTYPE = 5;
  95. int MINQUAN = 1;
  96. int MAXQUAN = 50;
  97. char prodscrip[40];
  98. double types[6];
  99. char choice;
  100.  
  101.  
  102. init_costs(types,6);
  103. do
  104. {
  105. heading();
  106.  
  107. prodnum = getint(MINPROD, MAXPROD, "product number");
  108. prodtype = getint(MINTYPE, MAXTYPE, "product type");
  109.  
  110. getstring(prodscrip, "product description");
  111. firstUpper(prodscrip);
  112. puts(prodscrip);
  113.  
  114. prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
  115. cost = getreal(MINCOST, MAXCOST, "product cost");
  116. price = getreal(MINPRICE, MAXPRICE, "product price");
  117. prod_cost = total(prodquan, cost);
  118. prod_price = total(prodquan, price);
  119. prod_profit = profit(prod_price, prod_cost);
  120.  
  121. types[prodtype] += prod_cost;
  122.  
  123. show(prodnum, prodtype, prodscrip, prodquan, cost, price, prod_price, prod_cost, prod_profit);
  124. choice = prompt("Would you like to enter another product");
  125. }
  126. while (choice != 'N');
  127. getchar();
  128. show_costs(types,6);
  129.  
  130.  
  131.  
  132. return 0;
  133. }
  134. /* ================================================================ */
  135. void getstring(char entry[], char prompt[])
  136. {
  137. int count;
  138. char prodscrip[40];
  139.  
  140. printf("Enter the %s:", prompt);
  141. count = scanf("%40s", prodscrip);
  142. return;
  143. }
  144. /* ================================================================ */
  145.  
  146. void firstUpper(char *buf)
  147. {
  148. *buf=toupper(*buf); // first char CAP
  149. while (*buf++ != '\0')
  150. if (*buf == ' ') // look for space
  151. *buf=toupper(*++buf); // CAP next char
  152. }
  153.  
  154.  
  155. /* ================================================================ */
  156.  
  157. int getint(int min, int max, char item[])
  158. {
  159. int x;
  160. printf("Enter a %s between %d and %d: ", item, min, max);
  161. scanf("%d%*c", &x);
  162. while (x < min || x > max)
  163. {
  164. message("\nError in range. Press Enter to continue.\a");
  165. printf("Enter a %s between %d and %d: ", item, min, max);
  166. scanf("%d%*c", &x);
  167. }
  168. return(x);
  169. }
  170. /* ================================================================ */
  171.  
  172. double getreal(double min, double max, char item[])
  173. {
  174. double y;
  175.  
  176. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  177. scanf("%lf%*c", &y);
  178. while (y < min || y > max)
  179. {
  180. message("\nError in range. Press Enter to continue.\a");
  181. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  182. scanf("%lf%*c", &y);
  183. }
  184. return(y);
  185. }
  186. /* ================================================================ */
  187.  
  188. double total(double quantity, double amount)
  189. {
  190. return (quantity * amount);
  191. }
  192. /* ================================================================ */
  193.  
  194. double profit(double profit_price, double profit_cost)
  195. {
  196. return (profit_price - profit_cost);
  197. }
  198. /* ================================================================ */
  199.  
  200. void init_costs(double t[], int x)
  201. {
  202. int z;
  203.  
  204. for (z = 1; z < x; z++)
  205. t[z] = 0;
  206.  
  207. }
  208. /* ================================================================ */
  209.  
  210. void show_costs(double t[], int x)
  211. {
  212. int z;
  213. double total = 0;
  214.  
  215. system("cls");
  216.  
  217. printf(" Product Cost by Type\n\n");
  218. printf("_Type_____________Cost_\n\n");
  219. for (z = 1; z < x; z++)
  220. {
  221. printf(" %2d%20.2lf\n", z, t[z]);
  222. total += t[z];
  223. }
  224. printf("________________________\n");
  225. printf("%23.2lf\n\n\n\n", total);
  226. printf("Press ENTER to return to the Main Menu...");
  227. getchar();
  228.  
  229.  
  230. }
  231. /* ================================================================ */
  232. void message(char msg[])
  233. {
  234. printf("%s\n\n", msg);
  235. }
  236. /* ================================================================ */
  237.  
  238. int prompt(char msg[])
  239. {
  240. int z;
  241. do
  242. {
  243. printf("%s (Y/N)? ", msg);
  244. z = toupper(getchar());
  245. if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
  246. }
  247. while (z != 'Y' && z != 'N');
  248. return(z);
  249. }
  250. /* ================================================================ */
  251.  
  252. int quit()
  253. {
  254. int TRUE = 1;
  255. int FALSE = 0;
  256. int z, end = TRUE;
  257.  
  258. z = prompt("\nEnd program\a");
  259. if (z == 'Y')
  260. {
  261. system("cls");
  262. message("\nSierra Sporting Goods Database closed successfully.");
  263. end = FALSE;
  264. }
  265. return(end);
  266. }
  267. /* ================================================================ */
  268.  
  269. int menuerror(int min, int max, char item[])
  270. {
  271. int z;
  272. printf("%s from %d to %d: ", item, min, max);
  273. scanf("%d%*c", &z);
  274. while (z < min || z > max)
  275. {
  276. message("\nError in range. Press Enter to continue.\a");
  277. printf("%s from %d to %d: ", item, min, max);
  278. scanf("%d%*c", &z);
  279. }
  280. return(z);
  281. }
  282. /* ================================================================ */
  283. void show(int prodnum, int prodtype, char prodscrip[], int prodquan, double cost, double price,
  284. double prod_price, double prod_cost, double prod_profit)
  285. {
  286.  
  287. printf("\n\n\nThe product number is ----------> %04d\n", prodnum);
  288. printf("The product type is ------------> %d\n", prodtype);
  289. printf("The product description is -----> %s\n", prodscrip);
  290. printf("The quantity is ----------------> %d\n", prodquan);
  291. printf("The cost is --------------------> $%.2lf\n", cost);
  292. printf("The price is -------------------> $%.2lf\n\n", price);
  293.  
  294. printf("Total product price ----------> $%.2lf\n", prod_price);
  295. printf("Total product cost -----------> $%.2lf\n", prod_cost);
  296. printf("Total product profit ---------> $%.2lf\n\n\n\n", prod_profit);
  297.  
  298. return;
  299. }
  300. /* ================================================================ */
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #8
Mar 14th, 2008
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
  1. getstring(prodscrip,"product description");

and replace it with
  1. printf("Enter the product description : ");
  2. gets(prodscrip);

that will work.
Last edited by jephthah; Mar 14th, 2008 at 8:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 59
Reputation: nelledawg is an unknown quantity at this point 
Solved Threads: 0
nelledawg nelledawg is offline Offline
Junior Poster in Training

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #9
Mar 14th, 2008
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?

  1. // ** Prototypes **
  2. void heading(void);
  3. int menu();
  4. int add();
  5. void getstring(char entry[], char prompt[]);
  6. int getint(int min, int max, char prompt[]);
  7. double getreal(double min, double max, char prompt[]);
  8. double total(double quantity, double amount);
  9. double profit(double profit_cost, double profit_price);
  10. void init_costs(double t[], int x);
  11. void show_costs(double t[], int x);
  12. void show(int prodnum, int prodtype, char d[], int prodquan, double cost, double price, double prod_price, double prod_cost, double prod_profit);
  13. int menuerror(int min, int max, char prompt[]);
  14. int prompt(char msg[]);
  15. int quit(void);
  16. void message(char msg[]);
  17. void firstUpper(char *buf);
  18.  
  19. /* ================================================================== */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ctype.h>
  24. #include <string.h>
  25. #include "Lab7.h"
  26.  
  27. /* ================================================================== */
  28.  
  29. // ** Functions **
  30.  
  31. int main()
  32. {
  33. int TRUE = 1;
  34. int FALSE = 0;
  35. int select, end = TRUE;
  36.  
  37. do
  38. {
  39.  
  40. select = menu();
  41. switch (select)
  42. {
  43. case 1: add(); break;
  44. case 2:
  45. case 3:
  46. case 4:
  47. case 5: end = quit(); break;
  48. default: message("\n\nPlease make a selection between 1 and 5.\a");
  49. }
  50. }
  51.  
  52. while (end);
  53. return 0;
  54. }
  55. /* ================================================================ */
  56.  
  57. int menu()
  58. {
  59. int choice;
  60.  
  61. system("cls");
  62.  
  63. printf("Sierra Sporting Goods\n\n");
  64. printf("1 = Add a record\n");
  65. printf("2 = Report\n");
  66. printf("3 = Delete a record\n");
  67. printf("4 = Change a record\n");
  68. printf("5 = Quit\n\n");
  69. choice = menuerror(1, 5, "Enter your selection");
  70.  
  71. return(choice);
  72. }
  73. /* ================================================================ */
  74.  
  75. void heading()
  76. {
  77. system("cls");
  78. printf("Sierra Sporting Goods\n\n\n");
  79. return;
  80. }
  81. /* ================================================================ */
  82.  
  83. int add()
  84. {
  85. double cost, price, prod_cost, prod_price, prod_profit;
  86. double MINCOST = 5.00;
  87. double MAXCOST = 900.00;
  88. double MINPRICE = 6.00;
  89. double MAXPRICE = 1000.00;
  90. int prodnum, prodtype, prodquan;
  91. int MINPROD = 1;
  92. int MAXPROD = 9999;
  93. int MINTYPE = 1;
  94. int MAXTYPE = 5;
  95. int MINQUAN = 1;
  96. int MAXQUAN = 50;
  97. char prodscrip[40];
  98. double types[6];
  99. char choice;
  100.  
  101.  
  102. init_costs(types,6);
  103. do
  104. {
  105. heading();
  106.  
  107. prodnum = getint(MINPROD, MAXPROD, "product number");
  108. prodtype = getint(MINTYPE, MAXTYPE, "product type");
  109.  
  110. printf("product description : ");
  111. gets(prodscrip);
  112.  
  113. prodquan = getint(MINQUAN, MAXQUAN, "product quantity");
  114. cost = getreal(MINCOST, MAXCOST, "product cost");
  115. price = getreal(MINPRICE, MAXPRICE, "product price");
  116. prod_cost = total(prodquan, cost);
  117. prod_price = total(prodquan, price);
  118. prod_profit = profit(prod_price, prod_cost);
  119.  
  120. types[prodtype] += prod_cost;
  121.  
  122. show(prodnum, prodtype, prodscrip, prodquan, cost, price, prod_price, prod_cost, prod_profit);
  123. choice = prompt("Would you like to enter another product");
  124. }
  125. while (choice != 'N');
  126. getchar();
  127. show_costs(types,6);
  128.  
  129.  
  130.  
  131. return 0;
  132. }
  133. /* ================================================================ */
  134. void getstring(char entry[], char prompt[])
  135. {
  136. int description;
  137. char prodscrip[40];
  138.  
  139. printf("Enter the %s:", prompt);
  140. description = scanf("%40s", prodscrip);
  141. return;
  142. }
  143. /* ================================================================ */
  144.  
  145. void firstUpper(char *buf)
  146. {
  147. *buf=toupper(*buf); // first char CAP
  148. while (*buf++ != '\0')
  149. if (*buf == ' ') // look for space
  150. *buf=toupper(*++buf); // CAP next char
  151. }
  152.  
  153.  
  154. /* ================================================================ */
  155.  
  156. int getint(int min, int max, char item[])
  157. {
  158. int x;
  159. printf("Enter a %s between %d and %d: ", item, min, max);
  160. scanf("%d%*c", &x);
  161. while (x < min || x > max)
  162. {
  163. message("\nError in range. Press Enter to continue.\a");
  164. printf("Enter a %s between %d and %d: ", item, min, max);
  165. scanf("%d%*c", &x);
  166. }
  167. return(x);
  168. }
  169. /* ================================================================ */
  170.  
  171. double getreal(double min, double max, char item[])
  172. {
  173. double y;
  174.  
  175. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  176. scanf("%lf%*c", &y);
  177. while (y < min || y > max)
  178. {
  179. message("\nError in range. Press Enter to continue.\a");
  180. printf("Enter a %s between $%.2lf and $%.2lf: ", item, min, max);
  181. scanf("%lf%*c", &y);
  182. }
  183. return(y);
  184. }
  185. /* ================================================================ */
  186.  
  187. double total(double quantity, double amount)
  188. {
  189. return (quantity * amount);
  190. }
  191. /* ================================================================ */
  192.  
  193. double profit(double profit_price, double profit_cost)
  194. {
  195. return (profit_price - profit_cost);
  196. }
  197. /* ================================================================ */
  198.  
  199. void init_costs(double t[], int x)
  200. {
  201. int z;
  202.  
  203. for (z = 1; z < x; z++)
  204. t[z] = 0;
  205.  
  206. }
  207. /* ================================================================ */
  208.  
  209. void show_costs(double t[], int x)
  210. {
  211. int z;
  212. double total = 0;
  213.  
  214. system("cls");
  215.  
  216. printf(" Product Cost by Type\n\n");
  217. printf("_Type_____________Cost_\n\n");
  218. for (z = 1; z < x; z++)
  219. {
  220. printf(" %2d%20.2lf\n", z, t[z]);
  221. total += t[z];
  222. }
  223. printf("________________________\n");
  224. printf("%23.2lf\n\n\n\n", total);
  225. printf("Press ENTER to return to the Main Menu...");
  226. getchar();
  227.  
  228.  
  229. }
  230. /* ================================================================ */
  231. void message(char msg[])
  232. {
  233. printf("%s\n\n", msg);
  234. }
  235. /* ================================================================ */
  236.  
  237. int prompt(char msg[])
  238. {
  239. int z;
  240. do
  241. {
  242. printf("%s (Y/N)? ", msg);
  243. z = toupper(getchar());
  244. if (z != 'Y' && z != 'N') printf("\nError, please enter Y or N only.\n");
  245. }
  246. while (z != 'Y' && z != 'N');
  247. return(z);
  248. }
  249. /* ================================================================ */
  250.  
  251. int quit()
  252. {
  253. int TRUE = 1;
  254. int FALSE = 0;
  255. int z, end = TRUE;
  256.  
  257. z = prompt("\nEnd program\a");
  258. if (z == 'Y')
  259. {
  260. system("cls");
  261. message("\nSierra Sporting Goods Database closed successfully.");
  262. end = FALSE;
  263. }
  264. return(end);
  265. }
  266. /* ================================================================ */
  267.  
  268. int menuerror(int min, int max, char item[])
  269. {
  270. int z;
  271. printf("%s from %d to %d: ", item, min, max);
  272. scanf("%d%*c", &z);
  273. while (z < min || z > max)
  274. {
  275. message("\nError in range. Press Enter to continue.\a");
  276. printf("%s from %d to %d: ", item, min, max);
  277. scanf("%d%*c", &z);
  278. }
  279. return(z);
  280. }
  281. /* ================================================================ */
  282. void show(int prodnum, int prodtype, char prodscrip[], int prodquan, double cost, double price,
  283. double prod_price, double prod_cost, double prod_profit)
  284. {
  285.  
  286. printf("\n\n\nThe product number is ----------> %04d\n", prodnum);
  287. printf("The product type is ------------> %d\n", prodtype);
  288. printf("The product description is -----> %s\n", prodscrip);
  289. printf("The quantity is ----------------> %d\n", prodquan);
  290. printf("The cost is --------------------> $%.2lf\n", cost);
  291. printf("The price is -------------------> $%.2lf\n\n", price);
  292.  
  293. printf("Total product price ----------> $%.2lf\n", prod_price);
  294. printf("Total product cost -----------> $%.2lf\n", prod_cost);
  295. printf("Total product profit ---------> $%.2lf\n\n\n\n", prod_profit);
  296.  
  297. return;
  298. }
  299. /* ================================================================ */
Last edited by Ancient Dragon; Mar 14th, 2008 at 10:02 pm. Reason: correct code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Capitalize letter in string to output... Should be a pretty easy solution!

 
0
  #10
Mar 14th, 2008
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:
  1. firstUpper(prodscrip);
  2. puts(prodscrip);


just curious, how much help did you have writing the rest of the program? did you write it yourself?
Last edited by jephthah; Mar 14th, 2008 at 8:57 pm.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 1172 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC