Hello again! This is a program that I made yesterday. I have to be able to calculate the amount of change in quarters, dimes, nickles and pennies out of $1. This is what I have so far, but when I run the program some numbers work and some others dont. Just wondering if there are any suggestions out there for me!
Thank you.
include <stdio.h>
include <stdlib.h>
int main()
{
int change, amount;
int quarters, dimes, nickels, pennies;
Are you an idiot? You've been to always initialize your variables. Do so.
Soma
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-02
Why is this a "for statement issue"? What "for statement"?
What Soma means is that if you never assign a value to a local variable, it is not necessarily zero. Since you only assign the coin values within conditionals for some values of 'change', the coin values will never be assigned a value.
Actually he probably meant what he said - but today I'm "good cop". ;-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry about the subject typo. It is actually a for statement. I slightly understand what you guys are asking, but I don't know how to go about giving them a value.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
you said>>>Hello! I am using Dev C++ at school in my basic programming class. I have to create a program that will ask the user to input number until they hit the 0 button<<<
you also said>>>I slightly understand what you guys are asking, but I don't know how to go about giving them a value<<<
You dont know how to assign a value to a variable??? Yet you already have or is that anothers code you posted.
Read,read,read.You cant get more basic than this,I think I know where your difficulties arise from,namly...."naming conventions".
If youve got the balls,in one week you'll be looking back at these posts and going red in the face. :)
I've done it plenty of times,theres nothing you cant learn,but dont ^uck around...time is precious.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-03
For the 'hard-of-thinking' (and because time is precious):
int quarters = 0 ;
int dimes = 0 ;
int nickels = 0 ;
int pennies = 0 ;
Declaring all your variables on one declaration statement is a valid but a nasty habit. This way it is easier to see which ones are initialised and what type they have.
>> It is actually a for statement.
No it is not! I am sure that "ussue" rather than "issue" was a typo, but there is still no 'for' statment in your code!
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ahhhhh.... IC. I did not know that you had to give the quarters, dimes etc the value of 0. When I think about it, it really does make sense. Thanks all!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sorry, I forgot to ask how I would make the program ask the user 3 times to enter an amount. I am not sure what type of loop to use and how to incorporate it. This is my new line of code:
include <stdio.h> // I Andrew Scanlon Student number 0001162834 confirm that this work is mine and was not copyed in any way shape or form.
include <stdlib.h>
int main()
{
int change, amount;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
Do you want them to enter three amounts and then add them, or ask them three times in case they enter invalid amounts? Also you should initialize amount in case they do enter something invalid. The scanf function only modifies the value at the address you pass when it succeeds. If you type something that doesn't scan as %d then amount is still uninitialized. You could check the return value which is the number of successfully scanned items or zero (or maybe EOF?) if it doesn't find anything good.
Also just FYI, I don't think you need the if statements either. Your program should work just fine without them since you're doing integer math. Maybe you should change the topic to be simply "issue". :) That will please Wayne - he likes vague thread titles.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Okay, I will try to remove the "if" statements. Also, I wanted to make it loop so when a number is entered and it gives the results it will ask for a number a second time etc.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You could put the whole thing in a while loop that ends when scanf returns zero. That way it would keep asking for new numbers and displaying new results until the user typed in something other than a number.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Okay, I made a for statement and added it to the code. When I went to go compile, it did compile, but it still only ran the one time. This is what I have so far:
int main()
{
int change, amount;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
int l=0;
}
if(change >=1)
{
pennies= change/1;
change = change-pennies*1;
}
}
printf(" Number of Quarters %d\n Dimes is %d\n Nickels is %d\n Pennies is %d\n" , quarters, dimes, nickels, pennies);
system("pause");return0;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2007-10-03
You ask for input once, you then calculate the answer based on the same input data three times, then you output the last answer (which will be the same as the other two) once!
Think about it! Where do the start and end of the loop need to be!? Around everything that you want to happen three times. Caution also you will need to reinitialise the coins to zero at the start of the loop. C programmers have a habit of putting all there variables at the top of the function because they think they have to, but in this case:
int i ;
for( i = 0; i < 3; i++ )
{
int change ;
int amount ;
int quarters = 0 ;
int dimes = 0 ;
int nickels = 0 ;
int pennies = 0 ;
// input, calculation, and output here
}
is valid. They must be at the top of a block (i.e. immediately after a '{') in C89, and are in scope (i.e. visible to the code) only within the block. C99 is like C++ where a variable can be declared anywhere in a block, and is in scope until the end of the block.
BTW 'l' (lower-case L) is a really bad variable name due to its close resemblance to '1' (one).
I notice you have not implemented the suggestion about removing the if-statement's. Consider this:
now for any positive value < 25, change / 25 == 0. Which is what you want it to be in any case, so not only do you not need teh if-statements, if you removed them, you would not need teh zero initialisation either. The above can be just:
and your original uninitialised variables would be fine because bnow you always assign a value to each coin type. The problem of course comes is some smart-aleck enters a negative value. But you could handle input validation separately.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello again! This is a program that I made yesterday. I have to be able to calculate the amount of change in quarters, dimes, nickles and pennies out of $1. This is what I have so far, but when I run the program some numbers work and some others dont. Just wondering if there are any suggestions out there for me!
Thank you.
include <stdio.h>
include <stdlib.h>
{
int change, amount;
int quarters, dimes, nickels, pennies;
{
quarters= change/25;
change = change-quarters25;
}
if(change >=10)
{
dimes= change/10;
change = change-dimes10;
}
if(change >=5)
{
nickels= change/5;
change = change-nickels*5;
}
if(change >=1)
{
pennies= change/1;
change = change-pennies*1;
}
printf(" Number of Quarters %d\n Dimes is %d\n Nickles is %d\n Pennies is %d\n" , quarters, dimes, nickels, pennies);
}
O_o
Are you an idiot? You've been to always initialize your variables. Do so.
Soma
Why is this a "for statement issue"? What "for statement"?
What Soma means is that if you never assign a value to a local variable, it is not necessarily zero. Since you only assign the coin values within conditionals for some values of 'change', the coin values will never be assigned a value.
Actually he probably meant what he said - but today I'm "good cop". ;-)
Sorry about the subject typo. It is actually a for statement. I slightly understand what you guys are asking, but I don't know how to go about giving them a value.
>_<
Ah, I see. I'm an idiot...
You're a troll and an idiot.
Soma
you said>>>Hello! I am using Dev C++ at school in my basic programming class. I have to create a program that will ask the user to input number until they hit the 0 button<<<
you also said>>>I slightly understand what you guys are asking, but I don't know how to go about giving them a value<<<
You dont know how to assign a value to a variable??? Yet you already have or is that anothers code you posted.
Read,read,read.You cant get more basic than this,I think I know where your difficulties arise from,namly...."naming conventions".
If youve got the balls,in one week you'll be looking back at these posts and going red in the face. :)
I've done it plenty of times,theres nothing you cant learn,but dont ^uck around...time is precious.
For the 'hard-of-thinking' (and because time is precious):
int quarters = 0 ;
int dimes = 0 ;
int nickels = 0 ;
int pennies = 0 ;
Declaring all your variables on one declaration statement is a valid but a nasty habit. This way it is easier to see which ones are initialised and what type they have.
>> It is actually a for statement.
No it is not! I am sure that "ussue" rather than "issue" was a typo, but there is still no 'for' statment in your code!
Clifford
Ahhhhh.... IC. I did not know that you had to give the quarters, dimes etc the value of 0. When I think about it, it really does make sense. Thanks all!
Sorry, I forgot to ask how I would make the program ask the user 3 times to enter an amount. I am not sure what type of loop to use and how to incorporate it. This is my new line of code:
include <stdio.h> // I Andrew Scanlon Student number 0001162834 confirm that this work is mine and was not copyed in any way shape or form.
include <stdlib.h>
{
int change, amount;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
{
quarters= change/25;
change = change-quarters25;
}
if(change >=10)
{
dimes= change/10;
change = change-dimes10;
}
if(change >=5)
{
nickels= change/5;
change = change-nickels*5;
}
if(change >=1)
{
pennies= change/1;
change = change-pennies*1;
}
printf(" Number of Quarters %d\n Dimes is %d\n Nickels is %d\n Pennies is %d\n" , quarters, dimes, nickels, pennies);
}
Do you want them to enter three amounts and then add them, or ask them three times in case they enter invalid amounts? Also you should initialize amount in case they do enter something invalid. The scanf function only modifies the value at the address you pass when it succeeds. If you type something that doesn't scan as %d then amount is still uninitialized. You could check the return value which is the number of successfully scanned items or zero (or maybe EOF?) if it doesn't find anything good.
Also just FYI, I don't think you need the if statements either. Your program should work just fine without them since you're doing integer math. Maybe you should change the topic to be simply "issue". :) That will please Wayne - he likes vague thread titles.
Okay, I will try to remove the "if" statements. Also, I wanted to make it loop so when a number is entered and it gives the results it will ask for a number a second time etc.
You could put the whole thing in a while loop that ends when scanf returns zero. That way it would keep asking for new numbers and displaying new results until the user typed in something other than a number.
Okay, I made a for statement and added it to the code. When I went to go compile, it did compile, but it still only ran the one time. This is what I have so far:
{
int change, amount;
int quarters=0;
int dimes=0;
int nickels=0;
int pennies=0;
int l=0;
{
if(change >=25)
{
quarters= change/25;
change = change-quarters25;
}
if(change >=10)
{
dimes= change/10;
change = change-dimes10;
}
if(change >=5)
{
nickels= change/5;
change = change-nickels*5;
}
if(change >=1)
{
pennies= change/1;
change = change-pennies*1;
}
}
printf(" Number of Quarters %d\n Dimes is %d\n Nickels is %d\n Pennies is %d\n" , quarters, dimes, nickels, pennies);
}
You ask for input once, you then calculate the answer based on the same input data three times, then you output the last answer (which will be the same as the other two) once!
Think about it! Where do the start and end of the loop need to be!? Around everything that you want to happen three times. Caution also you will need to reinitialise the coins to zero at the start of the loop. C programmers have a habit of putting all there variables at the top of the function because they think they have to, but in this case:
int i ;
for( i = 0; i < 3; i++ )
{
int change ;
int amount ;
int quarters = 0 ;
int dimes = 0 ;
int nickels = 0 ;
int pennies = 0 ;
// input, calculation, and output here
}
is valid. They must be at the top of a block (i.e. immediately after a '{') in C89, and are in scope (i.e. visible to the code) only within the block. C99 is like C++ where a variable can be declared anywhere in a block, and is in scope until the end of the block.
BTW 'l' (lower-case L) is a really bad variable name due to its close resemblance to '1' (one).
I notice you have not implemented the suggestion about removing the if-statement's. Consider this:
if(change >=25)
{
quarters= change/25;
change = change-quarters*25;
}
now for any positive value < 25, change / 25 == 0. Which is what you want it to be in any case, so not only do you not need teh if-statements, if you removed them, you would not need teh zero initialisation either. The above can be just:
quarters = change / 25 ;
change = change - quarters * 25 ;
and your original uninitialised variables would be fine because bnow you always assign a value to each coin type. The problem of course comes is some smart-aleck enters a negative value. But you could handle input validation separately.
Clifford
Thanks! I will remove the if statements and fix the for statement.
Before you guys comment on the comment on the top the teacher did say we can use online resources such as forums to get help, just FYI.
A loop to iterate exactly three times:
int i ;
for( i = 0; i < 3; i++ )
{
...
}
Surely you covered that in class!?
Clifford