PDA

View Full Version : C pointer problems

fwongmc
12-03-2004, 07:59 PM
Hello everybody!I am a new to C.My lecture asked us to write a program with pointers.But I do think I get stuck on pointer's stuff.Can anyone please do me a flavour?It is kinda urgent stuffs.

The following are the questions:

Write a program to dispense change.The user enters the amount paid and the amount due.The program determines how many dollars, 50cents,20cents,10cents should be given as change.

a)Write a function with the heading:
void dispense(int change,int *dollars,int *c10,int *c20,int *c50)
that determines and return the quantity of each kind of coin.
(Note:16 cents-5 cents=11 cents meaning that one 10cents is returned for charge.However 16 cents-1 cents=15 cents meaning that one 20-cents is required(round-up))

----------------------------------------------
The following is my work:

void dispense(int change,int *dollars,int *c10,int *c20,int *c50){
dollars=round(paid-due);
change=dollars/10;
while (change>=1&&change<=9){
switch (change) {
case '1' :c10+=1;
break;

case '2' :c20+=1;
break;

case '3' :c10+=1;
c20+=1;
break;

case '4' :c20+=2;
break;

case '5' :c50+=1;
break;

case '6' :c10+=1;
c50+=1;
break;

case '7' :c20+=1;
c50+=1;
break;

case '8' :c10+=1;
c20+=1;
c50+=1;
break;

case '9' :c20+=2;
c50+=1;
break;

printf ("\nEnter the amount-paid and amount-due please:");
}
}--------------------------------------------------------------------------------------------------------------------------
b)Write a function int getData(int *paid,int *due) that doe the following:

inform the user that amount paid and amount due should be entered in cents(integer)
do
prompt the user to enter the amount paid and amount due
while (amount due<0 or amount paid<amount due);
if both amount paid and amount due are zeros
return 0
otherwise
return 1

--------------------------------------------------------------------------------------------------------------------------
The following is my work:

int getData(int *paid,int *due){
printf ("\nThe amount-paid and amount-due should be entered in cents(integer)");
do {
printf ("\nTo terminate the program,enter 0 for both values");
printf ("\nEnter the amount-paid and amount-due please:");
scanf ("%d %d",&paid,&due); } while (due<0||paid<due);
if (paid==0&&due==0)
return 0;
else return 1;}--------------------------------------------------------------------------------------------------------------------------c)Your main program should repeat the following until both the amount paid and amount due are zeros.

1.call the function getData to ask for the amount paid and amount due
2.print out the charge and the # of each kind of coins to dispense.

Program output:

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:16 5
amount due:5,amount paid:16,and thus change=11
You are suggested to give him/her 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:26 5
amount due:5,amount paid:26,and thus change=21
You are suggested to give him/her 1 20-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:66 5
amount due:5,amount paid:66,and thus change=61
You are suggested to give him/her 1 50-cents coin(s) 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:81 5
amount due:5,amount paid:81,and thus change=76
You are suggested to give him/her 1 50-cents coin(s) 1 20-cents coin(s) 1 10-cents coin(s)

The amount-paid and amount-due should be entered in cents (integer)
To terminate the program,enter 0 for both values
Enter the amount paid and amount due please:5 81
Enter the amount paid and amount due please:0 0

--------------------------------------------------------------------------------------------------------------------------
#include <stdio.h>

int getData(int *paid,int *due){
printf ("\nThe amount-paid and amount-due should be entered in cents(integer)");
do {
printf ("\nTo terminate the program,enter 0 for both values");
printf ("\nEnter the amount-paid and amount-due please:");
scanf ("%d %d",&paid,&due); } while (due<0||paid<due);
if (paid==0&&due==0)
return 0;
else return 1;}

void dispense(int change,int *dollars,int *c10,int *c20,int *c50){
dollars=round(paid-due);
change=dollars/10;
while (change>=1&&change<=9){
switch (change) {
case '1' :c10+=1;
break;

case '2' :c20+=1;
break;

case '3' :c10+=1;
c20+=1;
break;

case '4' :c20+=2;
break;

case '5' :c50+=1;
break;

case '6' :c10+=1;
c50+=1;
break;

case '7' :c20+=1;
c50+=1;
break;

case '8' :c10+=1;
c20+=1;
c50+=1;
break;

case '9' :c20+=2;
c50+=1;
break;

printf ("\nEnter the amount-paid and amount-due please:");
}
}

int main ()

{
do {
getData();} while getData==1;

dispense();
printf ("\namount due:%d",due);
printf ("amount paid:%d",paid);
printf ("and thus change=%d",change);

if (change<5) {
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s)",c10);}
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s)",c20);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coiu(s)",c10,c20);}

else if (change>5) {
if (c10==0){
printf ("You are suggested to give him/her %d 20-cents coin(s) %d 50-cents coin(s)",c20,c50);}
if (c20==0){
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 50-cents coin(s)",c10,c50);}
else
printf ("You are suggested to give him/her %d 10-cents coin(s) %d 20-cents coin(s) %d 50-cents coin(s)",c10,c20,c50);}

else if (change==5) {
printf ("You are suggested to give him/her %d 50-cents coin(s)",c50);}

}
}

--------------------------------------------------------------------------------------------------------------------------
When I complier my program,the debugger told me that my program have 22 errors.
I am trying to find out the errors(some of the error are pointers errors)
Can anyone of your help me to debug my program?

aman
12-03-2004, 11:57 PM
When you see compiler errors, always fix the top one first because one programming error usually causes many compiler errors and you will find that fixing the first problem usually clears up a few of the compiler errors.

int main()
{
int due, paid;

// rest of code

Serex
12-07-2004, 09:15 AM
can i suggest using namespace std and cin and cout instead of printf and scanf

example:

#include "stdafx.h"
#include <iostream> // needed header for cin, cout
using namespace std;

int main()
{
int i, j, k;
cout << "please enter number one: ";
cin >> i;
cout << "please enter number two: ";
cin >> j;

if ( i > j )
{
k = i;
}
else
{
k = j;
}

cout << "biggest number: " << k << endl;
system("pause");
return 0;
}

if you have any questions just reply. :D
im working on your code now. its pretty messy :p

serfurj
12-07-2004, 05:27 PM
Serex,

i'd like to use iostream instead of stdio, but i've only been able to get it to work in c++. have you tried to compile that code you posted? it doesn't work for me.

gcc test.c -o test
test.c:1:20: stdafx.h: No such file or directory
test.c:2:51: iostream: No such file or directory
test.c:3: error: syntax error before "namespace"
test.c:3: warning: data definition has no type or storage class
test.c: In function `main':
test.c:8: error: `cout' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
test.c:9: error: `cin' undeclared (first use in this function)
test.c:22: error: `endl' undeclared (first use in this function)

Compilation exited abnormally with code 1 at Tue Dec 7 10:24:33

Serex
12-07-2004, 05:54 PM
Sorry my bad.... C pointer... not C++