Help fixing my cash register program.

This is a discussion on Help fixing my cash register program. within the C++ Programming forums, part of the General Programming Boards category; I need help making a simple cash register program which a user inputs a number and the program puts the ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Help fixing my cash register program.

    I need help making a simple cash register program which a user inputs a number and the program puts the amount of coins and of what value excluding pennies. for example if i enter 90 it should output 1 of 50, 2 of 20, 0 of 10 and 0 of 5.

    i have written two separate programs which both dont work.

    this one refuses to compile.
    Code:
    #include<stdio.h>
    
    void Getnum(int num)
    {
    	printf("Please enter a number between 5 and 95 in cents\n");
    	scanf("%d%*c", &num);
    }
    
    void DoCalc(int &num, int value)
    {
    	value = 0;
    
    	while(num >= 50)
    	{
    		num = num - 50;
    		value++;
    	}
    
    	while(num >= 20)
    	{
    		num = num - 20;
    		value++;
    	}
    
    	while(num >= 10)
    	{
    		num = num - 10;
    		value++;
    	}
    
    	while(num >= 5)
    	{
    		num = num - 50;
    		value++;
    	}	
    	return;
    }
    
    void PrintCal(int &value)
    {
    	printf("(Fifty: %d\n" ,value);
    	printf("Twenty: %d\n" ,value);
    	printf("Ten: %d\n" ,value);
    	printf("Five: %d\n" ,value);
    	return;
    }
    
    int main()
    {
    	int num;
    	int value;
    
    	Getnum(num);
    	DoCalc((num, value));
    	PrintCal(value);
    
    	return(0);
    }
    and the second compiles but when you enter a number it prints an infinite loop
    Code:
    include<stdio.h>
    
    void Getnum1(int &num1)
    {
    	printf("Please enter a number between 5 and 95 in cents\n");
    	scanf("%d%*c", &num1);
    }
    
    void DoCalc(int num1)
    {
    	int fifty = 0;
    	int twenty = 0;
    	int ten = 0;
    	int five = 0;
    
    	while(num1 > 0 && num1 >= 50)
    	{
    		num1 = num1 - 50;
    		fifty++;
    		printf("Fifty: %d\n" ,fifty);
    	}
    
    	while(num1 > 0 && num1 >= 20)
    	{
    		num1 = num1 - 20;
    		twenty++;
    		printf("Twenty: %d\n" ,twenty);
    	}
    
    	while(num1 > 0 && num1 >= 10)
    	{
    		num1 = num1 - 10;
    		ten++;
    		printf("Ten: %d\n" ,ten);
    	}
    
    	while(num1 > 0 && num1 >= 5)
    	{
    		num1 = num1 - 50;
    		five++;
    		printf("Five: %d\n" ,five);
    	}	
    	return;
    }
    
    int main()
    {
    	int num1;
    
    	Getnum1(num1);
    	DoCalc(num1);
    
    	return(0);
    }
    please can you help me in anyway to fix both.

    Thanks in advance

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    So what are the compiler errors of the first?

    This condition (and its siblings) doesn't make sense:
    num1 > 0 && num1 >= 50
    If num1 is greater than or equal to 50, the first part is a given.

    I don't see anything that would cause an infinite loop. But I do see that you subtract 50 in the 5 clause.
    Last edited by CornedBee; 04-11-2008 at 02:04 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    is that it thank you very much!!! I feel like such an idiot right now!
    anyway the errors in the first are

    warning line 34: 'value' is assigned a value that is never used in function DoCalc(int &,int)
    warning line 28: 'value' is assigned a value that is never used in function DoCalc(int &,int)
    warning line 22: 'value' is assigned a value that is never used in function DoCalc(int &,int)
    warning line 16: 'value' is assigned a value that is never used in function DoCalc(int &,int)
    warning line 11: 'value' is assigned a value that is never used in function DoCalc(int &,int)
    error line 54: call of nonfunction in function main()

    i sort of understand what they mean but dont know how to fix them especially the one in the main function have tried everything.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> DoCalc((num, value));
    Remove the extra parentheses.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Thank you,

    but it still says value is assigned but never used how would i make it apart of the code?

    And thanks again for all your help.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by lil_rom View Post
    Thank you,

    but it still says value is assigned but never used how would i make it apart of the code?

    And thanks again for all your help.
    int &value (just like int &num).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 08:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 04:23 PM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 05:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21