I am trying to learn C++ by myself using a book and want to work through everything it says. I am on a program that you have to write kinda like a cash register program that tells you how many 5's, 1's, quarters, dimes, nickles, and pennies you should get back. It wants you to create a seperate function to do the calculations. I am really lost. I have bits and pieces, but I don't know what to do. I've been going on this for about 6 hours now trying to figure stuff out and ended up just starting over a bunch of times cause it was just completely wrong. The code I have is below, but I don't know what to do from here. Any help would be GREATLY appreciated. Thank you so much.

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
float calcchange(float, float, float, float, float, float, float);  //function prototype
int main ()

{
	float  ftotal = 0,
		   fcents = 0,
		   fchange = 0,
           ffives = 0,
		   fones= 0,
		   fquarters = 0,
		   fdimes = 0,
		   fnickles = 0,
		   fpennies = 0;
		  

	cout << "   ~~~~~~~~Payroll Calculator ~~~~~~~~~~" << endl;
	cout << "Enter the total price: ";
	cin >> ftotal;
	
	


	//fpay = fhours * frate;
	fchange = calcPay(ftotal,ffives,fones,iquarters,);  // call function and send to arguments
	cout << "The total pay is $" << setprecision(2) << fixed
		 << fpay << endl; 

	cout << "any key..." << endl;
	_getch();
	return 0;
}
float calcPay(float h, float r)
{
	// compute the change
    while (cents >= 25) {
          quarters++;
          cents -= 25;
    }
    while (cents >= 10) {
          dimes++;
          cents -= 10;
    }
    while (cents >= 5) {
          nickels++;
          cents -= 5;
    }
    while (cents >= 1) {
          pennies++;
          cents -= 1;
    }
}

Dani AI

Generated

A compact, tested plan that fixes the common problems in the thread: treat money as integer cents (avoid floats for money), read both price and amount paid, compute change in cents with rounding, and compute each denomination by integer division and modulo. Give the calculation a simple, well-documented interface: either return a small struct of counts or accept explicit references. Keep names consistent (the OP had mismatched calcchange / calcPay) and avoid nonstandard headers like <conio.h>.

#include <iostream>
#include <cmath>

struct Change { int fives, ones, quarters, dimes, nickels, pennies; };

Change calcChange(int cents) {
    Change r{0,0,0,0,0,0};
    if (cents <= 0) return r;
    r.fives    = cents / 500; cents %= 500;
    r.ones     = cents / 100; cents %= 100;
    r.quarters = cents / 25;  cents %= 25;
    r.dimes    = cents / 10;  cents %= 10;
    r.nickels  = cents / 5;   cents %= 5;
    r.pennies  = cents;
    return r;
}

int main() {
    double price, paid;
    std::cin >> price >> paid;
    int changeCents = static_cast<int>(std::round((paid - price) * 100.0));
    if (changeCents < 0) { std::cout << "Insufficient payment\n"; return 0; }
    Change c = calcChange(changeCents);
    std::cout << "$5: " << c.fives << "  $1: " << c.ones << "  q: " << c.quarters
              << "  d: " << c.dimes << "  n: " << c.nickels << "  p: " << c.pennies << '\n';
}

Notes and troubleshooting: 's pass-by-reference idea works, but returning a small struct keeps the function easier to test and reduces side effects. 's reminder to plan first is important: sketch the steps (convert to cents → check negative → do divisions/modulos) before coding. Watch floating-point rounding (use std::round or accept cents as integer input), initialize all variables, and keep function names/prototypes consistent. Test with simple cases (for example: price 1.15 paid 5.00 → change 3.85 → 0 fives, 3 ones, 3 quarters, 1 dime, 0 nickels, 0 pennies) to confirm correctness.

Recommended Answers

All 4 Replies

Your calcPay function is never prototyped, so you can't call it from main (you do have a prototype for calcchange(), a function that is never declared, though).

Also, you never use the h and r parameters passed into calcPay. This isn't a syntax error, but it is probably not what you want to do.

And last of all, the values quarters, cents, dimes, nickels, and pennies, are not initialized inside off calcPay (or anywhere for that matter). And fpay in main is also not initialized or declared.

Your calcPay function is never prototyped, so you can't call it from main (you do have a prototype for calcchange(), a function that is never declared, though).

Also, you never use the h and r parameters passed into calcPay. This isn't a syntax error, but it is probably not what you want to do.

And last of all, the values quarters, cents, dimes, nickels, and pennies, are not initialized inside off calcPay (or anywhere for that matter)

Ya, I know, I had stuff in there and then I deleted it and then started looking at doing other stuff and I'm just so confused, I don't even know where to start on this program. I'm just lost and it seems like I'm in a black hole, so there is no light shining in any direction. If somebody could just kinda show me the direction I need to go even that would be a huge help. Thanks.

I would pass quarters, cents, dimes, nickels, and pennies into calcPay as a refrence, like float calcPay(float& quarters, float& cents, float& dimes, float& nickels, float& pennies); , then you should be able to modify them correctly in calcPay.

1) How can you have 2.4 dimes? Or 3.7 quarters? The values of the denominations only need to be integers.

2) Have you spent 6 hours sitting in front of the computer just whacking at the code hoping you get the right answer? If so, you are missing the most important part of programming. Writing out how to do what you need to do, changing it until you understand the process completely. Only then should you sit in front of the computer.

Writing a program is like taking a trip. The computer work is the driving part of the trip. But if you don't plan the route to where you are going, how can you ultimately reach the destination?

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.