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.

#include <iostream>
#include <string>                // do not remove


/********************************************************************
*                        User Include Files                         *
********************************************************************/
#include "sapi.h"                // do not remove
using namespace std;


/********************************************************************
*                      User Defined Constants                       *
********************************************************************/


/********************************************************************
*                        Function Prototypes                        *
********************************************************************/
void speech(string texttovoice);


/********************************************************************
* Function name: main                                               *
* Date Written :                                                    *
*                                                                   *
* Purpose:  Provides an entry point to our program.                 *
*                                                                   *
* Arguments: NONE                                                   *
*                                                                   *
* Return Value: integer                                             *
*                                                                   *
********************************************************************/
int main ()
{
int money, onedollar, fiftycent, twentycent, tencent, fivecent, purchase, payment, penny;
Speech ("Enter the amount of purchase \n");
cin << purchase;
speech ("Enter the amount of payment \n");
cin << payment;
change(money, onedollar, twentyfivecent, tencent, fivecent, penny);
cout << "One Dollar: " << onedollar << endl;

		cout << "twentyfive Cent: " << twentyfivecent << endl;
		cout << "Ten Cent: " << tencent << endl;
		cout << "five Cent: " << fivecent << endl;
		cout << "pennys: " << penny << endl;
	return 0;
}
int change(float money, int onedollar, int twentyfivecent, int tencent, int fivecent, int penny);
{
	float temp;

	temp = payment - purchase;

	temp *= 100;
	speech ("onedollar = temp/100");
	temp = temp%100;
	speech ("twentyfivecent = temp/25");
	temp = temp%25;
	speech("tencent = temp/10");
	temp = temp%10;
	speech( "fivecent = temp%5");
	temp = temp/5;	
	speech( "penny = temp/1");

	return 0;

}

/********************************************************************
* Function name: speech                                             *
* Date Written : 9/2/2005                                           *
*                                                                   *
* Purpose:  To take a string of characters that has been passed in  *
*           as arguments and output them to the screen and the      *
*           speakers.  Will only allow 1000 characters to be passed *
*           in to form a word/sentence.                             *
*                                                                   *
* Arguments: texttovoice    the string to be outputted to the       *
*                           screen and the speakers                 *
*                                                                   *
* Return Value: NONE                                                *
*                                                                   *
* Written by: Dr Toni Logar and Roger Schrader                      *
*                                                                   *
********************************************************************/
void speech( string texttovoice)
{
    int i;                     // index used for conversion 
    WCHAR wstring[1000];       // used to hold convert string
    ISpVoice* Voice = NULL;    // The voice interface

    // convert the string to a wide character string array
    // and NULL terminate the string
    i = 0;
    while( i < (int)texttovoice.size() && i < 999)
    {
       wstring[i] = texttovoice[i];
       i++;
    }
    wstring[i]='\0';

    // Initialize COM
    CoInitialize ( NULL );

    // Create the voice interface object
    CoCreateInstance ( CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, 
                     (void**)&Voice );

    // Speak! Output the words to the screen
    Voice->Speak(wstring,SPF_DEFAULT,NULL);

    // Shutdown the voice
    if ( Voice != NULL ) 
        Voice -> Release (); 
    
    Voice = NULL;

    // Shutdown COM
    CoUninitialize ();

    // Output the word to the screen
    cout << texttovoice;
}

Dani AI

Generated

Short summary and a clear fix path. The visible problems are mostly: using the wrong stream operator when reading input, calling a function before it’s declared/defined, mismatched function parameters, and doing currency math with raw floating-point values (which leads to rounding headaches). already pointed out the extraction operator and types; gave a good primer on prototypes. Below is a practical, safer design you can apply immediately.

Convert dollars to integer cents as soon as you read them, and do all arithmetic in integers. Provide a small type to hold the breakdown and a function that returns it (declare that function before main so the compiler knows its signature). This avoids floating-point rounding surprises and makes the change calculation trivial and reliable.

Example (new, compact) implementation idea:

#include <iostream>
#include <cmath>

struct Change { int dollars, quarters, dimes, nickels, pennies; };

Change computeChange(int purchaseCents, int paymentCents)
{
    Change c = {0,0,0,0,0};
    int diff = paymentCents - purchaseCents;
    if (diff <= 0) return c; // handle exact or insufficient payment as needed

    c.dollars  = diff / 100; diff %= 100;
    c.quarters = diff / 25;  diff %= 25;
    c.dimes    = diff / 10;  diff %= 10;
    c.nickels  = diff / 5;   diff %= 5;
    c.pennies  = diff;
    return c;
}

Usage notes and debugging tips: read inputs into double (or parse a string), convert with int cents = static_cast<int>(std::lround(amount * 100.0));, then call computeChange. Put a prototype or the full definition of computeChange above main. Compile with warnings enabled (g++ -Wall or MSVC /W4) to catch operator/typo errors. If you keep the speech/SAPI code, create and initialize the voice once and reuse it rather than calling CoInitialize/CoUninitialize on every speak—this reduces runtime overhead and avoids COM misuse.

Recommended Answers

All 3 Replies

>>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.

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.

>anymore help also i have no idea what prototype is because we haven't learned it yet
This is a function definition:

int change(float money, int onedollar, int twentyfivecent, int tencent, int fivecent, int penny)
{
  float temp;

  // ...

  return 0;
}

Note that it has no semicolon after the argument list but it defines a body. This is a prototype:

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:

int main()
{
  foo();
}

int foo()
{
  return 0;
}

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):

int foo()
{
  return 0;
}

int main()
{
  foo();
}

But that's not always practical. This is where prototypes come in:

int foo();

int main()
{
  foo();
}

int foo()
{
  return 0;
}

Now foo is declared before you use it, and you can still define it wherever you want.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.