473,405 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Question using C

22
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.

Expand|Select|Wrap|Line Numbers
  1. /*
  2.      File:  lab3.c
  3.      Author:  Nick Cantey
  4.      Description:  This program calculates and prints a receipt for purchases.
  5.      Date created:  2/22/2007
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. /* Constants for program */
  11.  
  12. #define TEACHER_DISCOUNT_SMALL 0.1
  13. #define TEACHER_DISCOUNT_BIG 0.12
  14. #define SALES_TAX 0.05
  15.  
  16. int main ()
  17.  
  18. {  
  19.      FILE* out;    /* sets out to point to a file */ 
  20.      char teacher; /* sets teacher to be a variable for main */ 
  21.      double salesTax, purchase_amount, discount, discount_total, total; /* variables for main */
  22.  
  23.      /* Prompting for total price of purchase and if buyer is a teacher */
  24.  
  25.      printf("Please enter total purchase amount: ");
  26.      scanf("%lf", &purchase_amount);
  27.      fflush(stdin);     
  28.      printf("Is customer a music teacher (y/n)?: ");
  29.      scanf("%c", &teacher);
  30.  
  31.      /* Opens receipt file */
  32.  
  33.      out = fopen("receipt.txt", "w"); 
  34.  
  35.      /* Determines price for teacher with a 12% discount */
  36.  
  37.      if (teacher == 'y' && purchase_amount >=100)
  38.      {
  39.               discount = purchase_amount*TEACHER_DISCOUNT_BIG;  
  40.               discount_total = purchase_amount-discount;
  41.               salesTax = discount_total*SALES_TAX;
  42.               total = discount_total+salesTax;
  43.               fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount);
  44.               fprintf(out, "Teacher Discount(12%%)\t$%10.2lf\n", discount);
  45.               fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
  46.               fprintf(out, "Total\t\t\t$%10.2lf\n", total);  
  47.               fclose(out); 
  48.      }
  49.  
  50.      /* Determines price for teacher with a 10% discount */
  51.  
  52.      else if ( teacher == 'y' && purchase_amount <100)
  53.      {
  54.               discount = purchase_amount*TEACHER_DISCOUNT_SMALL;
  55.               discount_total = purchase_amount-discount;
  56.               salesTax = discount_total*SALES_TAX;
  57.               total = discount_total+salesTax;
  58.               fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount); 
  59.               fprintf(out, "Teacher Discount(10%%)\t$%10.2lf\n", discount);
  60.               fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
  61.               fprintf(out, "Total \t\t\t$%10.2lf\n", total);
  62.               fclose(out);
  63.      }
  64.  
  65.      /* Determines price for a regular customer */
  66.  
  67.      else if ( teacher == 'n' && purchase_amount >0 )
  68.      {
  69.               salesTax = purchase_amount*SALES_TAX; 
  70.               total = purchase_amount+salesTax;  
  71.               fprintf(out, "Total Purchase\t\t$%10.2lf\n", purchase_amount);
  72.               fprintf(out, "Sales Tax(5%%)\t\t$%10.2lf\n", salesTax);
  73.               fprintf(out, "Total\t\t\t$%10.2lf\n", total);
  74.               fclose(out);
  75.      } 
  76.  
  77.      return; 
  78.  
  79. }
  80.  
  81.  
Mar 1 '07 #1
2 1578
DeMan
1,806 1GB
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)
Expand|Select|Wrap|Line Numbers
  1.  
  2. char toLower(char myChar)
  3. {
  4.   if((myChar >= 'A') && (myChar <='Z')
  5.   {
  6.     return (myChar -('A' - 'a'));  
  7.   }
  8.   return myChar;  //character is already lowercase or not a char that has case
  9. }
  10.  
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(teacher) == 'y')) (not such a good idea, because it means alterring LOTS of code)
Mar 1 '07 #2
Ganon11
3,652 Expert 2GB
Either that, or every time you test for 'y', you test if it equals 'y' OR it equals 'Y'. Same with 'n'.
Mar 1 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Simon Harvey | last post by:
Hi everyone, I was wondering if it is possible, to use SQL to return more than one table at a time into a dataset. I only know the basics of SQL and so I'm not sure if I'm just asking a stupid...
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
5
by: sea | last post by:
I have 2 applications, one a Microsoft Visual Basic application and the other a Java application both connecting to the same DB2 database. The database has binary LOB objects. The Visual Basic...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
13
by: Elaine | last post by:
This has to do with self-joins and includes a running balance problem. Is it possible to do this using SQL alone? Amortization schedule -------------------------- Givens: beginning balance,...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.