Hey guys, I'm not really a beginner in this, i can read and write code with enough ease but this is a whole new thing for me... i've written some code to give me the exact number of change (coins) when giving a number between 0 (inclusive) and 5 (non-inclusive)
the problem i'm having is, for example, when i put certain numbers, i'm getting a wrong count of change... off by 1 penny only
it's very bizarre... -
-
#include <stdio.h>
-
-
main()
-
-
{
-
// initialisation des variables
-
int nbrMax; // Nbr d'entiers à saisir
-
int tableauNbr[nbrMax-1]; // Tableau pour contenir les nombres entrés par l'usager
-
int i; // Compteur boucle
-
int j, k, somme; // variables pour calcul nbr parfait
-
char condition; // Oui ou Non
-
-
// Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
-
do
-
{
-
printf ("Entrez le nombre d'entier (Max 10):\n");
-
scanf ("%d", &nbrMax);
-
-
// boucle pour la saisie des nombres et remplissage le tableau.
-
for (i = 0; i < nbrMax; i++)
-
{
-
scanf ("%d", &tableauNbr[i]);
-
}
-
-
// Calculs pour trouver les nombres parfaits
-
somme = 0;
-
for (j = 0; j < nbrMax; j++)
-
{
-
for (k = 1; k < tableauNbr[j]; k++)
-
{
-
if(tableauNbr[j]%k==0)
-
{
-
somme += k;
-
}
-
}
-
// Affichage des resultats
-
if (somme = tableauNbr[j])
-
printf("%d est un nombre parfait\n", tableauNbr[j]);
-
else
-
printf("%d n'est pas un nombre parfait\n", tableauNbr[j]);
-
-
}
-
-
-
-
-
// 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") ;
-
}
-
I hope some1 can give me some insight on this
Thanks in advance
Marc
7 2091
oh wow i am an idiot... pasted the wrong code....
here's the actual one -
#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 = toInt(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") ;
-
}
-
Banfa 9,065
Expert Mod 8TB
Main returns int... ALWAYS. - fflush(stdin); // vider la memoire temp.
-
scanf("%f", monnaie);
Don't fflush any input stream or any input/output stream where the last operation was an input. The result is undefined and therefore non-portable read this. You may also want to follow the link and read about why you shouldn't use scanf as well. - cents = toInt(monnaie * 100); // pour faciliter les calculs
I don't know what your toInt function does but unless it takes account of rounding errors you can get when using floating point then this line is almost certainly where your calculation is going out by 1. Try adding 0.5
This is very poor style and in a utility program a pain as the program could not be called without manual intervention. Most people who put this in do so because they do not properly know how their IDE works. They think that when they run the program from the IDE it does not stop so they can't see the output. In reality with most IDEs when you debug the program it does not stop but if you are debugging the assumption is that you are going to have a break point in the program to stop it. If you actually run the program (rather than debugging it) then the console stops at the end of program execution without this line.
On MSVC debug-run is F5 but run is CTRL-F5
I haven't seen change done this way. Usually, you see for example: 1234 -
int dollars = money / 100;
-
money = money - dollars * 100;
-
-
int quarters = money / 25;
-
money = money - quarters * 25;
-
-
etc..
-
Main returns int... ALWAYS.
agreed - fflush(stdin); // vider la memoire temp.
-
scanf("%f", monnaie);
Don't fflush any input stream or any input/output stream where the last operation was an input. The result is undefined and therefore non-portable read this. You may also want to follow the link and read about why you shouldn't use scanf as well.
had to use fflush because when a number outside the range was inputted, that number stuck in memory and gave erroneous change results - cents = toInt(monnaie * 100); // pour faciliter les calculs
I don't know what your toInt function does but unless it takes account of rounding errors you can get when using floating point then this line is almost certainly where your calculation is going out by 1. Try adding 0.5
i realized after my post that I was using an imaginary function.... and the 0.5, as i've looked it up, was the solution given to float to int conversions, oh didn't i mention i was really rusty...
This is very poor style and in a utility program a pain as the program could not be called without manual intervention. Most people who put this in do so because they do not properly know how their IDE works. They think that when they run the program from the IDE it does not stop so they can't see the output. In reality with most IDEs when you debug the program it does not stop but if you are debugging the assumption is that you are going to have a break point in the program to stop it. If you actually run the program (rather than debugging it) then the console stops at the end of program execution without this line.
that line was added by Dev-C++ and i don't think the teacher knew what he was talking about when he explained it. now it makes all the sense.
but maybe for the purpose of the class and the programs being simple....
any how thanks again, it's been a great help
I haven't seen change done this way. Usually, you see for example: 1234 -
int dollars = money / 100;
-
money = money - dollars * 100;
-
-
int quarters = money / 25;
-
money = money - quarters * 25;
-
-
etc..
-
it's canadian dollars and we have 2 and 1 dollar coins... :P
There's more; look here near the start of main(): -
int nbrMax; // Nbr d'entiers à saisir
-
int tableauNbr[nbrMax-1]; // Tableau pour contenir les nombres entrés par l'usager
-
nbrMax isn't initialized (yet).
kind regards,
Jos
If i set nbrMax to 0 there and get the user input affected to it... should work right?
(In addition to what has been said)
that should be Sign in to post your reply or Sign up for a free account.
Similar topics
by: Aspersion |
last post by:
I'm building an ASP page that has a lot of text and graphics.
There is a calculation facility on the page. The user enters several
numbers in a form and presses a button to see the calculated...
|
by: anaxamandr |
last post by:
Hi.
I have a long loop in ASP that performs a rather lengthy calculation. I would love for my users to be able to stop that calculation, if they so choose, mid way through the process.
I attempted...
|
by: Del |
last post by:
Thanks in advance for any help.
I have a database that was created in Access 2000. Several users have
been upgraded to Access 2003. Since upgrading to 2003 we have noticed
that some of the...
|
by: Michiel Alsters |
last post by:
Hello everybody,
I hope anybody can help me. I'll try to give a brief overview of my
problem. I have running a program that performs a heavy calculation.
To give the user feedback what the...
|
by: Gordon |
last post by:
Hi there,
I have a VB.NET application which opens a new Excel workbook (and obviously
worksheet). The cell values in the worksheet depend on certain formulae,
therefore the sheet Calculate event...
|
by: cassbiz |
last post by:
I may be in the wrong forum so Ronald don't shoot :)
In my code I have an option box to choose a number - works fine. I want to carry over the new value to another field to do a recalculation.
...
|
by: gregincolumbus |
last post by:
I am trying to get the financial calculation on this to trigger whenever there is a change to select1. Right now, the user has to click on select2 to trigger the changes.
Ideally, a change of...
|
by: mattmao |
last post by:
Okay, I was asked by a friend about the result of this limit:
http://bbs.newwise.com/attdata/forumid_14/20070922_fe7f77c81050413a20fbDWYOGm7zeRj3.jpg
Not n->zero but n-> + infinite
I really...
|
by: mlwerth |
last post by:
Dear Access Group:
This is the most basic and most embarrassing of questions, but I
cannot find where to change the data type of a text field that I have
in Access 2003 to a number field. I've...
|
by: Lara1 |
last post by:
I'm trying to write a procedure that will enable button on a sheet (and colour it red), but ONLY when changes are made to cells in column E of that sheet (from row 10 to the bottom of the...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |