Current below code, the while loop is printing out to screen a duplicate \"Pleas
ID: 3544277 • Letter: C
Question
Current below code, the while loop is printing out to screen a duplicate "Please enter Y or N:". At first prompt "is the customer a teacher (y/n)?:" b, once it reads that it is not y/n, then prompts user to "Please enter Y or N:" twice one after the other. I'm not sure where the issue lies within the code. Below, after my code is the ouput of the current code, and below that is output of what the program is supposed to be doing. Any help is appreciated. Thanks.
code:
#include <stdio.h>
#include <math.h>
#include <ctype.h>
# define teach_rate10 0.1f
# define teach_rate12 0.12f
# define sales_tax 0.09975f
int main (void)
{
char ch;
float price, total, total1, discount, tax;
printf("Enter the total purchase price: ");
scanf("%f", &price);
ch = getchar()!= ' '; /* enter key */
printf("Is the customer a teacher (Y/N)?: ");
ch = getchar();
ch= toupper(ch);
while(ch != 'Y' && ch != 'N')
{
if(ch != 'Y' && ch !='N')
{
if(ch == 'Y' || ch == 'N')
ch = getchar()!= ' ';
printf("Please enter 'Y' OR 'N': ");
ch = getchar();
ch= toupper(ch);
}
if(ch == 'Y')
{
if(price > 100.00 && price < 9999.99)
{
discount = teach_rate12 * price;
total = price - discount;
tax = total * sales_tax;
total1 = tax+total;
printf("Total Purchases $ %.2f ", price);
printf("Teacher's Discount (10) $ %.2f ", discount);
printf("Discounted Total $ %.2f ", total);
printf("Sales Tax (9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
else if (price < 100)
{
discount = teach_rate10 * price;
total = price - discount;
tax = total * sales_tax;
total1 = tax+total;
printf(" Total Purchases $ %.2f ", price);
printf("Teacher's Discount (10) $ %.2f ", discount);
printf("Discounted Total $ %.2f ", total);
printf("Sales Tax (9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
}
else if(ch == 'N')
{
tax = price * sales_tax;
total1 = tax+ price;
printf("Total Purchases $ %.2f ", price);
printf("Sales Tax 9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
}
return 0;
}
output from my program:
Enter the total purchase price: 122.00
Is the customer a teacher (Y/N)?: q
Please enter 'Y' OR 'N': Please enter 'Y' OR 'N': y
Total Purchases $ 122.00
Teacher's Discount (10) $ 14.64
Discounted Total $ 107.36
Sales Tax (9.975) $ 10.71
Total $ 118.07
Example of what it is supposed to do below.
input:
Enter the total purchase price: 10.0
Is the customer a teacher (y/n)?: y
output:
total purchases: $10.00
teacher's discount (10%): $1.00
discounted total: $9.00
sales Tax (9.975%) $0.90
total: $9.90
input:
Enter the total purchase price: 122.00
Is the customer a teacher (y/n)?: q
Pleaser enter 'Y' or 'N': y
output:
total purchases: $122.00
Teacher's discount (12%): $14.64
Discounted Total: $107.36
Sales Tax (9.975%): $10.71
Total: $ 118.07
input:
Enter the total purchase price: 24.90
Is the customer a teacher (y/n)?: n
output:
total purchases: $24.90
sales tax (9.975%): $2.48
Total: $27.38
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <math.h>
#include <ctype.h>
# define teach_rate10 0.1f
# define teach_rate12 0.12f
# define sales_tax 0.09975f
int main (void)
{
char ch;
float price, total, total1, discount, tax;
printf("Enter the total purchase price: ");
scanf("%f", &price);
while(getchar()!= ' '); /* enter key */
printf("Is the customer a teacher (Y/N)?: ");
ch = getchar();
ch= toupper(ch);
while(ch != 'Y' && ch != 'N')
{while( getchar()!= ' ');
printf("Please enter 'Y' OR 'N': ");
ch = getchar();
ch= toupper(ch);
}
while(getchar()!= ' ');
if(ch == 'Y')
{
if(price > 100.00 && price < 9999.99)
{
discount = teach_rate12 * price;
total = price - discount;
tax = total * sales_tax;
total1 = tax+total;
printf("Total Purchases $ %.2f ", price);
printf("Teacher's Discount (10) $ %.2f ", discount);
printf("Discounted Total $ %.2f ", total);
printf("Sales Tax (9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
else if (price < 100)
{
discount = teach_rate10 * price;
total = price - discount;
tax = total * sales_tax;
total1 = tax+total;
printf(" Total Purchases $ %.2f ", price);
printf("Teacher's Discount (10) $ %.2f ", discount);
printf("Discounted Total $ %.2f ", total);
printf("Sales Tax (9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
}
else if(ch == 'N')
{
tax = price * sales_tax;
total1 = tax+ price;
printf("Total Purchases $ %.2f ", price);
printf("Sales Tax 9.975) $ %.2f ", tax);
printf("Total $ %.2f", total1);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.