Concerning C++ calculations using printf

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

Join Date: Feb 2005
Posts: 31
Reputation: mcldev is an unknown quantity at this point 
Solved Threads: 2
mcldev mcldev is offline Offline
Light Poster

Re: Concerning C++ calculations using printf

 
0
  #21
Aug 10th, 2007
Line 68, change the scanf to use one variable per format specifier. Otherwise you program may be hosing the stack and giving you u unpredictable results
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Concerning C++ calculations using printf

 
0
  #22
Aug 10th, 2007
You are calling this function the wrong way. You are passing the wrong arguments.
This is how the function is declared.
float price( const float ticket, int *num );

accept first an unchangeable float that's what const means, and a pointer to an int variable. OK? Now let's take a piece of your code where you call this function.


DailyChild = price( &DailyChildNum, DChild );

use are passing &DailyChildNum as the first argument. What is it?. The address of an int.
price only accepts a const float. Can you understand? Wrong type of variable and it cannot be the address.
Let's look at the second argument: DChild.
What is it? const float DChild = 1.50; A const float. What should be there? A pointer to an int. Something like &DChild if that were an int, which is not.
It seems that you have them switched.

By the way there's not reason for that function to not be declared after main like the others and prototyped at the beginning like the others.
Last edited by Aia; Aug 10th, 2007 at 12:42 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 58
Reputation: NycNessyness is an unknown quantity at this point 
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: Concerning C++ calculations using printf

 
0
  #23
Aug 10th, 2007
Thanks so much for the help guys! It works perfectly.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 58
Reputation: NycNessyness is an unknown quantity at this point 
Solved Threads: 0
NycNessyness NycNessyness is offline Offline
Junior Poster in Training

Re: Concerning C++ calculations using printf

 
0
  #24
Aug 10th, 2007
I lastly wanted to ask, although its not really needed, but for future purposes, how would I be able to save the input I entered. For example when the program runs and the buyer purchases a yearly ticket and then purchase a daily ticket both totals are displayed. But lets say the customer purchases 2 or more daily tickets it only shows the total for the last number entered. This is my current code.

  1. [INLINECODE][CODE]/*+*+*+*+*+*+*+* Preprocessor Directives *+*+*+*+*+*+*+*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /*+*+*+*+*+*+*+* Function Prototypes *+*+*+*+*+*+*+*/
  8.  
  9.  
  10. void PrintAStarLine(void);
  11. void GetInput(void);
  12. void Tickets(void);
  13. void DailyTickets(void);
  14. void YearlyTickets(void);
  15. void DailyPageDisplay(void);
  16. void YearlyPageDisplay(void);
  17. void TotalPageDisplay(void);
  18. void DailyCalculations(void);
  19. void YearlyCalculations(void);
  20. void TotalCalculation(void);
  21. void AskQuestion(void);
  22.  
  23. /*+*+*+*+*+*+*+* Variables *+*+*+*+*+*+*+*/
  24.  
  25.  
  26.  
  27.  
  28. const float DChild = 1.50;
  29. float DailyChild = 0.00;
  30. int DailyChildNum = 0;
  31.  
  32. const float YChild = 5.50;
  33. float YearlyChild = 0.00;
  34. int YearlyChildNum = 0;
  35.  
  36.  
  37. const float DAdult = 2.00;
  38. float DailyAdult = 0.00;
  39. int DailyAdultNum = 0;
  40.  
  41. const float YAdult = 6.50;
  42. float YearlyAdult = 0.00;
  43. int YearlyAdultNum = 0;
  44.  
  45. const float DSenior = 1.25;
  46. float DailySenior = 0.00;
  47. int DailySeniorNum = 0;
  48.  
  49. const float YSenior = 3.75;
  50. float YearlySenior = 0.00;
  51. int YearlySeniorNum = 0;
  52.  
  53. float DailyCost;
  54. float YearlyCost;
  55. float TotalCost;
  56.  
  57. float DailyDiscount;
  58. float YearlyDiscount;
  59. float TotalDiscount;
  60.  
  61. float TotalChild;
  62. float TotalAdult;
  63. float TotalSenior;
  64.  
  65. float DailyTotal;
  66. float YearlyTotal;
  67. float Total;
  68.  
  69. int TotalChildNum;
  70. int TotalAdultNum;
  71. int TotalSeniorNum;
  72.  
  73. char customerName[30];
  74. char tickettype[8];
  75. char TicketNumber[8];
  76. char NumberName[10];
  77. char Underlines[10];
  78. char aQuestion;
  79. char TotalIncome[10];
  80.  
  81.  
  82.  
  83. float price( const float ticket, int *num )
  84. {
  85. char newline;
  86.  
  87. scanf( "%d%c", num, &newline );
  88.  
  89. return ticket * (*num);
  90. }
  91.  
  92. /*+*+*+*+*+*+*+*^ The Main Module ^*+*+*+*+*+*+*+*/
  93.  
  94.  
  95. int main( void )
  96. {
  97.  
  98.  
  99.  
  100.  
  101.  
  102. PrintAStarLine();
  103.  
  104. printf(" Home Aquarium Data Entry \n");
  105. printf("\n\n");
  106.  
  107. PrintAStarLine();
  108.  
  109. aQuestion = 'N'; /* It will loop until user enter YES or YES */
  110. while ((aQuestion != 'Y') && (aQuestion != 'y')) /*sets value to go into loop the first time */
  111. {
  112. GetInput();
  113.  
  114. Tickets();
  115.  
  116. AskQuestion();
  117. }
  118.  
  119. system("cls");
  120.  
  121. TotalCalculation();
  122.  
  123. TotalPageDisplay();
  124.  
  125. return 0;
  126. }
  127.  
  128. /*+*+*+*+*+*+*+* Avoids making the program look real plain *+*+*+*+*+*+*+*/
  129.  
  130. void PrintAStarLine(void)
  131.  
  132. {
  133.  
  134. printf("******************************************************************************** \n");
  135. printf("\n\n");
  136.  
  137. return;
  138.  
  139. }
  140.  
  141.  
  142. /*+*+*+*+*+*+*+* Asks The User for their name *+*+*+*+*+*+*+*/
  143.  
  144. void GetInput(void)
  145. {
  146.  
  147.  
  148.  
  149. printf("Customer Name: ");
  150. scanf( "%[^\n]", customerName);
  151. printf("\n\n\n");
  152.  
  153. return;
  154. }
  155.  
  156.  
  157. /*+*+*+*+*+*+*+* Asks The User what ticket type they want *+*+*+*+*+*+*+*/
  158.  
  159. void Tickets(void)
  160. {
  161.  
  162. printf("\n\nType of ticket: ");
  163. scanf(" %s", &TicketNumber);
  164.  
  165.  
  166. if(strcmp(TicketNumber,"D")==0)
  167. {
  168. DailyTickets();
  169. DailyCalculations();
  170. DailyPageDisplay();
  171. }
  172. else if(strcmp(TicketNumber,"d")==0)
  173. {
  174. DailyTickets();
  175. DailyCalculations();
  176. DailyPageDisplay();
  177. }
  178. else if(strcmp(TicketNumber,"daily")==0)
  179. {
  180. DailyTickets();
  181. DailyCalculations();
  182. DailyPageDisplay();
  183. }
  184. else if(strcmp(TicketNumber,"Daily")==0)
  185. {
  186. DailyTickets();
  187. DailyCalculations();
  188. DailyPageDisplay();
  189. }
  190. else if(strcmp(TicketNumber,"Y")==0)
  191. {
  192. YearlyTickets();
  193. YearlyCalculations();
  194. YearlyPageDisplay();
  195. }
  196. else if(strcmp(TicketNumber,"y")==0)
  197. {
  198. YearlyTickets();
  199. YearlyCalculations();
  200. YearlyPageDisplay();
  201. }
  202. else if(strcmp(TicketNumber,"Yearly")==0)
  203. {
  204. YearlyTickets();
  205. YearlyCalculations();
  206. YearlyPageDisplay();
  207. }
  208. else if(strcmp(TicketNumber,"yearly")==0)
  209. {
  210. YearlyTickets();
  211. YearlyCalculations();
  212. YearlyPageDisplay();
  213. }
  214. else
  215. {
  216. printf("\n Incorrect Ticket Number Entered, please try again! \n");
  217. printf("\n");
  218. Tickets();
  219. }
  220. printf("\n");
  221. return;
  222. }
  223.  
  224. /*+*+*+*+*+*+*+* Allows the user to enter daily tickets *+*+*+*+*+*+*+*/
  225.  
  226. void DailyTickets(void)
  227. {
  228.  
  229. printf("\n\n");
  230. printf("#of Children: " );
  231. DailyChild = price( DChild, &DailyChildNum );
  232. printf("\n\n");
  233.  
  234.  
  235. printf("#of Adults: " );
  236. DailyAdult = price( DAdult, &DailyAdultNum );
  237. printf("\n\n");
  238.  
  239.  
  240. printf("#of Seniors: " );
  241. DailySenior = price( DSenior, &DailySeniorNum );
  242. printf("\n\n");
  243.  
  244.  
  245. return;
  246. }
  247.  
  248. /*+*+*+*+*+*+*+* Allows the user to enter yearly tickets *+*+*+*+*+*+*+*/
  249.  
  250. void YearlyTickets(void)
  251. {
  252. printf("\n\n");
  253. printf("#of Children: " );
  254. YearlyChild = price( YChild, &YearlyChildNum);
  255. printf("\n\n");
  256.  
  257.  
  258. printf("#of Adults: " );
  259. YearlyAdult = price( YAdult, &YearlyAdultNum);
  260. printf("\n\n");
  261.  
  262.  
  263. printf("#of Seniors: " );
  264. YearlySenior = price( YSenior, &YearlySeniorNum);
  265. system("pause");
  266. printf("\n\n\n");
  267.  
  268. return;
  269. }
  270.  
  271. /*+*+*+*+*+*+*+* Displays information that the user entered *+*+*+*+*+*+*+*/
  272.  
  273. void DailyPageDisplay(void)
  274. {
  275. system("cls");
  276.  
  277. PrintAStarLine();
  278. printf(" Home Aquarium Contract \n");
  279. printf("\n\n");
  280. PrintAStarLine();
  281.  
  282.  
  283. printf("Customer: %s \n", customerName);
  284.  
  285.  
  286. if(strcmp(TicketNumber,"D")==0)
  287. {
  288. printf("Type of ticket: Daily \n", TicketNumber);
  289. printf("\n\n");
  290. }
  291. else if(strcmp(TicketNumber,"d")==0)
  292. {
  293. printf("Type of ticket: Daily \n", TicketNumber);
  294. printf("\n\n");
  295. }
  296. else if(strcmp(TicketNumber,"Daily")==0)
  297. {
  298. printf("Type of ticket: Daily \n", TicketNumber);
  299. printf("\n\n");
  300. }
  301. else if(strcmp(TicketNumber,"daily")==0)
  302. {
  303. printf("Type of ticket: Daily \n", TicketNumber);
  304. printf("\n\n");
  305. }
  306.  
  307.  
  308.  
  309. system("pause");
  310. printf("\t\t\t\t Number\t\t\t\t Charge%s \n", NumberName);
  311. printf("\n\n");
  312. printf( "Child:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailyChildNum, DailyChild );
  313. printf("\n");
  314. printf( "Adult:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailyAdultNum, DailyAdult );
  315. printf("\n");
  316. printf( "Senior:\t\t\t\t%5d\t\t\t\t$%6.2f\n", DailySeniorNum, DailySenior );
  317. printf("\n");
  318. printf(" ======= ======= %s \n", Underlines);
  319. printf("\n\n");
  320. printf( "Total:\t\t\t\t\t\t\t\t$%6.2f\n", DailyCost );
  321. printf("\n");
  322. printf( "Less Discount:\t\t\t\t\t\t\t$%6.2f\n", DailyDiscount );
  323. printf("\n");
  324. printf( "Total After Discount:\t\t\t\t\t\t$%6.2f\n", DailyTotal );
  325. printf("\n");
  326.  
  327. return;
  328. }
  329.  
  330. /*+*+*+*+*+*+*+* Displays information that the user entered *+*+*+*+*+*+*+*/
  331.  
  332. void YearlyPageDisplay(void)
  333. {
  334. system("cls");
  335.  
  336. PrintAStarLine();
  337. printf(" Home Aquarium Contract \n");
  338. printf("\n\n");
  339. PrintAStarLine();
  340.  
  341.  
  342. printf("Customer: %s \n", customerName);
  343.  
  344.  
  345. if (TicketNumber == "Y")
  346. {
  347. printf("Type of ticket: Yearly \n", TicketNumber);
  348. printf("\n\n");
  349. }
  350. else if(strcmp(TicketNumber,"y")==0)
  351. {
  352. printf("Type of ticket: Yearly \n", TicketNumber);
  353. printf("\n\n");
  354. }
  355. else if(strcmp(TicketNumber,"Yearly")==0)
  356. {
  357. printf("Type of ticket: Yearly \n", TicketNumber);
  358. printf("\n\n");
  359. }
  360. else if(strcmp(TicketNumber,"yearly")==0)
  361. {
  362. printf("Type of ticket: Yearly \n", TicketNumber);
  363. printf("\n\n");
  364. }
  365.  
  366.  
  367. system("pause");
  368. printf("\t\t\t\t Number\t\t\t\t Charge%s \n", NumberName);
  369. printf("\n\n");
  370. printf( "Child:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlyChildNum, YearlyChild );
  371. printf("\n");
  372. printf( "Adult:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlyAdultNum, YearlyAdult );
  373. printf("\n");
  374. printf( "Senior:\t\t\t\t%5d\t\t\t\t$%6.2f\n", YearlySeniorNum, YearlySenior );
  375. printf("\n");
  376. printf(" ======= ======= %s \n", Underlines);
  377. printf("\n\n");
  378. printf( "Total:\t\t\t\t\t\t\t\t$%6.2f\n", YearlyCost );
  379. printf("\n");
  380. printf( "Less Discount:\t\t\t\t\t\t\t$%6.2f\n", YearlyDiscount );
  381. printf("\n");
  382. printf( "Total After Discount:\t\t\t\t\t\t$%6.2f\n", YearlyTotal );
  383. printf("\n");
  384.  
  385. return;
  386. }
  387.  
  388. /*+*+*+*+*+*+*+* Performs Calculations *+*+*+*+*+*+*+*/
  389.  
  390. void DailyCalculations(void)
  391. {
  392.  
  393.  
  394. /* Performs Daily Calculations */
  395.  
  396.  
  397. DailyCost = (DailyChild) + (DailyAdult) + (DailySenior);
  398.  
  399.  
  400. if ((DailyAdult > 2) && (DailyChild > 1))
  401. DailyDiscount = float(DailyCost * .10);
  402.  
  403. else if ((DailySenior > 3) && (DailyChild > 1))
  404. DailyDiscount = float(DailyCost * .10);
  405.  
  406. else
  407. DailyDiscount = (DailyCost - DailyCost);
  408. DailyTotal = (DailyCost - DailyDiscount);
  409.  
  410. return;
  411.  
  412. }
  413.  
  414.  
  415.  
  416.  
  417. void YearlyCalculations(void)
  418. {
  419. /* Performs Yearly Calculations */
  420.  
  421.  
  422. YearlyCost = (YearlyChild) + (YearlyAdult) + (YearlySenior);
  423.  
  424. if ((YearlyAdult > 2) && (YearlyChild > 1))
  425. YearlyDiscount = float(YearlyCost * .10);
  426.  
  427. else if ((YearlySenior > 3) && (YearlyChild > 1))
  428. YearlyDiscount = float(YearlyCost * .10);
  429. else
  430. YearlyDiscount = (YearlyCost - YearlyCost);
  431. YearlyTotal = (YearlyCost - YearlyDiscount);
  432.  
  433. return;
  434.  
  435. }
  436.  
  437. void TotalCalculation(void)
  438. {
  439. /* Performs Yearly Calculations */
  440.  
  441.  
  442.  
  443. TotalChild = (DailyChild + YearlyChild);
  444. TotalAdult = (DailyAdult + YearlyAdult);
  445. TotalSenior = (DailySenior + YearlySenior);
  446. TotalCost = (DailyCost + YearlyCost);
  447. TotalDiscount = (DailyDiscount + YearlyDiscount);
  448. Total = (TotalCost - TotalDiscount);
  449.  
  450. TotalChildNum = DailyChildNum + YearlyChildNum;
  451. TotalAdultNum = DailyAdultNum + YearlyAdultNum;
  452. TotalSeniorNum = DailySeniorNum + YearlySeniorNum;
  453.  
  454. return;
  455. }
  456.  
  457.  
  458. /*+*+*+*+*+*+*+* Asks The User A Question *+*+*+*+*+*+*+*/
  459.  
  460. void AskQuestion(void)
  461. {
  462. printf("\n\nAre we done for the day? (Yes/No)");
  463. scanf(" %c", &aQuestion);
  464. fflush(stdin);
  465.  
  466.  
  467.  
  468. return;
  469. }
  470.  
  471. /*+*+*+*+*+*+*+* Displays Final Cost to User *+*+*+*+*+*+*+*/
  472.  
  473. void TotalPageDisplay(void)
  474. {
  475.  
  476. system("cls");
  477.  
  478. PrintAStarLine();
  479. printf(" Home Aquarium Daily Totals \n");
  480. printf("\n\n");
  481. PrintAStarLine();
  482.  
  483. printf("Total Total \nNumber Income %s \n", TotalIncome);
  484.  
  485. printf("\n\n");
  486. printf("%d Child: $%6.2f\n", TotalChildNum, TotalChild );
  487. printf("\n");
  488. printf( "%d Adult: $%6.2f\n", TotalAdultNum, TotalAdult );
  489. printf("\n");
  490. printf( "%d Senior: $%6.2f\n", TotalSeniorNum, TotalSenior );
  491. printf("\n");
  492.  
  493. printf("________________________________________________________________________________%s \n", Underlines);
  494.  
  495.  
  496. printf( "Grand Total: $%6.2f\n", TotalCost );
  497. printf("\n");
  498.  
  499. printf( "Grand Total After Discount: $%6.2f\n", Total );
  500. printf("\n");
  501.  
  502. return;
  503.  
  504. }
[/INLINECODE][/code]
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC