Question using C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • canteyn
    New Member
    • Feb 2007
    • 22

    Question using C

    Here is my problem, my code is working correctly, and the program is functioning as it should, however I want the person using the program to be able to enter 'y', 'Y', 'n', or 'N' for whether or not the buyer is a teacher, currently I have it setup to where the only way it works is with the lowercase 'y' and 'n' .... could someone please check my code below and tell me what I need to add to have this work with the uppercase letters as well. Thanks.

    Code:
    /*
         File:  lab3.c
         Author:  Nick Cantey
         Description:  This program calculates and prints a receipt for purchases.
         Date created:  2/22/2007
    */
    
    #include <stdio.h>
    
    /* Constants for program */
    
    #define TEACHER_DISCOUNT_SMALL 0.1
    #define TEACHER_DISCOUNT_BIG 0.12
    #define SALES_TAX 0.05
    
    int main ()
    
    {  
         FILE* out;    /* sets out to point to a file */ 
         char teacher; /* sets teacher to be a variable for main */ 
         double salesTax, purchase_amount, discount, discount_total, total; /* variables for main */
    
         /* Prompting for total price of purchase and if buyer is a teacher */
    
         printf("Please enter total purchase amount: ");
         scanf("%lf", &purchase_amount);
         fflush(stdin);     
         printf("Is customer a music teacher (y/n)?: ");
         scanf("%c", &teacher);
         
         /* Opens receipt file */
     
         out = fopen("receipt.txt", "w"); 
    
         /* Determines price for teacher with a 12% discount */
    
         if (teacher == 'y' && purchase_amount >=100)
         {
                  discount = purchase_amount*TEACHER_DISCOUNT_BIG;  
                  discount_total = purchase_amount-discount;
                  salesTax = discount_total*SALES_TAX;
                  total = discount_total+salesTax;
                  fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount);
                  fprintf(out, "Teacher Discount(12%%)\t$%10.2lf\n", discount);
                  fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
                  fprintf(out, "Total\t\t\t$%10.2lf\n", total);  
                  fclose(out); 
         }
    
         /* Determines price for teacher with a 10% discount */
    
         else if ( teacher == 'y' && purchase_amount <100)
         {
                  discount = purchase_amount*TEACHER_DISCOUNT_SMALL;
                  discount_total = purchase_amount-discount;
                  salesTax = discount_total*SALES_TAX;
                  total = discount_total+salesTax;
                  fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount); 
                  fprintf(out, "Teacher Discount(10%%)\t$%10.2lf\n", discount);
                  fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
                  fprintf(out, "Total \t\t\t$%10.2lf\n", total);
                  fclose(out);
         }
        
         /* Determines price for a regular customer */
    
         else if ( teacher == 'n' && purchase_amount >0 )
         {
                  salesTax = purchase_amount*SALES_TAX; 
                  total = purchase_amount+salesTax;  
                  fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount);
                  fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
                  fprintf(out, "Total\t\t\t$%10.2lf\n", total);
                  fclose(out);
         } 
    
         return; 
    
    }
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I think one of the standard libraries has this, however (as I'm not really sure which one) it is a reasonably simple method to implement.....

    Thsi method will change an upper case to a lower case (since that is what you test for)
    Code:
    char toLower(char myChar)
    {
      if((myChar >= 'A') && (myChar <='Z')
      {
        return (myChar -('A' - 'a'));  
      }
      return myChar;  //character is already lowercase or not a char that has case
    }
    You can either do
    teacher = toLower(teacher ) when you first read teacher in (probably the better idea), or you can use this at each if (if(toLower(tea cher) == 'y')) (not such a good idea, because it means alterring LOTS of code)

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Either that, or every time you test for 'y', you test if it equals 'y' OR it equals 'Y'. Same with 'n'.

      Comment

      Working...