Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C pointer problems

    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;

    defaultrintf ("\nCheck your input!");
    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
    read in the data
    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;

    defaultrintf ("\nCheck your input!");
    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?

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    230
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Start with declaring the missing variables in your main() function, and work from there.
    Code:
    int main()
    {
         int due, paid;
    
        // rest of code

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    can i suggest using namespace std and cin and cout instead of printf and scanf

    example:

    Code:
    #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.
    im working on your code now. its pretty messy

  • #4
    New to the CF scene
    Join Date
    Dec 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    Sorry my bad.... C pointer... not C++


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •