0

My code is not presented correctly in the question. I have copied it from Gedit and pasted it in the text editor. When I posted it and saw the answer, I realised that some of my code was missing and other parts had been changed. I have now tried to edit my question and just type my code instead of pasting it, but still the same thing happens. The >= is being changed into just and = sign. and also other code in the 'if' statement has been removed. Can anyone please help me with this? If I cannot present my code properly, I will never get the right help with my Greedy issue.

This was my original question:

I have been working on Greedy for over a week and still stuck on the same element: When change is .80 only 3 coins are counted. I have written the code in several different ways and used a lot of the discussion feedback from other users but still got nowhere. Please give me advise on my work so I can improve my code to make it work properly. Thanks, Tessa

The outcome is 3 coins after every 'if' or 'else' statement. I believe only the quarters are being counted and not the dimes, nickels and pennies.

#include 
#include 
#include 

int main (void)
{
    int cents = 0;
    float change;

    do
    {
        printf("How much change is owed?: ");
        change = GetFloat();
    }
    while (change = 25)
    {
        do
        {
        cents = cents - (coins * 25);
        coins += cents / 25;
        }
        while (cents >= 25);
    }

    else if (cents = 10)
    {
        do
        {
        cents = cents % 10;
        coins += cents / 10;    
        }
        while (cents = 10);
    } 

    else if (cents = 5)
    {
        do
        {
        cents = cents % 5;
        coins += cents / 5;     
        }
        while (cents = 5);
    }

    else (cents = 1)
    {
        do
        {
        cents = cents % 1;
        coins += cents / 1;   
        }
        while (cents  1);
    } 
}
while (cents > 0);

    printf("I was greedy and only used %d coins\n", coins);    

}
2

Your code has many mistakes. First of all when you want to check for equality you must use the double equal sign and not a single one.
Ex.

if (change == 25)
{
    // your code here
}

and not

if (change = 25)
{
    // your code here
}

Also your logic has many tiny mistakes so I would suggest you rewatch the pset's walkthrough videos and follow along.

| improve this answer | |
  • I see your point. Unfortunately my code that I copied and pasted from my terminal was not pasted properly and I should have checked it. I was not using the equality sign but the >= sign but the > from the >= was not pasted. May I ask you to please have another look? Is there a way for me to give you the code as I typed it and not as I pasted it from my terminal into this site? – Tessa Lock-Mooibroek Jul 6 '15 at 19:25
  • 1
    Why are you copying it from the terminal? Open your .c file using gedit and copy your code, then paste it here. – ChrisG Jul 6 '15 at 20:01
  • Through a different channel I got the tip to remove all the if/else and do/while's and now my code works. Thanks for your time to help me out. I am glad that it now works. It also passed all the checks with a :-) So I am happy as :-) – Tessa Lock-Mooibroek Jul 8 '15 at 2:20
  • Glad you passed it! Keep up the good work! – ChrisG Jul 8 '15 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .