Thread: C++ cash register/how to split floats?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    5

    C++ cash register/how to split floats?

    I'm working on a cash register program using C ++ the issue is that i'm not sure how to use the mod operator to get the remainder and figure out how many cents and bills needed ( Twenties, tens, fives, dollars, quarters, dimes , nickels, pennies)
    I used a float to get the change now someone told me i should split that change value into changeindollars and changeincents, then it should be easier to get the other details. This is what i got down so far:
    thanks for the help anyone.




    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    int main(void) 
    {
    	float amountPurchase=0.00;
    	float amountTendered=0.00;
    	float change=0.00;
    	int changeInDollars=0;
    	int changeInCents=0;
                int Twenties, Tens, Fives; 
    
    
    	printf("Welcome to the Online cash register!\n"); 
    	
    	printf("Please enter the total amount of purchase: $"); 
    	scanf("%f", & amountPurchase);
    	getchar();
    	printf("Please enter the amount of money tendered: $");
    	scanf("%f",& amountTendered);
    	getchar();
    	change = amountTendered - amountPurchase;
    	printf("Your changes is:$ %.2f\n", change);
    
    	changeInDollars =   ?
                 changeInCents =   ?
    
    	Twenties = changeInDollars / 20;
    	printf("Twentie: %i\n", Twenties);
    	
    	
    	Tens = changeInDollars /10;
    	printf("Ten: %i\n", Tens);
    	
    	
    	Fives = changeInDollars /5;
    	printf("Five: %i\n", Fives);
    
                 
    	
    
    	printf("Thanks you for using the Online cash register!\n"); 
    
    	getchar();
    	return EXIT_SUCCESS; 
    }

  2. #2
    Registered User scarfish's Avatar
    Join Date
    Apr 2008
    Posts
    3
    Hi, I would make something like this:
    ...
    Twenties = int(change/20);
    change -= Twenties * 20;

    Tens = int(change/10);
    change -= Tens * 10;

    Fives = int(change/5);
    change -= Fives * 5;

    int dollars = int(change/1);
    change -= dollars;

    change *= 100;

    int quarters = int(change/25);
    change -= quarters * 25;
    and so on

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    i tried that but change is a float number and when i try compiling what you just posted it gives me a C2059 error 'type' . ..

  4. #4
    Registered User scarfish's Avatar
    Join Date
    Apr 2008
    Posts
    3
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    int main(void) 
    {
    	float amountPurchase=0.00;
    	float amountTendered=0.00;
    	float change=0.00;
    	int changeInDollars=0;
    	int changeInCents=0;
                int Twenties, Tens, Fives; 
    
    
    	printf("Welcome to the Online cash register!\n"); 
    	
    	printf("Please enter the total amount of purchase: $"); 
    	scanf("&#37;f", & amountPurchase);
    	getchar();
    	printf("Please enter the amount of money tendered: $");
    	scanf("%f",& amountTendered);
    	getchar();
    	change = amountTendered - amountPurchase;
    	printf("Your changes is:$ %.2f\n", change);
    
    	Twenties = int(change/20);
    	change -= Twenties * 20;
    	printf("Twenties: %i\n",Twenties);
    
    	Tens = int(change/10);
    	change -= Tens * 10;
    	printf("Tens: %i\n",Tens);
    
    	Fives = int(change/5);
    	change -= Fives * 5;
    	printf("Fives: %i\n",Fives);
    
    	int dollars = int(change/1);
    	change -= dollars;
    	printf("Dollars: %i\n",dollars);
    
    	change *= 100;
    
    	int quarters = int(change/25);
    	change -= quarters * 25; 
    	printf("Quarters: %i\n",quarters);
    	
    	printf("Thanks you for using the Online cash register!\n"); 
    
    	getchar();
    	return EXIT_SUCCESS; 
    }
    That works, tested with g++

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    5
    Ok thank you very much scarfish. ... it did help...however idk why but visual basic would not compile it because change is a float and it keeps giving me that same C2059 error but i've got to make it work with integer values (well at least that's better than what i had i'm gonna work on the decimal approximation myself). but thanks again !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help it won't compile!!!!!
    By esbo in forum C Programming
    Replies: 58
    Last Post: 01-04-2009, 03:22 PM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. split file
    By andriss in forum C Programming
    Replies: 4
    Last Post: 10-18-2006, 03:47 PM
  4. Split line
    By groorj in forum C Programming
    Replies: 8
    Last Post: 04-24-2005, 12:05 PM
  5. Split int variable
    By Jas11 in forum C++ Programming
    Replies: 4
    Last Post: 03-28-2005, 05:06 PM