# stuck on purse program.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-01-2012
rosemary
stuck on purse program.
hey guys-
yes, this is a homework assignment. i'm stuck and if anyone can point me in the right direction it would be much appreciated. i need a program to remove and insert coins into a purse. thanks.

Code:

#include <stdio.h>
#include <math.h>
int main(void)
{
int pennies=0, nickels=0, dimes=0, quarters=0, total=0, p,n,d,q,x;
int choice;

printf("Would You Like to Insert or Remove Coins?\n");
printf("Press 1 for Insert.\n");
printf("Press 2 for Remove.\n");
scanf("%d", &choice);

if (choice == 1)
{
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, total);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies += p;
nickels += n;
dimes += d;
quarters += q;
float x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, total);
}
else (choice == 2)
{
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, total);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;
float x = pennies + 5*nickels + 10*dimes + 25*quarters;
x =  (float)x/100;
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, total);
}

return 0;

}

• 10-01-2012
jimblumberg
My first suggestion would be to find an indentation style you like and use it consistently. This will make following your program logic much easier.

The next suggestion would be to ask specific questions based on the information you provide. Do you have some kind of question or problem with the code you provided?

Jim
• 10-01-2012
rosemary
First thing, I am not getting a correct total. It will show the correct number of coins, but the total reads \$0.
• 10-01-2012
Nyah Check
so what is the real problem with your code at least give some basic errors so i can help...
• 10-01-2012
qny
I think your code does not compile.

Anyway, it is wrong
Code:

if (choice == 1)
{
/* stuff */
}
else (choice == 2)
{
/* stuff */
}

You should write that as
Code:

if (choice == 1)
{
/* stuff */
}
else /* choice == 2 (unless the user didn't follow instructions) */
{
/* stuff */
}

• 10-01-2012
jimblumberg
That is probably being caused because you have defined x as an int, and an int has no fractions. So this line is probably the problem.
Code:

x =  (float)x/100;
What did you input into the program? Are you inserting into the purse or extracting? How many of each coin did you enter?

Jim
• 10-01-2012
rosemary
Quote:

Originally Posted by jimblumberg
So this line is probably the problem.
Code:

x =  (float)x/100;

Yes, but I do not know how to adjust this.

Quote:

What did you input into the program? Are you inserting into the purse or extracting? How many of each coin did you enter?Jim
I tried inserting. I entered
Code:

1+2+3+4
It displayed
Code:

1 pennies+2 nickels+3 dimes+4 quarters=\$0
• 10-01-2012
rosemary
K. I moved float to the top, indented, and changed to just else.
Code:

#include <stdio.h>
#include <math.h>
int main(void)
{
int pennies=0, nickels=0, dimes=0, quarters=0, p,n,d,q;
int choice=0;

float(x) = 25*quarters + 10*dimes + 5*nickels + pennies;
x =  x/100;

printf("Would You Like to Insert or Remove Coins?\n");
printf("Press 1 for Insert.\n");
printf("Press 2 for Remove.\n");
scanf("%d", &choice);

if (choice == 1)
{
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, x);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies += p;
nickels += n;
dimes += d;
quarters += q;
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, x);
}
else
{
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, x);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%d \n", pennies, nickels, dimes, quarters, x);
}

return 0;

}

• 10-01-2012
AndiPersti
Quote:

Originally Posted by rosemary
First thing, I am not getting a correct total. It will show the correct number of coins, but the total reads \$0.

That's because you never change the value of "total" (it is initialized with 0).

Bye, Andreas
• 10-01-2012
rosemary
ugh. the indentation didnt copy properly :(
• 10-01-2012
jimblumberg
Quote:

K. I moved float to the top, indented, and changed to just else.
What good does moving the calculation to the beginning, before you enter any values, do you?

Does the following even compile?
Code:

float(x) = 25*quarters + 10*dimes + 5*nickels + pennies;
x =  x/100;

Where is x defined? What type of variable is x?

Jim
• 10-01-2012
rosemary
[QUOTE=jimblumberg;1125437

Where is x defined? What type of variable is x?

Jim[/QUOTE]

Code:

#include <stdio.h>
#include <math.h>
int main(void)
{
int pennies=0, nickels=0, dimes=0, quarters=0, total=0, p,n,d,q,t;
int choice=0;

printf("Would You Like to Insert or Remove Coins?\n");
printf("Press 1 for Insert.\n");
printf("Press 2 for Remove.\n");
scanf("%d", &choice);

if (choice == 1)
{
printf( "%d pennies + %d nickels + %d dimes + %d quarters = \$%.2d total\n", pennies, nickels, dimes, quarters);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies += p;
nickels += n;
dimes += d;
quarters += q;
total=total+(25*quarters + 10*dimes + 5*nickels + pennies/100);
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2d total\n", pennies, nickels, dimes, quarters, total);
}
else
{
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2d total\n", pennies, nickels, dimes, quarters, total);
scanf("%d%d%d%d", &p, &n, &d, &q);
pennies -= p;
nickels -= n;
dimes -= d;
quarters -= q;
total=total-(25*quarters + 10*dimes + 5*nickels + pennies/100);
printf( " %d pennies + %d nickels + %d dimes + %d quarters = \$%.2d total\n", pennies, nickels, dimes, quarters, total);
}

return 0;

}

• 10-01-2012
jimblumberg
So does your modified code solve your problems? If not maybe you need to ask another question.

Jim
• 10-01-2012
varshneyabhi
one more point, keep it in a loop. Your program would exit just after single iteration.
• 10-01-2012
Matticus
Quote:

Originally Posted by varshneyabhi
one more point, keep it in a loop. Your program would exit just after single iteration.

This is a homework exercise, so a single iteration is sufficient - unless the assignment calls for the program to loop, which does not seem to be the case.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last