Need some help doing error checking

This is a discussion on Need some help doing error checking within the C Programming forums, part of the General Programming Boards category; Hi everybody, I am in an introduction to C class and here is a program we had to do. Now, ...

  1. #1
    t_6
    t_6 is offline
    Registered User
    Join Date
    May 2008
    Posts
    2

    Need some help doing error checking

    Hi everybody,

    I am in an introduction to C class and here is a program we had to do. Now, what I am trying to do is expand on it a little more and do some error checking. So, if the user inputs something other than the correct amount or the correct store I want it to loop back up and ask again. Can someone help, I have tried using a for loop but I haven't been able to get it to work correctly.


    Code:
    #include <stdio.h>
    
    // function prototypes 
    float tax_delmar(float);
    float tax_encinitas(float);
    float tax_lajolla(float);
    float add_total(float, float);
    
    // global variables that can be used by any function
    float sales_tax;
    float total;
    
    
    main()
    
    {
    
    	//defining and initializing data type 
    	float purchase_amt = 0.0;
    	char  store = '0';
    	
    	
    	printf("\nWhat is your purchase amount? $");
    	//receiving user input for the purchase amount
    	scanf("%f", &purchase_amt);
    	
    	printf("\nWhat store?\n");
    	printf("\nPress D for DelMar, E for Encinitas, or L for LaJolla: ");
    	scanf("%c", &store);
    
    	
    	if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'd')
    	{
    	//sales tax for the Del Mar store rate
    	printf("\n\nSales tax for $%.2f at the DelMar store is: $%.2f\n", purchase_amt, tax_delmar(purchase_amt));
    	//total price with sales tax included for the Del Mar store
    	printf("\nThe total purchase price at the DelMar store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
    	}
    	else if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'e')
    	{
    	//sales tax for the Encinitas store rate
    	printf("\nSales tax for $%.2f at the Encinitas store is: $%.2f\n", purchase_amt, tax_encinitas(purchase_amt));
    	//total price with sales tax included for the Encinitas store
    	printf("\nThe total purchase price at the Encinitas store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
    	}
    	else if(purchase_amt >= 0.0 && purchase_amt <= 1000.0 && store == 'l')
    	{
    	//sales tax for the La Jolla store rate
    	printf("\nSales tax for $%.2f at the LaJolla store is: $%.2f\n", purchase_amt, tax_lajolla(purchase_amt));
    	//total price with sales tax included for the La Jolla store
    	printf("\nThe total purchase price at the LaJolla store is = $%.2f\n\n\n", add_total(sales_tax, purchase_amt));
    	}
    	
    	printf("\nPress return to end program!\n");
    	
    	//pausing program until user ends program
    	getch();
    	return;
    	
    }	
    
    //calculate the sales tax on the purchase amount for the Del Mar store	
    float tax_delmar(float purchase_amt) 
    {
    	sales_tax = purchase_amt * 0.0725;
    	return sales_tax;
    }
    //calculate the sales tax on the purchase amount for the Encinitas store
    float tax_encinitas(float purchase_amt) 
    {
    	sales_tax = purchase_amt * 0.075;
    	return sales_tax;
    }
    //calculate the sales tax on the purchase amount for the La Jolla store	
    float tax_lajolla(float purchase_amt) 
    {
    	sales_tax = purchase_amt * 0.0775;
    	return sales_tax;
    }
    //add up a total for the purchase amount plus the sales tax for that amount
    float add_total(float sales_tax, float purchase_amt)
    {
    	total = sales_tax + purchase_amt;
    	return total;
    }

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    87
    Your choice for loops are:
    for, while, and do

    I'll do the easier comparison first, while and do.
    A while looks like
    Code:
    while (condition) {
      statement(s);
    }
    So, if condition isn't true to begin with, the loop will never be executed.

    A do loop looks like
    Code:
    do {
      statement(s);
    } while (condtion);
    So the statement(s) in the body of a do loop with always be executed at least once. If you want to read in some input and loop until it satisfies some criteria, you may often want a do loop.

    The for loop looks something like:
    Code:
    for(statement1, condition, statement2) {
      statement(s);
    }
    and is the same as (stealing from K&R)
    Code:
    statement1;
    while (condition) {
      statement(s);
      statement2;
    }
    So, a for is just a special kind of while loop. The context of the problem usually indicated which is better.

    Hope that helps some.
    Last edited by jason_m; 05-27-2008 at 09:56 PM. Reason: finger slipped! wasn't done typing

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Don't forget that scanf will leave stuff in the input buffer if the user enters incorrect information.
    Therefore, the easiest approach to this problem is typically to read a string, then convert to appropriate data type and check if the conversion succeeds. If it doesn't, then it's a bad input.
    String to float -> strtof.
    And for the char, just grab the first character in the string you read.
    For reading strings, use fgets.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    University of Phoenix assignment? Seems I've seen this before...

  5. #5
    t_6
    t_6 is offline
    Registered User
    Join Date
    May 2008
    Posts
    2
    Thanks everybody! Yes, it's for UOP.

Popular pages Recent additions subscribe to a feed

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