Hello. I'm new to Daniweb and am trying to create a program in C that will compile sales tax after the user inputs a purchase amount. Am I doing it right? I would appreciate any suggestions and feedback. Also, I am unable to see my output using the Run function in Miracle C Workbench.

Sincerely,
Gia

{ 
     
     //Declare variables 
 	int b = 725, c = 750, d = 775, e = 10000; 
	int DelMarTaxRate, EncinitasTaxRate, LaJollaTaxRate, PurchaseAmount, DelMarSalesTax, EncinitasSalesTax, LaJollaSalesTax;
            
            //Calculate tax rates 
            DelMarTaxRate = b/e; 
            EncinitasTaxRate = c/e; 
            LaJollaTaxRate = d/e; 
            
            //Ask user for purchase amount
            	printf("\nPlease enter your purchase amount: ");
            	scanf("s%", PurchaseAmount);
            
            //Thank user for entering purchase amount
            	printf("n\Thank you. You entered %s\n" PurchaseAmount);
 
            //Calculate sales tax 
            DelMarSalesTax = PurchaseAmount*b/e; 
            EncinitasSalesTax = PurchaseAmount*c/e; 
            LaJollaSalesTax = PurchaseAmount*d/e; 
           
           //Display results             
            printf("DelMar: %d\n Encinitas: %d\n LaJolla: %d\n", DelMarTaxRate, EncinitasTaxRate, LaJollaTaxRate); 
            printf("%ld\n", PurchaseAmount);
            printf("%ld\n", DelMarSalesTax); 
            printf("%ld\n", EncinitasSalesTax); 
            printf("%ld\n", LaJollaSalesTax);

	printf("\nPress any key to end."); 
	getche();

}

> int b = 725, c = 750, d = 775, e = 10000;
Compared to your other variable names, these are meaningless.

> scanf("s%", PurchaseAmount);
1. The % should be first, then the conversion format letter follows.
2. It's d for an integer.
3. You forgot the &, as in
scanf("%d", &PurchaseAmount);

> printf("%ld\n", PurchaseAmount);
Again, more inconsistency. It's just %d for a decimal integer.

There are syntax and type errors throughout (maybe only a little dyslexia, perhaps?).

For example: printf("n\Thank you. You entered %s\n" PurchaseAmount); should read: printf("\nThank you. You entered %d\n", PurchaseAmount); The main problem, though, is how you are handling types. Ah, I just previewed and I see Salem has noted the scanf() problem.

The last thing is that you are using all integers. This isn't necessarily a bad thing (I entered compiled and entered 1290 for $12.90), except that when you calculate the tax rates you are dividing small numbers by a really big number: DelMarTaxRate = 725/10000; That's less than zero, and since you are using integers, the fraction is lost and the DelMarTaxRate is set to zero. To get the cents you'd have to multiply the numerator by 100 first. Or change all your ints to floats (and use the proper scanf and printf formats).

Hope this helps.

Thanks for your help. I appreciate feedback from both of you. I don't want to have meaningless variable names and I like to float idea. How should I implement the use of floats for my variables?

How should I implement the use of floats for my variables?

change all your ints to floats (and use the proper scanf and printf formats).

float PurchaseAmount;

scanf( "%f", &PurchaseAmount );
printf( "This is the purchased amount: %.3f\n", PurchaseAmount );

Thanks! :)

Hi Guys,

I need some help here..I think I'm going wrong with the switch part. Please help...

Code:

#include<stdio.h> 
    int main() 
    
    { 
     
     //Declare variables 
 	int b = 725, c = 750, d = 775, e = 10000;
 	int iLocation;
	float delMarTaxRate, encinitasTaxRate, laJollaTaxRate, purchaseAmount, delMarSalesTax, encinitasSalesTax, laJollaSalesTax;
	
	//Initialize variable
		iLocation=0;
            
            //Calculate tax rates 
            delMarTaxRate = b/e; 
            encinitasTaxRate = c/e; 
            laJollaTaxRate = d/e; 
            
            //Ask user for purchase amount
            	printf("\nPlease enter your purchase amount: ");
            	scanf("%f", &purchaseAmount);
            
            //Verify validity of purchase amount input.  If invalid, loop.
            	while (purchaseAmount<=0.00)
            	{
            		printf("You have entered an invalid amount. Please enter your purchase amount: ");
            		scanf("%f", &purchaseAmount);
            	}
            
            //Thank user for entering purchase amount
            	printf("\nThank you. You entered: $%.2f\n", purchaseAmount);
            
            //Ask user for location of purchase
            	printf("\nPlease input the number for the location where you made the purchase: 1 for Del Mar, 2 for Encinitas, or 3 for La Jolla ");
            	scanf("%d", &iLocation);
 
            //Calculate sales tax 
            delMarSalesTax = purchaseAmount*b/e; 
            encinitasSalesTax = purchaseAmount*c/e; 
            laJollaSalesTax = purchaseAmount*d/e; 
           
           //Display results
           /*case*/         
            Switch(iLocation)
            	{
            		case 1:
            			printf("\nYou have selected Del Mar \n");
            			printf("\nPurchase Amount: $%.2f\n", purchaseAmount);
            			printf("\nDel Mar Tax Rate: $%.2f\n", delMarTaxRate);
            			printf("\nDel Mar Sales Tax: $%.2f\n", delMarSalesTax);
            			break;
            		case 2:
            			printf("\nYou have selected Encinitas \n");
            			printf("\nPurchase Amount: $%.2f\n", purchaseAmount);
            			printf("\nDEncinitas Tax Rate: $%.2f\n", encinitasTaxRate);            
            			printf("\nEncinitas Sales Tax: $%.2f\n", encinitasSalesTax);
            			break;
            		case 3:
            			printf("\nYou have selected La Jolla \n");
            			printf("\nPurchase Amount: $%.2f\n", purchaseAmount);
            			printf("\nLa Jolla Tax Rate: $%.2f\n", laJollaTaxRate);
            			printf("\nLa Jolla Sales Tax: $%.2f\n", laJollaSalesTax);
            		default:
            			printf("\nInvalid selection \n");
        	}
        	/*end case*/
        	
           
           //Display results             
            printf("DelMar: %f\n Encinitas: %f\n LaJolla: %f\n", delMarTaxRate, encinitasTaxRate, laJollaTaxRate); 
            printf("\nPurchase Amount: $%.2f\n", purchaseAmount);
            printf("\nDel Mar Sales Tax: $%.2f\n", delMarSalesTax); 
            printf("\nEncinitas Sales Tax: $%.2f\n", encinitasSalesTax); 
            printf("\nLa Jolla Sales Tax: $%.2f\n", laJollaSalesTax);

	printf("\nPress any key to end.\n"); 
	getche();

}

Lowercase switch() I helped him already on the IRC.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.