944,137 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1029
  • C++ RSS
Oct 10th, 2007
0

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

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jerdawg89 is offline Offline
2 posts
since Oct 2007
Oct 10th, 2007
0

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

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 11th, 2007
0

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

anymore help also i have no idea what prototype is because we haven't learned it yet

>>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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jerdawg89 is offline Offline
2 posts
since Oct 2007
Oct 11th, 2007
0

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

>anymore help also i have no idea what prototype is because we haven't learned it yet
This is a function definition:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

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: binary file into class using dynamic arrays
Next Thread in C++ Forum Timeline: I need the helps, the deadline is coming soon...





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


Follow us on Twitter


© 2011 DaniWeb® LLC