Hi guys, i'm new here and i figured what better place to be asking C question than a place dedicated to it... i'm not a total n00b, i just can't write code at like i write english, but i can fluently understand it, i've done and still do programming in many languages and after i decided to go back to university and having to retake C (something i did yrs ago B4 i dropped out) i'm feeling a little rusty and kinda outta shape, reading the class notes obviously helps but u know when u know more and it's not that that's stopping you... anyhow a little FYI about me just so i don't automatically get blasted into smithereens. and before i state my issue i'd like to say thanks to whoever takes the time to read this and hopefully i might get some insight.
so here's the deal...
an amount of change (money to be returned on purchase) is to be inputted by the user, between 0, inclusive and 5 non-inclusive (0<=change<5)
on enter, the program prints out, your change is composed of toonies (yes canadian money :P), loonies, quarters... etc pennies.
Now that's simple, it's been done and i'm getting good answers... but also bad ones...
say for example i put in 4.99 for change... my change is missing a penny... and only for some numbers am i getting a -1 on the pennies...
i'll explain my code before i paste it too, if the comments lack.
i decided to go with the "multiply by 100" to get an all pennies amount and worked my way down with while loops through 200/100/25/10/5/1 cents
these while loops were basically counting my coins and subtracting the amounts from the total cents.
another way would definitely be with mod(% i'm not sure which is short/faster
here's the code:
Code:
/*
#include <stdio.h>
main ()
{ // initialisation des variables
float monnaie; // Nombre entré par usager
int cents=0; // variable utilisée pour les calculs
int validation;
char condition;
// Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
do
{
int toonies=0;
int loonies=0;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
// Nbr de pièces
do // boucle de validation (monnaie entre 0 inclus et 5 non inclus)
{
printf("Entrez le montant a remettre: (x.xx)"); // Entrée du change a remettre
fflush(stdin); // vider la memoire temp.
scanf("%f", monnaie);
validation = ( monnaie >= 0 && monnaie < 5);
if (!validation) // si pas vrai
printf ("Montant de change non valide! SVP le retaper. \n");
} while (!validation); // tant que non valide
// Calculs
cents = monnaie * 100; // pour faciliter les calculs
while (cents >= 200) {
toonies++;
cents -= 200;
}
while (cents >= 100) {
loonies++;
cents -= 100;
}
while (cents >= 25) {
quarters++;
cents -= 25;
}
while (cents >= 10) {
dimes++;
cents -= 10;
}
while (cents >= 5) {
nickels++;
cents -= 5;
}
while (cents >=1) {
cents++;
cents-=1;
}
// Affichage des resultats
printf ("Vous aurez: \n");
printf ("%d pieces de 2$, \n", toonies);
printf ("%d pieces de 1$, \n", loonies);
printf ("%d pieces de 25c, \n", quarters);
printf ("%d pieces de 10c, \n", dimes);
printf ("%d pieces de 5c \n", nickels);
printf ("et \n%d pieces de 1c", pennies);
// Condition requise pour repartir du début
printf ("\nVoulez vous faire un autre calcul, (o/n)?");
fflush (stdin); // Fonction pour vider stdin
condition = toupper(getchar()); // Capitaliser la letter et soumettre à condition
} while ( condition == 'O'); // Repartir du début si O
system("pause") ;
}
/*
Résultats:
==========
*/
Again, thanks... this is greatly appreciated.