Sunset_Vista
asked on
Input error checking, C programming
I am very new to programming and having troubles. This is the second program I have written, aside from "Hello World". I got to this point by looking at other examples of user input error handling and taking those sections and adapting then into my program, sadly not fully understanding how they work, so now I am stuck.
My goal for this program is to have the user be able to enter the amount an item costs, then the program will print to the screen the sales tax on that items and the total amount of the sale for each of the three locations in the float section.
However, if a user enters a cost of .99 or 1.25, the program sees it as an error, and loops back asking for a positive integer. This is what I need to fix, the user needs to be able to enter, the cost without cents such as 125 or with cents such as 124.95.
Please help
My goal for this program is to have the user be able to enter the amount an item costs, then the program will print to the screen the sales tax on that items and the total amount of the sale for each of the three locations in the float section.
However, if a user enters a cost of .99 or 1.25, the program sees it as an error, and loops back asking for a positive integer. This is what I need to fix, the user needs to be able to enter, the cost without cents such as 125 or with cents such as 124.95.
Please help
//Author: David E. Berry
//Date: 15 May 2009, 23:38 this works for only positive numbers
//Revision: 2.0.9
//
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char input[BUFSIZ], *p;
long iAmountofSale; /* Used to for long result*/
//Section for Intro Text
printf("\n\tKudler Fine Foods Sales Tax and Total Calculation Program\n");
printf("\n\tversion 2.0 Author: David E. Berry\n");
printf("=========================================================\n");
printf("\Sales Tax per Store location and Sales Total for that location\n\n");
//Section for numbers to plug into equations
float iDelMarTaxRate = 0.0725;
float iEncinitasTaxRate = 0.075;
float iLaJollaTaxRate = 0.0775;
float iTax_Result_DelMar = 0;
float iTax_Result_Encinitas = 0;
float iTax_Result_LaJolla = 0;
float iTotal_Result_DelMar = 0;
float iTotal_Result_Encinitas = 0;
float iTotal_Result_LaJolla = 0;
printf("Enter purchase amount: $");
//Error handling section to ensure positive interger for input
for(;;)
{ /* Infinite "for" loop until the user enters a positive number */
fgets(input , sizeof(input), stdin);
iAmountofSale = strtol(input , &p, 10);
if(input[0] != '\n' && iAmountofSale > 0 && (*p == '\n' || *p == '\0')) break;
else printf("Invalid input!\n\nPlease enter a positive integer for the amount: $"); /*Displays if the user did not enter a postive number*/
iAmountofSale = 0;
}
printf("\nYou've entered the amount: $%ld\n", iAmountofSale); /*Displays what the number user input correctly*/
//Section for tax equations and results
iTax_Result_DelMar = iAmountofSale * iDelMarTaxRate;
printf("\nSales Tax for Del Mar is $%.2f\n", iTax_Result_DelMar);
iTax_Result_Encinitas = iAmountofSale * iEncinitasTaxRate;
printf("\nSales Tax Encinitas is $%.2f\n", iTax_Result_Encinitas);
iTax_Result_LaJolla = iAmountofSale * iLaJollaTaxRate;
printf("\nSales Tax La Jolla is $%.2f\n\n", iTax_Result_LaJolla);
//Section for Total Amount of sale equation and results
iTotal_Result_DelMar = iAmountofSale + iTax_Result_DelMar;
printf("\nTotal Sale for Del Mar is $%.2f\n\n", iTotal_Result_DelMar);
iTotal_Result_Encinitas = iAmountofSale + iTax_Result_Encinitas;
printf("Total Sale for Encinitas is $%.2f\n\n", iTotal_Result_Encinitas);
iTotal_Result_LaJolla = iAmountofSale + iTax_Result_LaJolla;
printf("Total Sale for La Jolla is $%.2f\n\n", iTotal_Result_LaJolla);
system("PAUSE");
return 0;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
evilrix,
I changed the type for iAmountofSale to double the fixed the strtod statement. But now it will not show the correct amount for what the user entered. I did go the references, as clear as they are it is still confusing to me. I have attached the part that is broken and a screen shot of how it looks when it runs.
I changed the type for iAmountofSale to double the fixed the strtod statement. But now it will not show the correct amount for what the user entered. I did go the references, as clear as they are it is still confusing to me. I have attached the part that is broken and a screen shot of how it looks when it runs.
for(;;)
{ /* Infinite "for" loop until the user enters a postive number */
fgets(input , sizeof(input), stdin);
iAmountofSale = strtod(input , &p); // was iAmountofSale = strtol(input , &p, 10);
if(input[0] != '\n' && iAmountofSale > 0 && (*p == '\n' || *p == '\0')) break;
else printf("Invalid input!\n\nPlease enter a positive integer for the amount: $"); /*Displays if the user did not enter a postive number*/
iAmountofSale = 0;
}
printf("\nYou've entered the amount: $%ld\n", iAmountofSale); /*Displays what the number user input correctly*/
16-May-09-16-39-53.png
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
masheik, Great catch! I changed it to a float and it works like I want it to now!! or least until I add more functions ;)
Both you and evilrix and answered all my questions that I had with this version of my little program. I am new to this forum, since you both gave answers to different parts of my question, how do I split the 'points' awarded?
Both you and evilrix and answered all my questions that I had with this version of my little program. I am new to this forum, since you both gave answers to different parts of my question, how do I split the 'points' awarded?
ASKER
Evilrix gave accurate and complete information, but being a beginner to programming I was struggling to understand some of the answers. Masheik's answer was presented in a fashion that was easy to understand.
ASKER
If I use scanf("%f", &amount); the program will let me enter negative numbers. That is why I moved to fgets . It there a way to write this using scanf to disallow character or negative numbers or a combination of both?