Develop a program that computes the new price of an item.
The program should receive a
character variable colour and a double precision
floating-point variable price from the
user. Discount rate is determined based on the colour of the
discount sticker, as shown in the
following table. An error message should be printed if an
invalid colour has been entered
Answer Posted / tw
I'm still learning, but I made this. The color input isn't
validated for random strings characters (will get a seg
fault). Still need to add the calculations
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void colors() {
int ctr = 0;
double price;
char color[20], tmp[20];
char colorMenu[9][20] = {" ", "WHITE", "BLACK", "RED",
"GREEN", "BLUE", "ORANGE", "YELLOW", "PURPLE"};
int discount[9]= { 0, 15, 20, 25, 30, 35, 40, 45, 50 };
do {
printf("\n\nEnter the discount sticker color -> ");
fgets(tmp, 20, stdin);
sscanf(tmp, "%s", &color);
} while (isalpha(color[1]) == 0);
do {
printf("\n\nEnter the price -> ");
fgets(tmp, 20, stdin);
} while (sscanf(tmp, "%f", &price) == 0);
for (int x = 0; color[x] != '\0'; x++)
color[x] = toupper(color[x]);
for (ctr = 0; (strcmp(color, colorMenu[ctr]) != 0); ctr++);
printf("\nPICK: %d", ctr);
printf("\nYOU CHOSE: %s, DISCOUNT: %d percent off",
colorMenu[ctr], discount[ctr]);
}
int main() {
colors();
return (0);
}
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is string concatenation in c?
How to delete a node from linked list w/o using collectons?
Explain what does it mean when a pointer is used in an if statement?
What extern c means?
Write a program to print all permutations of a given string.
What are valid signatures for the Main function?
Why clrscr is used after variable declaration?
What is the use of function in c?
1.int a=10; 2.int b=20; 3. //write here 4.b=30; Write code at line 3 so that when the value of b is changed variable a should automatically change with same value as b. 5.
What is sizeof int?
How are strings stored in c?
What does the error message "DGROUP exceeds 64K" mean?
How can I split up a string into whitespace-separated fields?
Why #include is used in c language?
What's a good way to check for "close enough" floating-point equality?