Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 17th, 2012, 05:09 PM
 lopfui
Registered User

Join Date: Dec 2012
Location: HueHueBrBr land
Posts: 2
Time spent in forums: 51 m 19 sec
Reputation Power: 0
Could you lead a hand to a beginner?

Well, first of all, it's nice to meet everyone. This is my first time on the forums so sorry if i derp something up.

Allright, here's the deal.
I needed to make a program where the user would give a positive value in dollars, and then it would tell the user the minimum amount possible of coins needed to reach that value, in the American coin system (0.25,0.10,0.05,0.01)

Here is my code:
Code:
```
#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void)
{
printf("How much change is owed?");
float total = GetFloat();

if (total <= 0)
{
printf("That`s not a valid number! \n");
total = GetFloat();
}
//Obtains a value

int value = roundf(total * 100);
//Changes the total from a float to a int, also transforms it into cents

int coins = 0;
//Just defining some values and stuff

do
{
while (value >= 25);
{
value = value - 25;
coins++;
}

while (value >= 10);
{
value = value - 10;
coins++;
}

while (value >= 5);
{
value = value - 5;
coins++;
}

while (value >= 1);
{
value = value - 1;
coins++;
}
}
while (value > 0);

//this substracts the value bit by bit, keeping track of the coins.

printf("Here are the %d coins i owe you\n", coins);
//prints the number of coins

return 0;
}
```

For some reason, the only value that works is 0.1 and it says 4 coins. I have no idea what's going wrong!

Im using a compiler that came with the virtual machine, i think it's clang but instead of typing -stuff you just type "make (program)" and it compiles it.

#2
December 17th, 2012, 05:38 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,536
Time spent in forums: 2 Months 3 Days 6 h 2 m 28 sec
Reputation Power: 4106
You have about 4-5 bugs, which are all the same type of issue:
Code:
```        while (value >= 25); <--- REMOVE THIS SEMICOLON
{
value = value - 25;
coins++;
}```

The ; ends the while loop right there. To the compiler, this looks like you typed:
Code:
```        while (value >= 25) {
// DO NOTHING HERE
}
// REST OF CODE CONTINUES HERE
{
value = value - 25;
coins++;
}```

Therefore, it never really loops through the block that you think it should be looping through. If you lose the ; then it will loop through the block.

You have the same error with the rest of the while loops as well.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#3
December 17th, 2012, 05:43 PM
 lopfui
Registered User

Join Date: Dec 2012
Location: HueHueBrBr land
Posts: 2
Time spent in forums: 51 m 19 sec
Reputation Power: 0
Quote:
 Originally Posted by Scorpions4ever You have about 4-5 bugs, which are all the same type of issue: Code: ``` while (value >= 25); <--- REMOVE THIS SEMICOLON { value = value - 25; coins++; }``` The ; ends the while loop right there. To the compiler, this looks like you typed: Code: ``` while (value >= 25) { // DO NOTHING HERE } // REST OF CODE CONTINUES HERE { value = value - 25; coins++; }``` Therefore, it never really loops through the block that you think it should be looping through. If you lose the ; then it will loop through the block. You have the same error with the rest of the while loops as well.

Ahahah that worked. Thanks so much man, i didn't know about that.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Could you lead a hand to a beginner?