# Thread: Need help with errors

1. ## Need help with errors

This program accepts a number of cents between 5 and 95 and sorts it out into change of 50,20,10 and 5 cent pieces. It seems to be having errors in the printf where it printf out the number of coins.
lines 40-43- unterminated string in function main
line40- function call ) missing in function main.
line 40- undefined symbol 'd' in function main.
Code:
```#include <stdio.h>
int main()
{
int num;
int fifty = 0;
int twenty = 0;
int ten = 0;
int five = 0;

printf("Please insert change");
scanf("%d%*c", &num);

while(num >= 5 && num <= 95)
{
if(num >= 50)
{
fifty++;
num=-50;
}
else
if(num >= 20)
{
twenty++;
num=-20;
}
else
if(num >= 10)
{
ten++;
num=-10;
}
else
if(num >= 5)
{
five++;
num=-5;
}
}

printf("Number of 50 cents "%d", fifty);
printf("Number of 20 cents "%d", twenty);
printf("Number of 10 cents "%d", ten);
printf("Number of 5 cents "%d", five);
return(0);
}```

2. The syntax highlighting should show you exactly what the problem is. Take a look and see if you can sort it out. Interestingly enough, it also shows a bug in the coloring system.

Quzah.

3. I would suggest looking into the use of curly braces in statements containing multiple vs. single lines.

4. Originally Posted by iceaway
I would suggest looking into the use of curly braces in statements containing multiple vs. single lines.
Braces are already in use. It is just that the formatting of the if else chain is unconventional, although consistent.

5. That's a beautiful set of logical statements, imo. (There is a simpler way to do this, using arithmetic, but your way is OK.)

If only there were a way to eliminate the extra char which causes the error here: *
Code:
` printf("Number of 50 cents "%d", fifty);`
And of course, it's always a good idea to add a \n char to the end of any printf() string.

*shamelessly borrowing a Quzah funny one liner.

6. Originally Posted by laserlight
Braces are already in use. It is just that the formatting of the if else chain is unconventional, although consistent.
Doh! Now I see it. Unconventional indeeed, it fooled me completely. Very rebellious use of indentation and line breaks :-)

7. Wait until you see Quzah's switch of index and pointer in an array access -- drives me around the bend and I know it's correct! It's just SO WRONG! <sigh>

8. Code:
```while(num >= 5 && num <= 95)
{
if(num >= 50)
{
fifty++;
num=-50;
}
else
if(num >= 20)
{
twenty++;
num=-20;
}
else
if(num >= 10)
{
ten++;
num=-10;
}
else
if(num >= 5)
{
five++;
num=-5;
}
}```
I believe there are "issues" with the highlighted sections of the code... unless those are typos.

9. hk_mp5kpdw, you are an eagle-eye! Nice catch.

Obviously the - goes before the = , not the other way around.

Popular pages Recent additions