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


Please Help Members By Posting Answers For Below Questions

What would the following code segment printint k = 8;docout << "k = " << k << " ";while k++ < 5; a) 13 b) 5 c) 8 d) pointers

690


How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?

600


What is string length in c?

622


What are the loops in c?

603


Tell me is null always defined as 0(zero)?

683






What are the different types of objects used in c?

587


What kind of structure is a house?

568


What is the use of sizeof () in c?

573


What is the benefit of using #define to declare a constant?

614


How many levels of indirection in pointers can you have in a single declaration?

605


What is #include in c?

612


What does it mean when the linker says that _end is undefined?

649


What are identifiers in c?

647


Where define directive used?

615


What are the preprocessor categories?

647