943,670 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2716
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 4th, 2007
0

Concerning C++ calculations using printf

Expand Post »
I'm currently having a problem with having where it says DailyChild on my code to go up when the user adds a number. For example if they type 1 it should say 1.50. If they enter 2, it should say 3.00. But no matter what I do, it keeps printing coming up to 1.50. Can someone show me what I'm doing wrong please, really in a jam on trying to get this to work. Also may someone be able to tell me why when I compile my program it shows no errors but when I'm bout to exit the program it says theres an error. You'll notice it when you enter in the code. Also if you notice any other problems with my code, please let me know. Thanks for the help, I'm patiently hoping for a reply soon!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream>
  5.  
  6.  
  7.  
  8. void PrintAStarLine(void);
  9. void GetInput(void);
  10. void GetTicket(void);
  11. void HowManyChildren(void);
  12. void HowManyAdults(void);
  13. void HowManySeniors(void);
  14. void ChildrenCalculations(void);
  15.  
  16.  
  17. float DailyChild;
  18. float YearlyChild;
  19. float DailyAdult;
  20. float YearlyAdult;
  21. float DailySenior;
  22. float YearlySenior;
  23.  
  24. char customerName[30];
  25. char tickettype[8];
  26. int childrenNum;
  27. int adultNum;
  28. int seniorNum;
  29.  
  30.  
  31. main()
  32. {
  33.  
  34. {
  35. PrintAStarLine();
  36.  
  37. printf(" Home Aquarium Data Entry \n");
  38. printf("\n\n");
  39.  
  40. PrintAStarLine();
  41.  
  42. GetInput();
  43. }
  44. GetTicket();
  45.  
  46.  
  47.  
  48. return 0;
  49. }
  50.  
  51. void PrintAStarLine(void)
  52.  
  53. {
  54.  
  55. printf("**************************************************************************** \n");
  56. printf("\n\n");
  57.  
  58. return;
  59.  
  60. }
  61.  
  62. void GetInput(void)
  63. {
  64.  
  65.  
  66.  
  67. printf("Customer Name (First Middle Last): ");
  68. scanf(" %s %s %s", &customerName);
  69. printf("\n\n\n");
  70.  
  71. return;
  72. }
  73.  
  74.  
  75. void GetTicket(void)
  76. {
  77.  
  78.  
  79.  
  80.  
  81. printf("Type of ticket (Daily(D) or Yearly(Y)): ");
  82. scanf(" %s", &tickettype);
  83. if (strcmp (tickettype,"D") == 0)
  84. {
  85. HowManyChildren();
  86. HowManyAdults();
  87. HowManySeniors();
  88. printf("Children total cost: %10.2f\n", DailyChild);
  89. printf("Adult total cost: %10.2f\n", DailyAdult);
  90. printf("Senior total cost: %10.2f\n", DailySenior);
  91. }
  92. else if (strcmp (tickettype,"d") == 0)
  93. {
  94. HowManyChildren();
  95. HowManyAdults();
  96. HowManySeniors();
  97. printf("Children total cost: %10.2f\n", DailyChild);
  98. printf("Adult total cost: %10.2f\n", DailyAdult);
  99. printf("Senior total cost: %10.2f\n", DailySenior);
  100. }
  101. else if (strcmp (tickettype,"Daily") == 0)
  102. {
  103. HowManyChildren();
  104. HowManyAdults();
  105. HowManySeniors();
  106. printf("Children total cost: %10.2f\n", DailyChild);
  107. printf("Adult total cost: %10.2f\n", DailyAdult);
  108. printf("Senior total cost: %10.2f\n", DailySenior);
  109. }
  110. else if (strcmp (tickettype,"daily") == 0)
  111. {
  112. HowManyChildren();
  113. HowManyAdults();
  114. HowManySeniors();
  115. printf("Children total cost: %10.2f\n", DailyChild);
  116. printf("Adult total cost: %10.2f\n", DailyAdult);
  117. printf("Senior total cost: %10.2f\n", DailySenior);
  118. }
  119. else if (strcmp (tickettype,"Y") == 0)
  120. {
  121. HowManyChildren();
  122. HowManyAdults();
  123. HowManySeniors();
  124. printf("Children total cost: %10.2f\n", YearlyChild);
  125. printf("Adult total cost: %10.2f\n", YearlyAdult);
  126. printf("Senior total cost: %10.2f\n", YearlySenior);
  127. }
  128. else if (strcmp (tickettype, "y") == 0)
  129. {
  130. HowManyChildren();
  131. HowManyAdults();
  132. HowManySeniors();
  133. printf("Children total cost: %10.2f\n", YearlyChild);
  134. printf("Adult total cost: %10.2f\n", YearlyAdult);
  135. printf("Senior total cost: %10.2f\n", YearlySenior);
  136. }
  137. else if (strcmp (tickettype,"Yearly") == 0)
  138. {
  139. HowManyChildren();
  140. HowManyAdults();
  141. HowManySeniors();
  142. printf("Children total cost: %10.2f\n", YearlyChild);
  143. printf("Adult total cost: %10.2f\n", YearlyAdult);
  144. printf("Senior total cost: %10.2f\n", YearlySenior);
  145. }
  146. else if (strcmp (tickettype,"yearly") == 0)
  147. {
  148. HowManyChildren();
  149. HowManyAdults();
  150. HowManySeniors();
  151. printf("Children total cost: %10.2f\n", YearlyChild);
  152. printf("Adult total cost: %10.2f\n", YearlyAdult);
  153. printf("Senior total cost: %10.2f\n", YearlySenior);
  154. }
  155. else
  156. {
  157. printf("\nIncorrect Input, please try again.\n");
  158. printf("\n\n\n");
  159. GetTicket();
  160. }
  161.  
  162. printf("\n\n");
  163.  
  164. return;
  165. }
  166.  
  167. void HowManyChildren(void)
  168.  
  169. {
  170.  
  171. DailyChild=1.50;
  172.  
  173. YearlyChild = 5.50;
  174.  
  175. printf("\n");
  176. printf("#of Children: ");
  177. scanf(" %d", &childrenNum);
  178. printf("\n");
  179.  
  180. if (childrenNum <= 999)
  181. {
  182. }
  183. else
  184. {
  185. printf("Invalid Number Entered, please try again.");
  186. printf("\n\n\n");
  187. HowManyChildren();
  188. }
  189.  
  190.  
  191. return;
  192. }
  193.  
  194. void HowManyAdults(void)
  195. {
  196.  
  197. DailyAdult = 2.00;
  198. YearlyAdult = 6.50;
  199.  
  200. printf("\n");
  201. printf("#of Adults: ");
  202. scanf(" %d", &adultNum);
  203. printf("\n");
  204.  
  205. if (adultNum <= 999)
  206. {
  207. }
  208. else
  209. {
  210. printf("Invalid Number Entered, please try again.");
  211. printf("\n\n\n");
  212. HowManyAdults();
  213. }
  214.  
  215.  
  216. return;
  217. }
  218.  
  219. void HowManySeniors(void)
  220. {
  221.  
  222. DailySenior = 1.25;
  223. YearlySenior= 3.75;
  224.  
  225. printf("\n");
  226. printf("#of Seniors: ");
  227. scanf(" %d", &seniorNum);
  228. printf("\n");
  229.  
  230. if (seniorNum <= 999)
  231. {
  232. }
  233. else
  234. {
  235. printf("Invalid Number Entered, please try again.");
  236. printf("\n\n\n");
  237. system("pause");
  238. HowManySeniors();
  239. }
  240.  
  241.  
  242.  
  243. return ;
  244.  
  245. }
  246.  
  247. void ChildrenCalculations(void)
  248. {
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257. return;
  258.  
  259. }
Last edited by Ancient Dragon; Aug 4th, 2007 at 6:05 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Aug 4th, 2007
0

Re: Concerning C++ calculations using printf

This isn't C++ code, its C. (The two languages are very different)

Probably the reason that you get an error when your code exits, is that your declaration of main() is ill-formed.
Change your main declaration so that it looks like this -
  1. int main(void)

As for your issue of the wrong output, it looks as if your program doesn't do anything with the number of children after you prompt the user for it.


I think your program has a lot of design issues - you're using alot of global variables, this is one way to make your program very messy and hard to follow. Try rearranging your program so that variables are declared where they're needed, and passed or returned to/from functions instead. You should find that the logic of the program is much easier to trace by doing this.
Last edited by Bench; Aug 4th, 2007 at 5:50 pm.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 4th, 2007
0

Re: Concerning C++ calculations using printf

As far as the arranging of my code, I'll work on that right away. I'm still new to it, and havent gotten any help concerning C programming until now. I'm in a C++ class atm, but it seems we are starting out with C coding first. I'm still trying to figure out how to get the calculations to work in my program. If someone can shed some light on that area for me, it would be greatly appreciated. Thanks again.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Aug 4th, 2007
0

Re: Concerning C++ calculations using printf

Look at this section of code, where you ask the user to enter the number of children
void HowManyChildren(void)
{
	DailyChild=1.50;
	YearlyChild = 5.50;
	
	printf("\n");
	printf("#of Children:   ");
	scanf(" %d", &childrenNum);
	printf("\n");

	if (childrenNum <= 999) 
	{
	}
	else 
	{
	printf("Invalid Number Entered, please try again.");
	printf("\n\n\n");
	HowManyChildren();
	}

	return;
}
After asking the user to input the number of children, your program doesn't do anything else with childrenNum aside from checking that its less than 999.

Somewhere you want to calculate the ticket cost using this number and your cost-per-ticket. Maybe you intended to put that in your ChildrenCalculations() function.



- With regards to C vs C++ - These days, its unusual for a C++ course to use things like printf, scanf, strcmp (They're valid in C++, but only for the sake of compatibility with C). Most modern C++ courses don't teach the language as 'a better C' - although there's nothing wrong with learning both languages, you'll find alot of things done differently when you move onto C++.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Aug 4th, 2007
0

Re: Concerning C++ calculations using printf

I'm still trying to figure out how to get the calculations to work in my program. If someone can shed some light on that area for me, it would be greatly appreciated. Thanks again.
Without trying to discourage you, I have to say your code is full of errors.
My suggestion is that you should work little by little in the steps that
you need to take for you code to work. Compile each of those steps in separate stages and if they work implement them together. That way the bugs would have a harder time compounding.

Let's look at any of your functions. All seems to be suffering of the same problems.

  1. void HowManySeniors(void)
  2. {
  3.  
  4. DailySenior = 1.25;
  5. YearlySenior= 3.75;
  6.  
  7. printf("\n");
  8. printf("#of Seniors: ");
  9. scanf(" %d", &seniorNum);
  10. printf("\n");
  11.  
  12. if (seniorNum <= 999)
  13. {
  14. }
  15. else
  16. {
  17. printf("Invalid Number Entered, please try again.");
  18. printf("\n\n\n");
  19. system("pause");
  20. HowManySeniors();
  21. }
  22.  
  23.  
  24.  
  25. return ;
  26.  
  27. }

DailySenior = 1.25;
YearlySenior= 3.75;

Nothing is using those values inside the function, therefore no reason to be there.
scanf(" %d", &seniorNum);
scanf() is nothing but a lot of grief for any one that uses it. Specially, if you read strings with it. Avoid using that function.
Here's a couple of links that would help you to understand better.

http://www.daniweb.com/tutorials/tutorial45806.htm
http://www.daniweb.com/code/snippet266.htmli
http://www.gidnetwork.com/b-59.html

if (seniorNum <= 999)
{
}

What are you trying to do with that if? That construction is incorrect.
If you want the if() to do nothing it must be something like:
  1. if (seniorNum <= 999) /* if Seniors are less or iquals to 999 */
  2. {
  3. ; /* do nothing */
  4. }

system("pause");
Avoid that too. Read here about it.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 5th, 2007
0

Re: Concerning C++ calculations using printf

Thanks for the info guys. I changed some stuff around in my code and got rid of some of the stuff I dont need. I would use a different code but my instructor wants us to use printf/scanf coding specifically. I'm stil having trouble with getting the calculation to work. I think I'm close though. Also I'm trying to figure out how I can make my program deny letters if its only asking for numbers. For example when it asks for the Customer's Name, I'm trying to make it reject the input if the user were to enter a number for their name, same thing with when it asks for number of children and so forth. I appreciate the help you guys have given me and will try my best to get better. This is the code again below.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream>
  5.  
  6.  
  7.  
  8. void PrintAStarLine(void);
  9. void GetInput(void);
  10. void GetTicket(void);
  11. void HowManyChildren(void);
  12. void HowManyAdults(void);
  13. void HowManySeniors(void);
  14. void TotalCost(void);
  15.  
  16. float DailyChild;
  17. float YearlyChild;
  18. float DailyAdult;
  19. float YearlyAdult;
  20. float DailySenior;
  21. float YearlySenior;
  22.  
  23.  
  24. char customerName[30];
  25. char tickettype[8];
  26.  
  27.  
  28.  
  29. int main(void)
  30. {
  31.  
  32. {
  33. PrintAStarLine();
  34.  
  35. printf(" Home Aquarium Data Entry \n");
  36. printf("\n\n");
  37.  
  38. PrintAStarLine();
  39.  
  40. GetInput();
  41. }
  42. GetTicket();
  43.  
  44. HowManyChildren();
  45.  
  46. HowManyAdults();
  47.  
  48. HowManySeniors();
  49.  
  50. TotalCost();
  51.  
  52.  
  53.  
  54. return 0;
  55. }
  56.  
  57. void PrintAStarLine(void)
  58.  
  59. {
  60.  
  61. printf("**************************************************************************** \n");
  62. printf("\n\n");
  63.  
  64. return;
  65.  
  66. }
  67.  
  68. void GetInput(void)
  69. {
  70.  
  71.  
  72.  
  73. printf("Customer Name (First Middle Last): ");
  74. scanf(" %s %s %s", &customerName);
  75. printf("\n\n\n");
  76.  
  77. return;
  78. }
  79.  
  80.  
  81. void GetTicket(void)
  82. {
  83.  
  84.  
  85.  
  86.  
  87. printf("Type of ticket (Daily(D) or Yearly(Y)): ");
  88. scanf(" %s", &tickettype);
  89. if (strcmp (tickettype,"D") == 0)
  90. {
  91.  
  92. }
  93. else if (strcmp (tickettype,"d") == 0)
  94. {
  95.  
  96. }
  97. else if (strcmp (tickettype,"Daily") == 0)
  98. {
  99.  
  100. }
  101. else if (strcmp (tickettype,"daily") == 0)
  102. {
  103.  
  104. }
  105. else if (strcmp (tickettype,"Y") == 0)
  106. {
  107.  
  108. }
  109. else if (strcmp (tickettype, "y") == 0)
  110. {
  111.  
  112. }
  113. else if (strcmp (tickettype,"Yearly") == 0)
  114. {
  115.  
  116. }
  117. else if (strcmp (tickettype,"yearly") == 0)
  118. {
  119.  
  120. }
  121. else
  122. {
  123. printf("\nIncorrect Input, please try again.\n");
  124. printf("\n\n");
  125. GetTicket();
  126. }
  127.  
  128. printf("\n\n");
  129.  
  130. return;
  131. }
  132.  
  133. void HowManyChildren(void)
  134.  
  135. {
  136.  
  137. DailyChild = 1.50;
  138. YearlyChild = 5.50;
  139.  
  140.  
  141. printf("\n");
  142. printf("#of Children: ");
  143. scanf(" %f", &DailyChild);
  144. printf("\n");
  145.  
  146. if (DailyChild <= 999)
  147. {
  148. ;
  149. }
  150. else
  151. {
  152. printf("Invalid Number Entered, please try again.");
  153. printf("\n\n\n");
  154.  
  155. printf("\n");
  156. printf("#of Children: ");
  157. scanf(" %f", &YearlyChild);
  158. printf("\n");
  159. HowManyChildren();
  160. }
  161.  
  162. if (YearlyChild <= 999)
  163. {
  164. ;
  165. }
  166. else
  167. {
  168. printf("Invalid Number Entered, please try again.");
  169. printf("\n\n\n");
  170. HowManyChildren();
  171. }
  172.  
  173. return;
  174. }
  175.  
  176. void HowManyAdults(void)
  177. {
  178.  
  179. DailyAdult = 2.00;
  180. YearlyAdult = 6.50;
  181.  
  182. printf("\n");
  183. printf("#of Adults: ");
  184. scanf(" %f", &DailyAdult);
  185. printf("\n");
  186.  
  187. if (DailyAdult <= 999)
  188. {
  189. ;
  190. }
  191. else
  192. {
  193. printf("Invalid Number Entered, please try again.");
  194. printf("\n\n\n");
  195. HowManyAdults();
  196. }
  197.  
  198.  
  199. return;
  200. }
  201.  
  202. void HowManySeniors(void)
  203. {
  204.  
  205. DailySenior = 1.25;
  206. YearlySenior= 3.75;
  207.  
  208. printf("\n");
  209. printf("#of Seniors: ");
  210. scanf(" %f", &DailySenior);
  211. printf("\n");
  212.  
  213. if (DailySenior <= 999)
  214. {
  215. ;
  216. }
  217. else
  218. {
  219. printf("Invalid Number Entered, please try again.");
  220. printf("\n\n\n");
  221. system("PAUSE");
  222. HowManySeniors();
  223. }
  224.  
  225.  
  226.  
  227. return ;
  228.  
  229. }
  230.  
  231. void TotalCost(void)
  232.  
  233. {
  234. printf("Children total cost: %10.2f\n", DailyChild);
  235. printf("Adult total cost: %10.2f\n", DailyAdult);
  236. printf("Senior total cost: %10.2f\n", DailySenior);
  237.  
  238. return ;
  239.  
  240. }
Last edited by Ancient Dragon; Aug 5th, 2007 at 1:02 am. Reason: add code tags -- please start using them
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Aug 5th, 2007
0

Re: Concerning C++ calculations using printf

>I would use a different code but my instructor wants us to use printf/scanf coding specifically

Well, if you must use scanf() let's learn about it correctly, shall we?
Let's look at this function, as an example:

  1. void GetInput(void)
  2. {
  3. printf("Customer Name (First Middle Last): ");
  4. scanf(" %s %s %s", &customerName);
  5. printf("\n\n\n");
  6. return;
  7. }

scanf(" %s %s %s", &customerName);
There's not chance this is going to fly.
First: You are trying to read a string. The proper arguments for that are:
scanf( "%s", customerName ); you don't want the & operator there. & is only for integers and solo chars.
I suppose you could write something like:
scanf( "%s%s%s", first_array, second_array, third_array );
passing to it three different arrays but don't do it.
scanf() stops reading from the buffer as soon as encounters a space.
That implies that if the user enters name, middle name and last name. You code goes where many codes has gone before.
What can you do?. One way is to tell scanf how much you want to read. It is not pretty but doable.
scanf( "%[^\n]", customerName ); This statement tells scanf to read until it encounters a newline. But it leaves that newline behind in the stdin buffer.
To pick that newline we could write it like:

char newline;

scanf( "%[^\n]%c", customerName, &newline );

Don't mention to anyone I told you so.
Last edited by Aia; Aug 5th, 2007 at 1:47 am.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 5th, 2007
0

Re: Concerning C++ calculations using printf

Thanks alot. Not only did that help the name problem I had but it also took care of the error that I had aswell. Didn't know why I kept getting the error until you explained it. Now just to figure out this calculation problem and I'm pretty much on my way to success!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
NycNessyness is offline Offline
58 posts
since Aug 2007
Aug 5th, 2007
0

Re: Concerning C++ calculations using printf

>I'm pretty much on my way to success!
I am glad of that optimism. Even when you become famous, remember that you could stop by at anytime.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Aug 5th, 2007
0

Re: Concerning C++ calculations using printf

>Now just to figure out this calculation problem
I think I know what your want to do, but I am not sure.
Perhaps this piece of code will demostrate an example of how to do it.
  1. /*
  2.  * HomeDepot.c
  3.  * Shows how to match item with price.
  4.  */
  5. #include <stdio.h>
  6.  
  7. float price( const float item )
  8. {
  9. int piece = 0;
  10. char newline;
  11.  
  12. scanf( "%d%c", &piece, &newline ); /* amount of items */
  13.  
  14. return item * piece;
  15. }
  16.  
  17. int main( void )
  18. {
  19. const float screw = .50; /* this never should change */
  20. const float nail = .25; /* this never should change */
  21. float price_screws = 0.00; /* final total price items */
  22. float price_nails = 0.00; /* final total price items */
  23.  
  24.  
  25. /* ask for nails */
  26. printf( " Enter amount of nails: " );
  27. fflush( stdout ); /* refresh the screen */
  28.  
  29. /* get & compute the total price of nails */
  30. price_nails = price( nail );
  31.  
  32. /* ask for screws */
  33. printf( "Enter amount of screws: " );
  34. fflush( stdout ); /* refresh the screen */
  35.  
  36. /* get & compute the total price of screws */
  37. price_screws = price( screw );
  38.  
  39. /* final display */
  40. puts( "\n\tYour total order" );
  41. puts( "\t================\n");
  42. printf( " Price for nails: %6.2f\n", price_nails );
  43. printf( "Price for screws: %6.2f\n", price_screws );
  44. printf( " Grand total: %6.2f\n", price_nails + price_screws );
  45.  
  46. getchar();
  47. return 0;
  48. }
  49. /*
  50.  my input / output.
  51.   Enter amount of nails: 34
  52. Enter amount of screws: 23
  53.  
  54.   Your total order
  55.   ================
  56.  
  57.  Price for nails: $ 8.50
  58. Price for screws: $ 11.50
  59.   Grand total: $ 20.00
  60.  
  61. */
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Delay
Next Thread in C Forum Timeline: Computer Oriented Numerical Methods





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC