trying to make a program to say the change due back after a purchase is made

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 2
Reputation: jerdawg89 is an unknown quantity at this point 
Solved Threads: 0
jerdawg89 jerdawg89 is offline Offline
Newbie Poster

trying to make a program to say the change due back after a purchase is made

 
0
  #1
Oct 10th, 2007
trying to make a program to say the change due back after a purchase is made.
this is what i have so far but it doesn't want to work so i really could use some help please.
  1. #include <iostream>
  2. #include <string> // do not remove
  3.  
  4.  
  5. /********************************************************************
  6. * User Include Files *
  7. ********************************************************************/
  8. #include "sapi.h" // do not remove
  9. using namespace std;
  10.  
  11.  
  12. /********************************************************************
  13. * User Defined Constants *
  14. ********************************************************************/
  15.  
  16.  
  17. /********************************************************************
  18. * Function Prototypes *
  19. ********************************************************************/
  20. void speech(string texttovoice);
  21.  
  22.  
  23. /********************************************************************
  24. * Function name: main *
  25. * Date Written : *
  26. * *
  27. * Purpose: Provides an entry point to our program. *
  28. * *
  29. * Arguments: NONE *
  30. * *
  31. * Return Value: integer *
  32. * *
  33. ********************************************************************/
  34. int main ()
  35. {
  36. int money, onedollar, fiftycent, twentycent, tencent, fivecent, purchase, payment, penny;
  37. Speech ("Enter the amount of purchase \n");
  38. cin << purchase;
  39. speech ("Enter the amount of payment \n");
  40. cin << payment;
  41. change(money, onedollar, twentyfivecent, tencent, fivecent, penny);
  42. cout << "One Dollar: " << onedollar << endl;
  43.  
  44. cout << "twentyfive Cent: " << twentyfivecent << endl;
  45. cout << "Ten Cent: " << tencent << endl;
  46. cout << "five Cent: " << fivecent << endl;
  47. cout << "pennys: " << penny << endl;
  48. return 0;
  49. }
  50. int change(float money, int onedollar, int twentyfivecent, int tencent, int fivecent, int penny);
  51. {
  52. float temp;
  53.  
  54. temp = payment - purchase;
  55.  
  56. temp *= 100;
  57. speech ("onedollar = temp/100");
  58. temp = temp%100;
  59. speech ("twentyfivecent = temp/25");
  60. temp = temp%25;
  61. speech("tencent = temp/10");
  62. temp = temp%10;
  63. speech( "fivecent = temp%5");
  64. temp = temp/5;
  65. speech( "penny = temp/1");
  66.  
  67. return 0;
  68.  
  69. }
  70.  
  71. /********************************************************************
  72. * Function name: speech *
  73. * Date Written : 9/2/2005 *
  74. * *
  75. * Purpose: To take a string of characters that has been passed in *
  76. * as arguments and output them to the screen and the *
  77. * speakers. Will only allow 1000 characters to be passed *
  78. * in to form a word/sentence. *
  79. * *
  80. * Arguments: texttovoice the string to be outputted to the *
  81. * screen and the speakers *
  82. * *
  83. * Return Value: NONE *
  84. * *
  85. * Written by: Dr Toni Logar and Roger Schrader *
  86. * *
  87. ********************************************************************/
  88. void speech( string texttovoice)
  89. {
  90. int i; // index used for conversion
  91. WCHAR wstring[1000]; // used to hold convert string
  92. ISpVoice* Voice = NULL; // The voice interface
  93.  
  94. // convert the string to a wide character string array
  95. // and NULL terminate the string
  96. i = 0;
  97. while( i < (int)texttovoice.size() && i < 999)
  98. {
  99. wstring[i] = texttovoice[i];
  100. i++;
  101. }
  102. wstring[i]='\0';
  103.  
  104. // Initialize COM
  105. CoInitialize ( NULL );
  106.  
  107. // Create the voice interface object
  108. CoCreateInstance ( CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice,
  109. (void**)&Voice );
  110.  
  111. // Speak! Output the words to the screen
  112. Voice->Speak(wstring,SPF_DEFAULT,NULL);
  113.  
  114. // Shutdown the voice
  115. if ( Voice != NULL )
  116. Voice -> Release ();
  117.  
  118. Voice = NULL;
  119.  
  120. // Shutdown COM
  121. CoUninitialize ();
  122.  
  123. // Output the word to the screen
  124. cout << texttovoice;
  125. }
Last edited by Ancient Dragon; Oct 10th, 2007 at 11:31 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
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: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: trying to make a program to say the change due back after a purchase is made

 
0
  #2
Oct 10th, 2007
>>but it doesn't want to work
by that I guess you mean it will not compile. What are the compile errors? What lines won't compile?

I know for a fact that lines 38 and 40 are wrong. -- using << instead of >>

also purchase and payment variables should probably be floats not ints

Another obvious problem -- you did not prototype function change(). It must be prototyped at the top of the program if you call it before it is coded. Just like variables have to be declared before used, functions have to be prototyped (or coded) before called.
Last edited by Ancient Dragon; Oct 10th, 2007 at 11:40 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: Oct 2007
Posts: 2
Reputation: jerdawg89 is an unknown quantity at this point 
Solved Threads: 0
jerdawg89 jerdawg89 is offline Offline
Newbie Poster

Re: trying to make a program to say the change due back after a purchase is made

 
0
  #3
Oct 11th, 2007
anymore help also i have no idea what prototype is because we haven't learned it yet

Originally Posted by Ancient Dragon View Post
>>but it doesn't want to work
by that I guess you mean it will not compile. What are the compile errors? What lines won't compile?

I know for a fact that lines 38 and 40 are wrong. -- using << instead of >>

also purchase and payment variables should probably be floats not ints

Another obvious problem -- you did not prototype function change(). It must be prototyped at the top of the program if you call it before it is coded. Just like variables have to be declared before used, functions have to be prototyped (or coded) before called.
Last edited by jerdawg89; Oct 11th, 2007 at 1:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: trying to make a program to say the change due back after a purchase is made

 
0
  #4
Oct 11th, 2007
>anymore help also i have no idea what prototype is because we haven't learned it yet
This is a function definition:
  1. int change(float money, int onedollar, int twentyfivecent, int tencent, int fivecent, int penny)
  2. {
  3. float temp;
  4.  
  5. // ...
  6.  
  7. return 0;
  8. }
Note that it has no semicolon after the argument list but it defines a body. This is a prototype:
  1. int change(float money, int onedollar, int twentyfivecent, int tencent, int fivecent, int penny);
Notice that it has no body, but it does have a semicolon after the argument list. A prototype is designed to declare the name, return type, and parameters of a function so that you can use it without having to define it first. The golden rule is that everything has to be declared before you can use it. That means this is very wrong, and shouldn't compile:
  1. int main()
  2. {
  3. foo();
  4. }
  5.  
  6. int foo()
  7. {
  8. return 0;
  9. }
Why? Because foo wasn't declared before you used it. You can fix the problem by moving the definition before the use (because a definition is also a declaration):
  1. int foo()
  2. {
  3. return 0;
  4. }
  5.  
  6. int main()
  7. {
  8. foo();
  9. }
But that's not always practical. This is where prototypes come in:
  1. int foo();
  2.  
  3. int main()
  4. {
  5. foo();
  6. }
  7.  
  8. int foo()
  9. {
  10. return 0;
  11. }
Now foo is declared before you use it, and you can still define it wherever you want.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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