Yep. I have no life. Here's the simplest and stupidest form of C++ coding ever known to man kind...the change counter.
What does it do? Count change :-)
Yep. I have no life. Here's the simplest and stupidest form of C++ coding ever known to man kind...the change counter.
What does it do? Count change :-)
#include <iostream.h>
#include <conio.h>
#include <math.h>
int main ()
{
int halfdollar = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0;
cout << "Change Calculator \n \n";
cout << "How many half dollars do you have? \n ";
cin >> halfdollar;
cout << "How many quarters do you have? \n ";
cin >> quarters;
cout << "How many dimes do you have? \n ";
cin >> dimes;
cout << "How many nickels do you have? \n ";
cin >> nickels;
cout << "How many pennies do you have? \n ";
cin >> pennies;
cout << "\n You have a total of : $" << float (((halfdollar * 50) + (quarters * 25) + (dimes * 10) + (nickels * 5) + (pennies * 1))) / 100;
getch();
return 0;
} Quick expert addendum to 's example. The original is a perfectly fine minimal demo, but a few modernizations make it safer and more portable: use standard headers (not legacy <iostream.h>), avoid nonportable <conio.h>/getch(), treat money as integer cents (not float), validate input, and print cents with two digits. A compact, robust version that applies those fixes is shown below.
#include <iostream>
#include <iomanip>
#include <limits>
#include <cstdint>
#include <string>
void read_nonneg(const std::string &prompt, std::int64_t &v) {
while (true) {
std::cout << prompt;
if (!(std::cin >> v)) {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Invalid input; enter a nonnegative integer.\n";
continue;
}
if (v < 0) {
std::cout << "Negative counts are not allowed.\n";
continue;
}
break;
}
}
int main() {
std::int64_t halfdollar=0, quarters=0, dimes=0, nickels=0, pennies=0;
read_nonneg("How many half dollars do you have? ", halfdollar);
read_nonneg("How many quarters do you have? ", quarters);
read_nonneg("How many dimes do you have? ", dimes);
read_nonneg("How many nickels do you have? ", nickels);
read_nonneg("How many pennies do you have? ", pennies);
std::int64_t total_cents = halfdollar*50 + quarters*25 + dimes*10 + nickels*5 + pennies;
std::int64_t dollars = total_cents / 100;
std::int64_t cents = total_cents % 100;
std::cout << "\nTotal: $" << dollars << '.' << std::setw(2) << std::setfill('0') << cents << '\n';
return 0;
} Notes and troubleshooting: integer arithmetic avoids rounding surprises that float/double can produce when representing currency. std::int64_t handles extremely large counts; fall back to long long if <cstdint> is unavailable. Input validation prevents malformed input from leaving the program in a bad state. For a shorter implementation, store coin values in an array and loop over prompts; for internationalized currency formatting, consider locale-aware facilities instead of manual formatting.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.