Menu

for statement ussue

Andrew
2007-10-02
2012-09-26
  • Andrew

    Andrew - 2007-10-02

    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;

    printf(&quot;Enter an amount from 1-100:&quot;);
    scanf(&quot;%d&quot;,&amp;amount);
    
    change = 100 - amount;
    
    if(change &gt;=25)
    

    {
    quarters= change/25;
    change = change-quarters25;
    }
    if(change >=10)
    {
    dimes= change/10;
    change = change-dimes
    10;
    }
    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);

    system (&quot;pause&quot;);
    return 0;
    

    }

     
    • Soma

      Soma - 2007-10-02

      O_o

      Are you an idiot? You've been to always initialize your variables. Do so.

      Soma

       
    • Anonymous

      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". ;-)

       
    • Andrew

      Andrew - 2007-10-02

      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.

       
      • Soma

        Soma - 2007-10-03

        >_<

        Ah, I see. I'm an idiot...

        You're a troll and an idiot.

        Soma

         
    • Kurgusov

      Kurgusov - 2007-10-03

      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.

       
    • Anonymous

      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

       
    • Andrew

      Andrew - 2007-10-03

      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!

       
    • Andrew

      Andrew - 2007-10-03

      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;

      printf(&quot;Enter an amount from 1-99:&quot;);
      scanf(&quot;%d&quot;,&amp;amount);
      
      change = 100 - amount;
      
      if(change &gt;=25)
      

      {
      quarters= change/25;
      change = change-quarters25;
      }
      if(change >=10)
      {
      dimes= change/10;
      change = change-dimes
      10;
      }
      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);

      system (&quot;pause&quot;);
      return 0;
      

      }

       
      • Osito

        Osito - 2007-10-03

        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.

         
    • Andrew

      Andrew - 2007-10-03

      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.

       
      • Osito

        Osito - 2007-10-03

        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.

         
    • Andrew

      Andrew - 2007-10-03

      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;

      printf(&quot;Enter an amount from 1-99:&quot;);
      scanf(&quot;%d&quot;,&amp;amount);
      
      change = 100 - amount;
      
      for ( l = 0; l &lt; 3; l++ )
      

      {
      if(change >=25)
      {
      quarters= change/25;
      change = change-quarters25;
      }
      if(change >=10)
      {
      dimes= change/10;
      change = change-dimes
      10;
      }
      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);

      system (&quot;pause&quot;);
      return 0;
      

      }

       
    • Anonymous

      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:

      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

       
    • Andrew

      Andrew - 2007-10-03

      Thanks! I will remove the if statements and fix the for statement.

       
    • Andrew

      Andrew - 2007-10-03

      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.

       
    • Anonymous

      Anonymous - 2007-10-03

      A loop to iterate exactly three times:

      int i ;

      for( i = 0; i < 3; i++ )
      {
      ...
      }

      Surely you covered that in class!?

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.