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
Describe static function with its usage?
Can a pointer be volatile in c?
Why is it important to memset a variable, immediately after allocating memory to it ?
What is the time and space complexities of merge sort and when is it preferred over quick sort?
Place the #include statement must be written in the program?
What is the difference between specifying a constant variable like with constant keyword and #define it? i.e what is the difference between CONSTANT FLOAT A=1.25 and #define A 1.25
What is the difference between pure virtual function and virtual function?
What's the difference between constant char *p and char * constant p?
What does != Mean in c?
What are integer variable, floating-point variable and character variable?
The postoder traversal is 7,14,3,55,22,5,17 Then ur Inorder traversal is??? please help me on this
Is it better to use a macro or a function?
a character or group of characters that defines a register,or a part of storage a) memory b) byte c) address d) linear list
Why are some ANSI/ISO Standard library routines showing up as undefined, even though I've got an ANSI compiler?
What are pointers really good for, anyway?